【问题标题】:python convert filetime to datetime for dates before 1970对于 1970 年之前的日期,python 将文件时间转换为日期时间
【发布时间】:2016-12-17 03:31:48
【问题描述】:

我需要将文件时间转换为日期时间。我正在使用此代码filetime.py,来自here,如该线程Datetime to filetime (Python) 中所述。

在代码中

EPOCH_AS_FILETIME = 116444736000000000  # January 1, 1970 as MS file time
HUNDREDS_OF_NANOSECONDS = 10000000

def filetime_to_dt(ft):
    """Converts a Microsoft filetime number to a Python datetime. The new datetime object is time zone-naive but is equivalent to tzinfo=utc.

    >>> filetime_to_dt(116444736000000000)
    datetime.datetime(1970, 1, 1, 0, 0)
    """
    # Get seconds and remainder in terms of Unix epoch
    (s, ns100) = divmod(ft - EPOCH_AS_FILETIME, HUNDREDS_OF_NANOSECONDS)
    # Convert to datetime object
    dt = datetime.utcfromtimestamp(s)
    # Add remainder in as microseconds. Python 3.2 requires an integer
    dt = dt.replace(microsecond=(ns100 // 10))
    return dt

datetime.utcfromtimestamp 在 Windows 系统上不取负值,因此我无法转换 1970 年 1 月 1 日之前的文件时间。但我可以使用完全相同的代码在 Mac 上转换 1970 年之前的日期(原因 here)。 Windows有什么解决方法吗?

【问题讨论】:

  • 所以你的意思是你想在正常日期时间转换一些纪元时间对吗?

标签: python datetime filetime


【解决方案1】:

通过将timedelta 添加到参考日期,您可以使用任何您喜欢的日期公式。 timedelta 可以是正数也可以是负数。

def filetime_to_dt(ft):
    us = (ft - EPOCH_AS_FILETIME) // 10
    return datetime(1970, 1, 1) + timedelta(microseconds = us)

【讨论】:

    【解决方案2】:

    According to docs,你必须使用:

    dt = datetime.fromtimestamp(s, datetime.timezone.utc)
    

    代替:

    dt = datetime.utcfromtimestamp(s)
    

    【讨论】:

    • 对不起,我使用import datetime datetime.timezone.utc 给出错误“AttributeError: 'module' object has no attribute 'timezone'”。这是为什么呢?
    • @Echo:也许你没有使用 Python 3?
    • 我正在使用 Python 2
    【解决方案3】:

    首先您需要将其转换为可识别的文件时间格式,如下所示:

    >>> dwLowDateTime = 0x0F7297A80
    >>> dwHighDateTime = 0x1C3F10F << 32
    >>> ft = (dwLowDateTime & 0xFFFFFFFF) | dwHighDateTime
    127210265370000000
    

    然后使用这个脚本,它将文件时间转换为日期时间,反之亦然https://gist.github.com/Mostafa-Hamdy-Elgiar/9714475f1b3bc224ea063af81566d873

    >>> filetime_to_dt(ft)
    2004-02-12 02:28:57
    

    【讨论】:

      【解决方案4】:

      使用timedeltas 在时间范围之间进行加/减/除:

      FILE_TIME_EPOCH = datetime.datetime(1601, 1, 1)
      FILE_TIME_MICROSECOND = 10 # FILETIME counts 100 nanoseconds intervals = 0.1 microseconds, so 10 of those are 1 microsecond
      
      def convert_from_file_time(file_time):
          microseconds_since_file_time_epoch = file_time // FILE_TIME_MICROSECOND
          return FILE_TIME_EPOCH + datetime.timedelta(microseconds=microseconds_since_file_time_epoch)
      

      另一方面是:

      def convert_to_file_time(date_time):
          microseconds_since_file_time_epoch = (date_time - FILE_TIME_EPOCH) // datetime.timedelta(microseconds=1)
          return microseconds_since_file_time_epoch * FILE_TIME_MICROSECOND
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-23
        • 2017-04-28
        • 2011-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-19
        • 1970-01-01
        相关资源
        最近更新 更多