Convert datetime back to Windows 64-bit FILETIME
<p>I would like to create network timestamps in NT format.</p>

<p>I've been able to convert them to readable time with this function:</p>

<pre><code>NetworkStamp = "\xc0\x65\x31\x50\xde\x09\xd2\x01"

def GetTime(NetworkStamp):
    Ftime = int(struct.unpack('&lt;Q',NetworkStamp)[0])
    Epoch = divmod(Ftime - 116444736000000000, 10000000)
    Actualtime = datetime.datetime.fromtimestamp(Epoch[0])
    return Actualtime, Actualtime.strftime('%Y-%m-%d %H:%M:%S')

print GetTime(NetworkStamp)
</code></pre>

<p>Output:</p>

<pre class="lang-none prettyprint-override"><code>(datetime.datetime(2016, 9, 8, 11, 35, 57), '2016-09-08 11:35:57')
</code></pre>

<p>Now I'd like to do the opposite, converting <code>'2016/09/08 11:35:57'</code> sec to this format:</p>

<pre><code> "\xc0\x65\x31\x50\xde\x09\xd2\x01"
</code></pre>

<p>If you understand how to perform the conversion in one direction, doing it in reverse is basically using the inverse of each method in reverse order. Just look at the documentation for the modules/classes you're using:</p>

<ol>
<li><code>strftime</code> has <a href="https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime" rel="nofollow">a <code>strptime</code> counterpart</a></li>
<li><code>fromtimestamp</code> <a href="https://docs.python.org/3/library/datetime.html#datetime.datetime.timestamp" rel="nofollow">is matched by <code>timestamp</code></a> (if you're on pre-3.3 Python, <code>timestamp</code> doesn't exist, but you could define <code>FILETIME_epoch = datetime.datetime(1601, 1, 1) - datetime.timedelta(seconds=time.altzone if time.daylight else time.timezone)</code> outside the function to precompute a <code>datetime</code> that represents the <code>FILETIME</code> epoch for your timezone, then use <code>int((mydatetime - FILETIME_epoch).total_seconds())</code> to get <code>int</code> seconds since <code>FILETIME</code> epoch directly, without manually adjusting for difference between <code>FILETIME</code> and Unix epoches separately)</li>
<li><code>divmod</code> (which you don't really need, since you only use the quotient, not the remainder, you could just do <code>Epoch = (Ftime - 116444736000000000) // 10000000</code> and avoid indexing later) is trivially reversible (just multiply and add, with the add being unnecessary if you use my trick to convert to <code>FILETIME</code> epoch seconds directly from #2)</li>
<li><code>struct.unpack</code> <a href="https://docs.python.org/3/library/struct.html#struct.pack" rel="nofollow">is matched by <code>struct.pack</code></a></li>
</ol>

<p>I'm not providing the exact code because you really should learn to use these things yourself (and read the docs when necessary); I'm guessing your forward code was written without understanding what it is doing, because if you understood it, the reverse should have been obvious; every step has an inverse documented on the same page.</p>

python python-2.x filetime