1 # Copyright (c) 2009, David Buxton <david@gasmark6.com>
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
15 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 """Tools to convert between Python datetime instances and Microsoft times.
27 from datetime
import datetime
, timedelta
, tzinfo
28 from calendar
import timegm
31 # http://support.microsoft.com/kb/167296
32 # How To Convert a UNIX time_t to a Win32 FILETIME or SYSTEMTIME
33 EPOCH_AS_FILETIME
= 116444736000000000 # January 1, 1970 as MS file time
34 HUNDREDS_OF_NANOSECONDS
= 10000000
38 HOUR
= timedelta(hours
=1)
43 def utcoffset(self
, dt
):
56 def dt_to_filetime(dt
):
57 """Converts a datetime to Microsoft filetime format. If the object is
58 time zone-naive, it is forced to UTC before conversion.
60 >>> "%.0f" % dt_to_filetime(datetime(2009, 7, 25, 23, 0))
63 >>> "%.0f" % dt_to_filetime(datetime(1970, 1, 1, 0, 0, tzinfo=utc))
66 >>> "%.0f" % dt_to_filetime(datetime(1970, 1, 1, 0, 0))
69 >>> dt_to_filetime(datetime(2009, 7, 25, 23, 0, 0, 100))
72 if (dt
.tzinfo
is None) or (dt
.tzinfo
.utcoffset(dt
) is None):
73 dt
= dt
.replace(tzinfo
=utc
)
74 ft
= EPOCH_AS_FILETIME
+ (timegm(dt
.timetuple()) * HUNDREDS_OF_NANOSECONDS
)
75 return ft
+ (dt
.microsecond
* 10)
78 def filetime_to_dt(ft
):
79 """Converts a Microsoft filetime number to a Python datetime. The new
80 datetime object is time zone-naive but is equivalent to tzinfo=utc.
82 >>> filetime_to_dt(116444736000000000)
83 datetime.datetime(1970, 1, 1, 0, 0)
85 >>> filetime_to_dt(128930364000000000)
86 datetime.datetime(2009, 7, 25, 23, 0)
88 >>> filetime_to_dt(128930364000001000)
89 datetime.datetime(2009, 7, 25, 23, 0, 0, 100)
91 # Get seconds and remainder in terms of Unix epoch
92 (s
, ns100
) = divmod(ft
- EPOCH_AS_FILETIME
, HUNDREDS_OF_NANOSECONDS
)
93 # Convert to datetime object
94 dt
= datetime
.utcfromtimestamp(s
)
95 # Add remainder in as microseconds. Python 3.2 requires an integer
96 dt
= dt
.replace(microsecond
=(ns100
// 10))
100 if __name__
== "__main__":