Created
January 29, 2021 04:58
-
-
Save monpetit/00e50f667265295dc391cb5d9063f694 to your computer and use it in GitHub Desktop.
python datetime helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import dateutil.parser as dtparser | |
from pytz import timezone, utc | |
KST = timezone('Asia/Seoul') | |
def utc_to_local(dt, tz=KST): | |
try: | |
return utc.localize(dt).astimezone(tz) | |
except ValueError: | |
return dt | |
def now_utc_str(): | |
n = datetime.datetime.utcnow() | |
return utc.localize(n).isoformat() | |
def now_local_str(tz=KST): | |
n = datetime.datetime.utcnow() | |
return utc.localize(n).astimezone(tz).isoformat() | |
def now_utc_datetime(): | |
n = datetime.datetime.utcnow() | |
return utc.localize(n) | |
def now_local_datetime(tz=KST): | |
n = datetime.datetime.utcnow() | |
return utc.localize(n).astimezone(tz) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment