Return local time as an aware datetime object. If called without arguments, return current time. Otherwise *dt* argument should be a datetime instance, and it is converted to the local time zone according to the system time zone database. If *dt* is naive (that is, dt.tzinfo
(dt=None, isdst=-1)
| 332 | # |
| 333 | |
| 334 | def localtime(dt=None, isdst=-1): |
| 335 | """Return local time as an aware datetime object. |
| 336 | |
| 337 | If called without arguments, return current time. Otherwise *dt* |
| 338 | argument should be a datetime instance, and it is converted to the |
| 339 | local time zone according to the system time zone database. If *dt* is |
| 340 | naive (that is, dt.tzinfo is None), it is assumed to be in local time. |
| 341 | In this case, a positive or zero value for *isdst* causes localtime to |
| 342 | presume initially that summer time (for example, Daylight Saving Time) |
| 343 | is or is not (respectively) in effect for the specified time. A |
| 344 | negative value for *isdst* causes the localtime() function to attempt |
| 345 | to divine whether summer time is in effect for the specified time. |
| 346 | |
| 347 | """ |
| 348 | if dt is None: |
| 349 | return datetime.datetime.now(datetime.timezone.utc).astimezone() |
| 350 | if dt.tzinfo is not None: |
| 351 | return dt.astimezone() |
| 352 | # We have a naive datetime. Convert to a (localtime) timetuple and pass to |
| 353 | # system mktime together with the isdst hint. System mktime will return |
| 354 | # seconds since epoch. |
| 355 | tm = dt.timetuple()[:-1] + (isdst,) |
| 356 | seconds = time.mktime(tm) |
| 357 | localtm = time.localtime(seconds) |
| 358 | try: |
| 359 | delta = datetime.timedelta(seconds=localtm.tm_gmtoff) |
| 360 | tz = datetime.timezone(delta, localtm.tm_zone) |
| 361 | except AttributeError: |
| 362 | # Compute UTC offset and compare with the value implied by tm_isdst. |
| 363 | # If the values match, use the zone name implied by tm_isdst. |
| 364 | delta = dt - datetime.datetime(*time.gmtime(seconds)[:6]) |
| 365 | dst = time.daylight and localtm.tm_isdst > 0 |
| 366 | gmtoff = -(time.altzone if dst else time.timezone) |
| 367 | if delta == datetime.timedelta(seconds=gmtoff): |
| 368 | tz = datetime.timezone(delta, time.tzname[dst]) |
| 369 | else: |
| 370 | tz = datetime.timezone(delta) |
| 371 | return dt.replace(tzinfo=tz) |
nothing calls this directly
no test coverage detected