Package pike :: Module nttime
[hide private]
[frames] | no frames]

Source Code for Module pike.nttime

  1  # 
  2  # Copyright (c) 2013, EMC Corporation 
  3  # All rights reserved. 
  4  # 
  5  # Redistribution and use in source and binary forms, with or without 
  6  # modification, are permitted provided that the following conditions are met: 
  7  # 
  8  # 1. Redistributions of source code must retain the above copyright notice, 
  9  # this list of conditions and the following disclaimer. 
 10  # 2. Redistributions in binary form must reproduce the above copyright notice, 
 11  # this list of conditions and the following disclaimer in the documentation 
 12  # and/or other materials provided with the distribution. 
 13  # 
 14  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 15  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 16  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 17  # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
 18  # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 19  # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 20  # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 21  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 22  # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 23  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 24  # POSSIBILITY OF SUCH DAMAGE. 
 25  # 
 26  # Module Name: 
 27  # 
 28  #        nttime.py 
 29  # 
 30  # Abstract: 
 31  # 
 32  #        NT time format utility class 
 33  # 
 34  # Authors: Rafal Szczesniak (rafal.szczesniak@isilon.com) 
 35  #          Masen Furer (masen.furer@dell.com) 
 36  # 
 37   
 38  from datetime import datetime, timedelta 
 39  import math 
 40  import time 
 41   
 42  _unix_time_offset = 11644473600L 
 43  _unix_epoch = datetime.fromtimestamp(0) + timedelta(hours=time.localtime().tm_isdst) 
 44  _intervals_per_second = 10000000L 
 45   
46 -def _unix_time_to_nt_time(t):
47 return (t + _unix_time_offset) * _intervals_per_second
48
49 -def _datetime_to_unix_time(t):
50 td = (t - _unix_epoch) 51 return td.seconds + (td.days * 24 * 3600)
52
53 -def _datetime_to_nt_time(t):
54 return _unix_time_to_nt_time(_datetime_to_unix_time(t))
55
56 -def _nt_time_to_unix_time(t):
57 """ 58 Converts "FILETIME" to Python's time library format. 59 60 routine borrowed from dist_shell: sys_ex/windows/file.py 61 """ 62 63 # This calculation below might seem rather complex for such time 64 # conversions, however, due to issues with floating point 65 # arithmetics precision, this HACK is required to compute the time 66 # in Python's time library format such that times like "12:59.59.1" 67 # don't turn into "01:00:00.0" when converted back. This 68 # calculation below makes use of Python's bigint capabilities. 69 py_time = (t << 32) / _intervals_per_second 70 py_time -= _unix_time_offset << 32 71 py_time_parts = divmod(py_time, 2**32) 72 py_time = float(py_time_parts[0]) 73 py_time += math.copysign(py_time_parts[1], py_time) / 2**32 74 return py_time
75
76 -def GMT_to_datetime(gmt_token):
77 dt_obj = datetime.strptime( 78 gmt_token, 79 "@GMT-%Y.%m.%d-%H.%M.%S") 80 # apply timezone conversion 81 dt_obj -= timedelta(seconds=time.timezone) 82 return dt_obj
83
84 -class NtTime(long):
85 """ 86 NtTime may be initialized with any of the following values 87 * string in ISO format or @GMT- format (timewarp) 88 * datetime 89 * int/long indicating number of 100-nanosecond intervals since 1601-01-01 90 """
91 - def __new__(cls, value):
92 if isinstance(value, basestring): 93 if value.startswith("@GMT-"): 94 dt = GMT_to_datetime(value) 95 else: 96 dt = datetime.strptime(value, "%Y-%m-%d %H:%M:%S") 97 value = _datetime_to_nt_time(dt) 98 elif isinstance(value, datetime): 99 value = _datetime_to_nt_time(value) 100 101 return super(NtTime, cls).__new__(cls, long(value))
102
103 - def __repr__(self):
104 return str(self)
105
106 - def __str__(self):
107 return self.to_datetime().isoformat(" ")
108
109 - def to_datetime(self):
110 return datetime.fromtimestamp(_nt_time_to_unix_time(self))
111
112 - def to_pytime(self):
113 return _nt_time_to_unix_time(self)
114
115 - def to_unixtime(self):
116 return long(self.to_pytime())
117