Skip to content

Instantly share code, notes, and snippets.

@monpetit
Created January 29, 2021 04:58
Show Gist options
  • Save monpetit/00e50f667265295dc391cb5d9063f694 to your computer and use it in GitHub Desktop.
Save monpetit/00e50f667265295dc391cb5d9063f694 to your computer and use it in GitHub Desktop.
python datetime helper
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