|
32 | 32 |
|
33 | 33 | import sys
|
34 | 34 | import decimal
|
35 |
| -import time |
36 |
| - |
37 | 35 | from datetime import datetime as Timestamp, date as Date, time as Time
|
| 36 | +from datetime import tzinfo # pylint: disable=unused-import |
38 | 37 | from datetime import timedelta as TimeDelta
|
39 | 38 |
|
40 | 39 | try:
|
41 | 40 | from typing import Tuple, Union # pylint: disable=unused-import
|
42 | 41 | except ImportError:
|
43 | 42 | pass
|
44 | 43 |
|
| 44 | +import tzlocal |
45 | 45 | from .exception import DataError
|
| 46 | +from .calendar import ymd2day, day2ymd |
| 47 | + |
| 48 | +# zoneinfo.ZoneInfo is preferred but not introduced into python3.9 |
| 49 | +if sys.version_info >= (3, 9): |
| 50 | + # used for python>=3.9 with support for zoneinfo.ZoneInfo |
| 51 | + from zoneinfo import ZoneInfo # pylint: disable=unused-import |
| 52 | + from datetime import timezone as TimeZone |
| 53 | + |
| 54 | + def utc_TimeStamp(year, month, day, hour=0, minute=0, second=0, microsecond=0): |
| 55 | + # type: (int,int,int,int,int,int,int) -> Timestamp |
| 56 | + """ |
| 57 | + timezone aware datetime with UTC timezone. |
| 58 | + """ |
| 59 | + return Timestamp(year=year, month=month, day=day, |
| 60 | + hour=hour, minute=minute, second=second, |
| 61 | + microsecond=microsecond, tzinfo=TimeZone.utc) |
| 62 | + |
| 63 | + def timezone_aware(tstamp, tz_info): |
| 64 | + # type: (Timestamp, tzinfo) -> Timestamp |
| 65 | + return tstamp.replace(tzinfo=tz_info) |
| 66 | + |
| 67 | +else: |
| 68 | + # used for python<3.9 without support for zoneinfo.ZoneInfo |
| 69 | + import pytz as TimeZone |
| 70 | + |
| 71 | + def utc_TimeStamp(year, month, day, hour=0, minute=0, second=0, microsecond=0): |
| 72 | + # type: (int,int,int,int,int,int,int) -> Timestamp |
| 73 | + """ |
| 74 | + timezone aware datetime with UTC timezone. |
| 75 | + """ |
| 76 | + dt = Timestamp(year=year, month=month, day=day, |
| 77 | + hour=hour, minute=minute, second=second, microsecond=microsecond) |
| 78 | + return TimeZone.utc.localize(dt, is_dst=None) |
| 79 | + |
| 80 | + def timezone_aware(tstamp, tz_info): |
| 81 | + # type: (Timestamp, tzinfo) -> Timestamp |
| 82 | + return tz_info.localize(tstamp, is_dst=None) |
| 83 | + |
46 | 84 |
|
47 | 85 | isP2 = sys.version[0] == '2'
|
| 86 | +TICKSDAY = 86400 |
| 87 | +localZoneInfo = tzlocal.get_localzone() |
48 | 88 |
|
49 | 89 |
|
50 | 90 | class Binary(bytes):
|
@@ -83,56 +123,126 @@ def string(self):
|
83 | 123 | def DateFromTicks(ticks):
|
84 | 124 | # type: (int) -> Date
|
85 | 125 | """Convert ticks to a Date object."""
|
86 |
| - return Date(*time.localtime(ticks)[:3]) |
| 126 | + y, m, d = day2ymd(ticks // TICKSDAY) |
| 127 | + return Date(year=y, month=m, day=d) |
87 | 128 |
|
88 | 129 |
|
89 |
| -def TimeFromTicks(ticks, micro=0): |
90 |
| - # type: (int, int) -> Time |
| 130 | +def TimeFromTicks(ticks, micro=0, zoneinfo=localZoneInfo): |
| 131 | + # type: (int,int, tzinfo) -> Time |
91 | 132 | """Convert ticks to a Time object."""
|
92 |
| - return Time(*time.localtime(ticks)[3:6] + (micro,)) |
93 |
| - |
94 |
| - |
95 |
| -def TimestampFromTicks(ticks, micro=0): |
96 |
| - # type: (int, int) -> Timestamp |
| 133 | + seconds = ticks % TICKSDAY |
| 134 | + hours = (seconds // 3600) % 24 |
| 135 | + minutes = (seconds // 60) % 60 |
| 136 | + seconds = seconds % 60 |
| 137 | + tstamp = Timestamp.combine(Date(1970, 1, 1), |
| 138 | + Time(hour=hours, |
| 139 | + minute=minutes, |
| 140 | + second=seconds, |
| 141 | + microsecond=micro) |
| 142 | + ) |
| 143 | + # remove offset that the engine added |
| 144 | + utcoffset = zoneinfo.utcoffset(tstamp) |
| 145 | + if utcoffset: |
| 146 | + tstamp += utcoffset |
| 147 | + # returns naive time , should a timezone-aware time be returned instead |
| 148 | + return tstamp.time() |
| 149 | + |
| 150 | + |
| 151 | +def TimestampFromTicks(ticks, micro=0, zoneinfo=localZoneInfo): |
| 152 | + # type: (int, int, tzinfo) -> Timestamp |
97 | 153 | """Convert ticks to a Timestamp object."""
|
98 |
| - return Timestamp(*time.localtime(ticks)[:6] + (micro,)) |
| 154 | + day = ticks // TICKSDAY |
| 155 | + y, m, d = day2ymd(day) |
| 156 | + timeticks = ticks % TICKSDAY |
| 157 | + hour = timeticks // 3600 |
| 158 | + sec = timeticks % 3600 |
| 159 | + min = sec // 60 |
| 160 | + sec %= 60 |
| 161 | + |
| 162 | + # this requires both utc and current session to be between year 1 and year 9999 inclusive. |
| 163 | + # nuodb could store a timestamp that is east of utc where utc would be year 10000. |
| 164 | + if y < 10000: |
| 165 | + dt = utc_TimeStamp(year=y, month=m, day=d, hour=hour, |
| 166 | + minute=min, second=sec, microsecond=micro) |
| 167 | + dt = dt.astimezone(zoneinfo) |
| 168 | + else: |
| 169 | + # shift one day. |
| 170 | + dt = utc_TimeStamp(year=9999, month=12, day=31, hour=hour, |
| 171 | + minute=min, second=sec, microsecond=micro) |
| 172 | + dt = dt.astimezone(zoneinfo) |
| 173 | + # add day back. |
| 174 | + dt += TimeDelta(days=1) |
| 175 | + # returns timezone-aware datetime |
| 176 | + return dt |
99 | 177 |
|
100 | 178 |
|
101 | 179 | def DateToTicks(value):
|
102 | 180 | # type: (Date) -> int
|
103 | 181 | """Convert a Date object to ticks."""
|
104 |
| - timeStruct = Date(value.year, value.month, value.day).timetuple() |
105 |
| - try: |
106 |
| - return int(time.mktime(timeStruct)) |
107 |
| - except Exception: |
108 |
| - raise DataError("Year out of range") |
109 |
| - |
110 |
| - |
111 |
| -def TimeToTicks(value): |
112 |
| - # type: (Time) -> Tuple[int, int] |
| 182 | + day = ymd2day(value.year, value.month, value.day) |
| 183 | + return day * TICKSDAY |
| 184 | + |
| 185 | + |
| 186 | +def _packtime(seconds, microseconds): |
| 187 | + # type: (int,int) -> Tuple[int,int] |
| 188 | + if microseconds: |
| 189 | + ndiv = 0 |
| 190 | + shiftr = 1000000 |
| 191 | + shiftl = 1 |
| 192 | + while (microseconds % shiftr): |
| 193 | + shiftr //= 10 |
| 194 | + shiftl *= 10 |
| 195 | + ndiv += 1 |
| 196 | + return (seconds * shiftl + microseconds // shiftr, ndiv) |
| 197 | + else: |
| 198 | + return (seconds, 0) |
| 199 | + |
| 200 | + |
| 201 | +def TimeToTicks(value, zoneinfo=localZoneInfo): |
| 202 | + # type: (Time, tzinfo) -> Tuple[int, int] |
113 | 203 | """Convert a Time object to ticks."""
|
114 |
| - timeStruct = TimeDelta(hours=value.hour, minutes=value.minute, |
115 |
| - seconds=value.second, |
116 |
| - microseconds=value.microsecond) |
117 |
| - timeDec = decimal.Decimal(str(timeStruct.total_seconds())) |
118 |
| - return (int((timeDec + time.timezone) * 10**abs(timeDec.as_tuple()[2])), |
| 204 | + epoch = Date(1970, 1, 1) |
| 205 | + tz_info = value.tzinfo |
| 206 | + if not tz_info: |
| 207 | + tz_info = zoneinfo |
| 208 | + |
| 209 | + my_time = Timestamp.combine(epoch, Time(hour=value.hour, |
| 210 | + minute=value.minute, |
| 211 | + second=value.second, |
| 212 | + microsecond=value.microsecond |
| 213 | + )) |
| 214 | + my_time = timezone_aware(my_time, tz_info) |
| 215 | + |
| 216 | + utc_time = Timestamp.combine(epoch, Time()) |
| 217 | + utc_time = timezone_aware(utc_time, TimeZone.utc) |
| 218 | + |
| 219 | + td = my_time - utc_time |
| 220 | + |
| 221 | + # fence time within a day range |
| 222 | + if td < TimeDelta(0): |
| 223 | + td = td + TimeDelta(days=1) |
| 224 | + if td > TimeDelta(days=1): |
| 225 | + td = td - TimeDelta(days=1) |
| 226 | + |
| 227 | + timeDec = decimal.Decimal(str(td.total_seconds())) |
| 228 | + return (int((timeDec) * 10**abs(timeDec.as_tuple()[2])), |
119 | 229 | abs(timeDec.as_tuple()[2]))
|
120 | 230 |
|
121 | 231 |
|
122 |
| -def TimestampToTicks(value): |
123 |
| - # type: (Timestamp) -> Tuple[int, int] |
| 232 | +def TimestampToTicks(value, zoneinfo=localZoneInfo): |
| 233 | + # type: (Timestamp,tzinfo) -> Tuple[int, int] |
124 | 234 | """Convert a Timestamp object to ticks."""
|
125 |
| - timeStruct = Timestamp(value.year, value.month, value.day, value.hour, |
126 |
| - value.minute, value.second).timetuple() |
127 |
| - try: |
128 |
| - if not value.microsecond: |
129 |
| - return (int(time.mktime(timeStruct)), 0) |
130 |
| - micro = decimal.Decimal(value.microsecond) / decimal.Decimal(1000000) |
131 |
| - t1 = decimal.Decimal(int(time.mktime(timeStruct))) + micro |
132 |
| - tlen = len(str(micro)) - 2 |
133 |
| - return (int(t1 * decimal.Decimal(int(10**tlen))), tlen) |
134 |
| - except Exception: |
135 |
| - raise DataError("Year out of range") |
| 235 | + # if naive timezone then leave date/time but change tzinfo to |
| 236 | + # be connection's timezone. |
| 237 | + if value.tzinfo is None: |
| 238 | + value = timezone_aware(value, zoneinfo) |
| 239 | + dt = value.astimezone(TimeZone.utc) |
| 240 | + timesecs = ymd2day(dt.year, dt.month, dt.day) * TICKSDAY |
| 241 | + timesecs += dt.hour * 3600 |
| 242 | + timesecs += dt.minute * 60 |
| 243 | + timesecs += dt.second |
| 244 | + packedtime = _packtime(timesecs, dt.microsecond) |
| 245 | + return packedtime |
136 | 246 |
|
137 | 247 |
|
138 | 248 | class TypeObject(object):
|
|
0 commit comments