diff --git a/dtable_events/automations/actions.py b/dtable_events/automations/actions.py index ff6d6bdb..25f07bdc 100644 --- a/dtable_events/automations/actions.py +++ b/dtable_events/automations/actions.py @@ -18,13 +18,13 @@ from dtable_events.app.metadata_cache_managers import BaseMetadataCacheManager from dtable_events.app.event_redis import redis_cache from dtable_events.app.config import DTABLE_WEB_SERVICE_URL, DTABLE_PRIVATE_KEY, \ - SEATABLE_FAAS_AUTH_TOKEN, SEATABLE_FAAS_URL, INNER_DTABLE_DB_URL + SEATABLE_FAAS_AUTH_TOKEN, SEATABLE_FAAS_URL, INNER_DTABLE_DB_URL, TIME_ZONE from dtable_events.dtable_io import send_wechat_msg, send_email_msg, send_dingtalk_msg, batch_send_email_msg from dtable_events.page_design.manager import conver_page_to_pdf_manager from dtable_events.notification_rules.notification_rules_utils import fill_msg_blanks_with_converted_row, \ send_notification, fill_msg_blanks_with_sql_row from dtable_events.utils import uuid_str_to_36_chars, is_valid_email, get_inner_dtable_server_url, \ - normalize_file_path, gen_file_get_url, gen_random_option + normalize_file_path, gen_file_get_url, gen_random_option, current_now_in_tz from dtable_events.utils.constants import ColumnTypes from dtable_events.utils.dtable_server_api import DTableServerAPI from dtable_events.utils.dtable_web_api import DTableWebAPI @@ -3182,7 +3182,10 @@ def can_do_actions(self): return True elif self.run_condition in CRON_CONDITIONS: - cur_datetime = datetime.now() + tz_str = self.trigger.get('time_zone', None) + if not tz_str: + tz_str = TIME_ZONE + cur_datetime = current_now_in_tz(tz_str) cur_hour = cur_datetime.hour cur_week_day = cur_datetime.isoweekday() cur_month_day = cur_datetime.day diff --git a/dtable_events/notification_rules/notification_rules_utils.py b/dtable_events/notification_rules/notification_rules_utils.py index 310f3e41..b7cd7839 100644 --- a/dtable_events/notification_rules/notification_rules_utils.py +++ b/dtable_events/notification_rules/notification_rules_utils.py @@ -9,10 +9,10 @@ import requests from dtable_events import filter2sql -from dtable_events.app.config import DTABLE_PRIVATE_KEY, DTABLE_WEB_SERVICE_URL, INNER_DTABLE_DB_URL +from dtable_events.app.config import DTABLE_PRIVATE_KEY, DTABLE_WEB_SERVICE_URL, INNER_DTABLE_DB_URL, TIME_ZONE from dtable_events.app.metadata_cache_managers import RuleIntentMetadataCacheManger, RuleIntervalMetadataCacheManager from dtable_events.notification_rules.utils import get_nickname_by_usernames -from dtable_events.utils import is_valid_email, uuid_str_to_36_chars, get_inner_dtable_server_url +from dtable_events.utils import is_valid_email, uuid_str_to_36_chars, get_inner_dtable_server_url, current_now_in_tz from dtable_events.utils.constants import ColumnTypes, FormulaResultType from dtable_events.utils.dtable_server_api import DTableServerAPI from dtable_events.utils.dtable_web_api import DTableWebAPI @@ -553,8 +553,10 @@ def trigger_near_deadline_notification_rule(rule, db_session, rule_interval_meta table_id = trigger['table_id'] view_id = trigger['view_id'] notify_hour = trigger.get('notify_hour') - cur_datetime = datetime.now() - + tz_str = trigger.get('time_zone', None) + if not tz_str: + tz_str = TIME_ZONE + cur_datetime = current_now_in_tz(tz_str) cur_hour = int(cur_datetime.hour) diff --git a/dtable_events/utils/__init__.py b/dtable_events/utils/__init__.py index 747f1865..dc716658 100644 --- a/dtable_events/utils/__init__.py +++ b/dtable_events/utils/__init__.py @@ -6,6 +6,7 @@ import subprocess import uuid +import datetime import pytz import re @@ -262,3 +263,14 @@ def gen_inner_file_upload_url(token, op, replace=False): if replace is True: url += '?replace=1' return url + + +def current_now_in_tz(tz_str): + utc_now = datetime.datetime.utcnow() + utc_now = pytz.utc.localize(utc_now) + try: + current_now = utc_now.astimezone(pytz.timezone(tz_str)) + except Exception as e: + logger.error("transfer current now to timezone %s error: %s" % (tz_str, e)) + current_now = datetime.datetime.now() + return current_now