Abstract base class for time zone info classes. Subclasses must override the tzname(), utcoffset() and dst() methods.
| 1290 | |
| 1291 | |
| 1292 | class tzinfo: |
| 1293 | """Abstract base class for time zone info classes. |
| 1294 | |
| 1295 | Subclasses must override the tzname(), utcoffset() and dst() methods. |
| 1296 | """ |
| 1297 | __slots__ = () |
| 1298 | |
| 1299 | def tzname(self, dt): |
| 1300 | "datetime -> string name of time zone." |
| 1301 | raise NotImplementedError("tzinfo subclass must override tzname()") |
| 1302 | |
| 1303 | def utcoffset(self, dt): |
| 1304 | "datetime -> timedelta, positive for east of UTC, negative for west of UTC" |
| 1305 | raise NotImplementedError("tzinfo subclass must override utcoffset()") |
| 1306 | |
| 1307 | def dst(self, dt): |
| 1308 | """datetime -> DST offset as timedelta, positive for east of UTC. |
| 1309 | |
| 1310 | Return 0 if DST not in effect. utcoffset() must include the DST |
| 1311 | offset. |
| 1312 | """ |
| 1313 | raise NotImplementedError("tzinfo subclass must override dst()") |
| 1314 | |
| 1315 | def fromutc(self, dt): |
| 1316 | "datetime in UTC -> datetime in local time." |
| 1317 | |
| 1318 | if not isinstance(dt, datetime): |
| 1319 | raise TypeError("fromutc() requires a datetime argument") |
| 1320 | if dt.tzinfo is not self: |
| 1321 | raise ValueError("dt.tzinfo is not self") |
| 1322 | |
| 1323 | dtoff = dt.utcoffset() |
| 1324 | if dtoff is None: |
| 1325 | raise ValueError("fromutc() requires a non-None utcoffset() " |
| 1326 | "result") |
| 1327 | |
| 1328 | # See the long comment block at the end of this file for an |
| 1329 | # explanation of this algorithm. |
| 1330 | dtdst = dt.dst() |
| 1331 | if dtdst is None: |
| 1332 | raise ValueError("fromutc() requires a non-None dst() result") |
| 1333 | delta = dtoff - dtdst |
| 1334 | if delta: |
| 1335 | dt += delta |
| 1336 | dtdst = dt.dst() |
| 1337 | if dtdst is None: |
| 1338 | raise ValueError("fromutc(): dt.dst gave inconsistent " |
| 1339 | "results; cannot convert") |
| 1340 | return dt + dtdst |
| 1341 | |
| 1342 | # Pickle support. |
| 1343 | |
| 1344 | def __reduce__(self): |
| 1345 | getinitargs = getattr(self, "__getinitargs__", None) |
| 1346 | if getinitargs: |
| 1347 | args = getinitargs() |
| 1348 | else: |
| 1349 | args = () |
no outgoing calls