Skip to content

Commit b40f0e8

Browse files
committed
updates during writing
1 parent ba8a605 commit b40f0e8

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

python-get-current-time/unix_time.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,34 @@
22
import time
33

44
# Should prefer datetime over time
5+
LOCAL_TIMEZONE = (
6+
datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
7+
)
58

69
datetime_unixtime = datetime.datetime.now().timestamp()
710
time_unixtime = time.time()
11+
timezone_aware_unixtime = datetime.datetime.now(tz=LOCAL_TIMEZONE).timestamp()
12+
13+
print(
14+
f"""
15+
{datetime_unixtime = }
16+
{time_unixtime = }
17+
{timezone_aware_unixtime = }
18+
"""
19+
)
20+
21+
import datetime
22+
23+
LOCAL_TIMEZONE = (
24+
datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
25+
)
26+
27+
timezone_naive_unixtime = datetime.datetime.now().timestamp()
28+
timezone_aware_unixtime = datetime.datetime.now(tz=LOCAL_TIMEZONE).timestamp()
829

930
print(
1031
f"""
11-
{datetime_unixtime=}
12-
{time_unixtime=}
32+
{timezone_naive_unixtime = }
33+
{timezone_aware_unixtime = }
1334
"""
1435
)

python-get-current-time/with_datetime.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434

3535
print(
3636
f"""
37-
{now=}
38-
{now.isoformat()=}
37+
UTC timezone aware:
38+
{now = }
39+
{now.isoformat() = }
3940
"""
4041
)
4142

@@ -48,8 +49,9 @@
4849

4950
print(
5051
f"""
51-
{now=}
52-
{now.isoformat()=}
52+
Local timezone aware:
53+
{now = }
54+
{now.isoformat() = }
5355
"""
5456
)
5557

0 commit comments

Comments
 (0)