Skip to content

Commit d76810a

Browse files
committed
timezone and utc
1 parent f098031 commit d76810a

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

basic/decorator_sp/arg_parse_deco.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import functools
21
import base64
2+
import functools
33
import inspect
44

55

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import time
2+
import datetime
3+
4+
5+
def main():
6+
print(time.timezone)
7+
print(time.tzname)
8+
print(time.localtime())
9+
print(time.localtime().tm_isdst)
10+
print(time.altzone)
11+
utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone
12+
utc_offset = datetime.timedelta(seconds=-utc_offset_sec)
13+
print(datetime.timezone(offset=utc_offset))
14+
15+
16+
if __name__ == '__main__':
17+
main()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import time
2+
import datetime
3+
4+
5+
def main():
6+
local_now = datetime.datetime.now()
7+
print(local_now)
8+
print(local_now.isoformat())
9+
10+
utc_now = datetime.datetime.utcnow()
11+
print(utc_now)
12+
print(utc_now.isoformat())
13+
14+
# calculate the offset taking into account daylight saving time
15+
utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone
16+
utc_offset = datetime.timedelta(seconds=-utc_offset_sec)
17+
timezone = datetime.timezone(offset=utc_offset)
18+
print(timezone)
19+
20+
local_now_with_tz = datetime.datetime.now().replace(tzinfo=timezone)
21+
print(local_now_with_tz)
22+
print(local_now_with_tz.isoformat())
23+
24+
utc_now_with_tz = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
25+
print(utc_now_with_tz)
26+
print(utc_now_with_tz.isoformat())
27+
28+
29+
if __name__ == '__main__':
30+
main()
31+
32+
33+
# https://stackoverflow.com/a/28147286/3936457

0 commit comments

Comments
 (0)