feat: Added get_datetime_in_timezone in frappe.utils to get datetime in specific timezones

* Added util in safe_exec to access via Server Scripts and System
Console
This commit is contained in:
Gavin D'souza 2021-01-21 13:12:42 +05:30
parent 9591d01c2c
commit 33ea496a8b
2 changed files with 11 additions and 2 deletions

View file

@ -154,14 +154,22 @@ def get_time_zone():
return frappe.cache().get_value("time_zone", _get_time_zone)
def convert_utc_to_user_timezone(utc_timestamp):
def convert_utc_to_timezone(utc_timestamp, time_zone):
from pytz import timezone, UnknownTimeZoneError
utcnow = timezone('UTC').localize(utc_timestamp)
try:
return utcnow.astimezone(timezone(get_time_zone()))
return utcnow.astimezone(timezone(time_zone))
except UnknownTimeZoneError:
return utcnow
def get_datetime_in_timezone(time_zone):
utc_timestamp = datetime.datetime.utcnow()
return convert_utc_to_timezone(utc_timestamp, time_zone)
def convert_utc_to_user_timezone(utc_timestamp):
time_zone = get_time_zone()
return convert_utc_to_timezone(utc_timestamp, time_zone)
def now():
"""return current datetime as yyyy-mm-dd hh:mm:ss"""
if frappe.flags.current_date:

View file

@ -222,6 +222,7 @@ VALID_UTILS = (
"get_last_day_of_week",
"get_last_day",
"get_time",
"get_datetime_in_timezone",
"get_datetime_str",
"get_date_str",
"get_time_str",