-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.py
More file actions
69 lines (46 loc) · 1.52 KB
/
time.py
File metadata and controls
69 lines (46 loc) · 1.52 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pytz
from typing import Dict
from datetime import datetime, timezone
def datetime_str(value: datetime) -> str:
if value is not None:
return value.isoformat()
else:
return None
def current_datetime() -> datetime:
return datetime.now(pytz.UTC)
def current_datetime_str() -> str:
return datetime_str(current_datetime())
def get_current_year() -> int:
return datetime.now(pytz.UTC).year
def get_current_month() -> int:
return datetime.now(pytz.UTC).month
def get_current_day() -> int:
return datetime.now(pytz.UTC).day
def get_date_range_of_month(year: int, month: int):
def get_last_day_of_month(year: int, month: int) -> int:
from calendar import monthrange
return monthrange(year=year, month=month)[1]
start_date = datetime(year=year, month=month, day=1, tzinfo=timezone.utc)
end_date = datetime(
year=year,
month=month,
day=get_last_day_of_month(year, month),
hour=23,
minute=59,
second=59,
microsecond=999999,
tzinfo=timezone.utc,
)
return start_date, end_date
def str_to_datetime(value: str) -> datetime:
def to_utc(date: datetime) -> datetime:
from pytz import timezone
_tz = timezone('UTC')
localized = _tz.localize(date)
return localized
from dateutil import parser
no_timezone_info = parser.parse(value).tzinfo is None
if no_timezone_info:
return to_utc(parser.parse(value))
else:
return parser.parse(value)