-
Notifications
You must be signed in to change notification settings - Fork 1
/
datetimehelper.py
62 lines (47 loc) · 1.67 KB
/
datetimehelper.py
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
from datetime import datetime, timedelta
import pytz
import sys
from os import environ
utc = pytz.utc
try:
localtz = pytz.timezone(environ["TIMEZONE"])
except pytz.UnknownTimeZoneError:
print("Unknown timezone specified, using UTC instead.", file=sys.stderr)
localtz = pytz.utc
get_now = lambda :datetime.now(utc)
def convertToDateTime(x: str) -> datetime:
"""
convertToDateTime converts a string that is in UTC time to a datetime object.
This function is used to convert the string returned by the GitHub GraphQL API to a datetime object.
args:
x: str - the string to be converted
returns:
datetime - the converted datetime object
"""
dt = datetime.strptime(x, "%Y-%m-%dT%H:%M:%SZ")
return dt.replace(tzinfo=utc)
def format_local(dt: datetime) -> str:
"""
format_local formats a datetime object to a string in the local timezone.
args:
dt: datetime - the datetime object to be formatted
"""
return dt.astimezone(localtz).strftime("%Y-%m-%d %H:%M:%S")
def format_to_utc(dt: datetime) -> str:
"""
format_to_utc formats a datetime object to a string in the UTC timezone.
args:
dt: datetime - the datetime object to be formatted
returns:
str - the formatted string
"""
return dt.astimezone(utc).strftime("%Y-%m-%dT%H:%M:%SZ")
def get_n_day_prior(n: int) -> datetime:
"""
get_n_day_prior returns the datetime object n days prior to the current time in UTC time.
args:
n: int - the number of days prior
returns:
datetime - the datetime object n days prior to the current time in UTC time
"""
return datetime.now(utc) - timedelta(days=n)