日期时间到文件时间 (Python)

发布于 2024-11-16 17:06:48 字数 140 浏览 2 评论 0原文

有什么链接可以让我使用 python 将日期时间转换为文件时间吗?

示例:2011 年 4 月 13 日 07:21:01.0874 (UTC) FILETIME=[57D8C920:01CBF9AB]

从电子邮件标题中获取上述内容。

Any links for me to convert datetime to filetime using python?

Example: 13 Apr 2011 07:21:01.0874 (UTC) FILETIME=[57D8C920:01CBF9AB]

Got the above from an email header.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴我心暖 2024-11-23 17:06:48

我在重复问题中的答案被删除了,所以我将在这里发布:
冲浪我发现这个链接:http://cboard.cprogramming。 com/windows-programming/85330-hex-time-filetime.html

之后,一切都变得简单:

>>> ft = "57D8C920:01CBF9AB"
... # switch parts
... h2, h1 = [int(h, base=16) for h in ft.split(':')]
... # rebuild
... ft_dec = struct.unpack('>Q', struct.pack('>LL', h1, h2))[0]
... ft_dec
... 129471528618740000L
... # use function from iceaway's comment
... print filetime_to_dt(ft_dec)
2011-04-13 07:21:01

调整由您决定。

My answer in duplicated question got deleted, so I'll post here:
Surfing around i found this link: http://cboard.cprogramming.com/windows-programming/85330-hex-time-filetime.html

After that, everything become simple:

>>> ft = "57D8C920:01CBF9AB"
... # switch parts
... h2, h1 = [int(h, base=16) for h in ft.split(':')]
... # rebuild
... ft_dec = struct.unpack('>Q', struct.pack('>LL', h1, h2))[0]
... ft_dec
... 129471528618740000L
... # use function from iceaway's comment
... print filetime_to_dt(ft_dec)
2011-04-13 07:21:01

Tuning it up is up for you.

时光瘦了 2024-11-23 17:06:48

这是我最终根据

    parm3=0x57D8C920; parm3=0x01CBF9AB

    #Int32x32To64
    ft_dec = struct.unpack('>Q', struct.pack('>LL', parm4, parm3))[0]

    from datetime import datetime
    EPOCH_AS_FILETIME = 116444736000000000;  HUNDREDS_OF_NANOSECONDS = 10000000
    dt = datetime.fromtimestamp((ft_dec - EPOCH_AS_FILETIME) / HUNDREDS_OF_NANOSECONDS)

    print dt

Output will be:
 2011-04-13 09:21:01          (GMT +1)
13 Apr 2011 07:21:01.0874 (UTC)

David Buxton 'filetimes.py' 得到 的解决方案
^-请注意,小时数有所不同

我更改了两件事:

  1. fromtimestamp() 比 *UTC*fromtimestamp() 更适合,因为我在这里处理文件时间。
  2. FAT 时间分辨率为 2 秒,因此我不关心可能会崩溃的 100 纳秒休息时间。
    (实际上,由于分辨率为 2 秒,通常在划分 HUNDREDS_OF_NANOSECONDS 时不会休息)

...除了参数传递的顺序之外,请注意 struct.pack('>LL' 用于无符号 32 位 Int!

如果您已签署 int,只需将其更改为 struct.pack('>ll' for signed 32bit Int's!

(或点击struct.pack 上面的链接了解更多信息)

Well here is the solution I end up with

    parm3=0x57D8C920; parm3=0x01CBF9AB

    #Int32x32To64
    ft_dec = struct.unpack('>Q', struct.pack('>LL', parm4, parm3))[0]

    from datetime import datetime
    EPOCH_AS_FILETIME = 116444736000000000;  HUNDREDS_OF_NANOSECONDS = 10000000
    dt = datetime.fromtimestamp((ft_dec - EPOCH_AS_FILETIME) / HUNDREDS_OF_NANOSECONDS)

    print dt

Output will be:
 2011-04-13 09:21:01          (GMT +1)
13 Apr 2011 07:21:01.0874 (UTC)

base on David Buxton 'filetimes.py'
^-Note that theres a difference in the hours

Well I changes two things:

  1. fromtimestamp() fits somehow better than *UTC*fromtimestamp() since I'm dealing with file times here.
  2. FAT time resolution is 2 seconds so I don't care about the 100ns rest that might fall apart.
    (Well actually since resolution is 2 seconds normally there be no rest when dividing HUNDREDS_OF_NANOSECONDS )

... and beside the order of parameter passing pay attention that struct.pack('>LL' is for unsigned 32bit Int's!

If you've signed int's simply change it to struct.pack('>ll' for signed 32bit Int's!

(or click the struct.pack link above for more info)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文