- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建 NT 格式的网络时间戳。
我已经能够使用此函数将它们转换为可读时间:
NetworkStamp = "\xc0\x65\x31\x50\xde\x09\xd2\x01"
def GetTime(NetworkStamp):
Ftime = int(struct.unpack('<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)
输出:
(datetime.datetime(2016, 9, 8, 11, 35, 57), '2016-09-08 11:35:57')
现在我想做相反的事情,将 '2016/09/08 11:35:57'
秒转换为以下格式:
"\xc0\x65\x31\x50\xde\x09\xd2\x01"
最佳答案
将 Window 的 FILETIME
值转换为 datetime.datetime
的代码并不像它应有的那么准确 - 它截断了可能存在的任何小数秒(因为它忽略了 divmod()
结果的其余部分)。这在代码创建的可读字符串中并不明显,因为它只显示整秒。
即使包含秒小数部分,您也无法完全按照您的要求进行操作,因为 Windows FILETIME
结构的值间隔为 100 纳秒(0.1 微秒),但 Python 的 datetime
仅支持精确到整微秒。因此,最好的办法是近似原始值,因为即使进行最准确的转换,也会丢失信息。
下面是适用于 Python 2 和 3 的代码,使用问题中的 NetworkStamp
测试值演示了这一点:
import datetime
import struct
import time
WINDOWS_TICKS = int(1/10**-7) # 10,000,000 (100 nanoseconds or .1 microseconds)
WINDOWS_EPOCH = datetime.datetime.strptime('1601-01-01 00:00:00',
'%Y-%m-%d %H:%M:%S')
POSIX_EPOCH = datetime.datetime.strptime('1970-01-01 00:00:00',
'%Y-%m-%d %H:%M:%S')
EPOCH_DIFF = (POSIX_EPOCH - WINDOWS_EPOCH).total_seconds() # 11644473600.0
WINDOWS_TICKS_TO_POSIX_EPOCH = EPOCH_DIFF * WINDOWS_TICKS # 116444736000000000.0
def get_time(filetime):
"""Convert windows filetime winticks to python datetime.datetime."""
winticks = struct.unpack('<Q', filetime)[0]
microsecs = (winticks - WINDOWS_TICKS_TO_POSIX_EPOCH) / WINDOWS_TICKS
return datetime.datetime.fromtimestamp(microsecs)
def convert_back(timestamp_string):
"""Convert a timestamp in Y=M=D H:M:S.f format into a windows filetime."""
dt = datetime.datetime.strptime(timestamp_string, '%Y-%m-%d %H:%M:%S.%f')
posix_secs = int(time.mktime(dt.timetuple()))
winticks = (posix_secs + int(EPOCH_DIFF)) * WINDOWS_TICKS
return winticks
def int_to_bytes(n, minlen=0): # helper function
""" int/long to bytes (little-endian byte order).
Note: built-in int.to_bytes() method could be used in Python 3.
"""
nbits = n.bit_length() + (1 if n < 0 else 0) # plus one for any sign bit
nbytes = (nbits+7) // 8 # number of whole bytes
ba = bytearray()
for _ in range(nbytes):
ba.append(n & 0xff)
n >>= 8
if minlen > 0 and len(ba) < minlen: # zero pad?
ba.extend([0] * (minlen-len(ba)))
return ba # with low bytes first
def hexbytes(s): # formatting helper function
"""Convert string to string of hex character values."""
ba = bytearray(s)
return ''.join('\\x{:02x}'.format(b) for b in ba)
win_timestamp = b'\xc0\x65\x31\x50\xde\x09\xd2\x01'
print('win timestamp: b"{}"'.format(hexbytes(win_timestamp)))
dtime = get_time(win_timestamp)
readable = dtime.strftime('%Y-%m-%d %H:%M:%S.%f') # includes fractional secs
print('datetime repr: "{}"'.format(readable))
winticks = convert_back(readable) # includes fractional secs
new_timestamp = int_to_bytes(winticks)
print('new timestamp: b"{}"'.format(hexbytes(new_timestamp)))
输出:
win timestamp: b"\xc0\x65\x31\x50\xde\x09\xd2\x01"
datetime repr: "2016-09-08 07:35:57.996998"
new timestamp: b"\x80\x44\x99\x4f\xde\x09\xd2\x01"
关于python - 将日期时间转换回 Windows 64 位 FILETIME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39481221/
我有一个 imput .txt 文件,其中包含一个日期。日期采用 DDMONYY 格式,如 08JUN98 或 16NOV05。我需要将此日期与文件的创建日期进行比较,看看哪个更快。 FILETIME
给定一个 FileTime fileTime,如何以自定义方式将其格式化为字符串? String s = fileTime.toString() 仅以 ISO 格式提供。 String s = Dat
在 Looking for Good CTIME/Unixtime converter 问题中,我要求提供一个良好的基于 Web 的 CTIME(自 1970 年 1 月 1 日以来的秒数)时间转
我一直在阅读关于日期的 JavaScript ‘off-by-one’,但我不知道如何在这种情况下实现它。我正在将一些值从 Internet Explorer cookie 转换为 Microsoft
我正在尝试从文件中获取字节数组,将其解释为 uint64_t ,然后将此 uint 转换为 FILETIME 在谷歌搜索并调试了一下之后,我一直坚持遵循错误的工作代码。 uint64_t win_fi
我需要将文件的最后修改时间与数据库中存储的日期时间进行比较。我最初看了this question开始。 我目前正在为文件的最后一次写入获取 FILETIME,将其转换为 SYSTEMTIME。然后,我
我需要将 QDateTime 传递给接受 FILETIME 的 Win32 函数. 这是 MSDN 对 FILETIME 的定义: Contains a 64-bit value representi
假设我有一个 FILETIME,当我在 timezone1 时它有一些值(我们可以通过将它转换为系统时间来查看)。假设我将时区更改为 timezone2。现在我想获得时区 1 中的小时和分钟(格式)时
FILETIME structure根据 Microsoft 文档,从 1601 年 1 月 1 日(大概是那天的开始)开始计数,但这是否包括闰秒? 最佳答案 问题不应该是 FILETIME 是否包含
我将如何转换 Windows FILETIME将对象转换为 time_t 或原始秒/毫秒?我正在将一些代码从 Windows 移植到 Unix,因此我不能依赖 Windows API 函数。 最佳答案
如何将 FILETIME 转换为秒数?我需要比较两个 FILETIME 对象.. 我找到了 this ,但似乎并不能解决问题... ULARGE_INTEGER ull; ull.LowPa
我想从 c/c++ 中的 FILETIME 创建一个 VARIANT 或 _variant_t。 基本上,这就是我想要的: FILETIME ft; //Populate ft VARIANT v
标题说明了一切。互联网上有很多如何将 FILETIME 转换为 TDateTime 的示例,但反之则不然。 最佳答案 从 System.SysUtils 调用 DateTimeToSystemTime
我有秒数的时间(例如:1505306792)。 如何将其转换为 FILETIME? 这是我试过的代码 INT64 timer64 = 1505306792; timer64 = tim
我有这个功能 void prtduration(const FILETIME *ft_start, const FILETIME *ft_end) { double duration = (f
我想存储目录中存在的每个文件的文件名和创建时间。我发现以下代码浏览目录并检测每个文件。问题是我不知道如何存储 WIN32_FIND_DATA 的值。 cFilename 不应该太难,它是 TCHAR(
如何比较FileTime和 LocalDateTime Java 中的对象? 我想将文件 (Files.getLastModifiedTime(item)) 中的 FileTime 对象与当前时间 (
我再次从 Windows 转到 Linux,我必须将计算 NTP 时间的函数从 Windows 移植到 Linux。看起来很简单,但格式是在 Windows FILETIME格式。我有点知道差异是什么
我正在使用 DateTime.ToFileTime 和 FromFileTime 方法在数据库中存储和检索时间戳。最小的 windows 文件时间是 1601 年 1 月 1 日午夜。是否有一个类似于
我有一个文件时间值,例如:122327856000000000 并希望将其转换为 Excel 日期 (1988-aug-23) 最佳答案 我只能找到 unix 时间(自 1970/1/1 以来的秒数)
我是一名优秀的程序员,十分优秀!