-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.py
More file actions
28 lines (18 loc) · 884 Bytes
/
Copy pathdatetime.py
File metadata and controls
28 lines (18 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from zoneinfo import ZoneInfo
from datetime import datetime
class DatetimeUtils:
@staticmethod
def now(strftime='%Y-%m-%dT%H:%M:%S', zone_info='America/Sao_Paulo') -> str:
dt_utc = datetime.now(ZoneInfo('UTC'))
return dt_utc.astimezone(ZoneInfo(zone_info)).strftime(strftime)
@staticmethod
def parse_by_zone(dt_utc, strftime='%Y-%m-%dT%H:%M:%S', zone_info='America/Sao_Paulo') -> str:
if dt_utc.tzinfo is None:
dt_utc = dt_utc.replace(tzinfo=ZoneInfo('UTC'))
return dt_utc.astimezone(ZoneInfo(zone_info)).strftime(strftime)
def parse(dt_utc, strftime) -> str:
if dt_utc.tzinfo is None:
dt_utc = dt_utc.replace(tzinfo=ZoneInfo('UTC'))
if strftime is None:
strftime = '%Y-%m-%dT%H:%M:%S'
return dt_utc.strftime(strftime)