# 返回 utc ms
def get_current_time_in_beijing():
# 获取当前时间并转换为北京时间
beijing_tz = pytz.timezone('Asia/Shanghai')
current_time = datetime.datetime.now(beijing_tz)
return current_time
def get_utc_milliseconds_today():
now = get_current_time_in_beijing()
start_of_day = datetime.datetime(now.year, now.month, now.day)
start_utc_milliseconds = int(time.mktime(start_of_day.timetuple()) * 1000)
end_of_day = start_of_day + datetime.timedelta(days=1) - datetime.timedelta(microseconds=1)
end_utc_milliseconds = int(time.mktime(end_of_day.timetuple()) * 1000)
return start_utc_milliseconds, end_utc_milliseconds
def get_week_start_end():
now = get_current_time_in_beijing()
start_of_week = now - datetime.timedelta(days=now.weekday()) # 本周的第一天(周一)
end_of_week = start_of_week + datetime.timedelta(days=6)
# 设置时分秒
start_of_week = start_of_week.replace(hour=00, minute=00, second=00)
end_of_week = end_of_week.replace(hour=23, minute=59, second=59)
start_utc_ms = int(time.mktime(start_of_week.timetuple()) * 1000)
end_utc_ms = int(time.mktime(end_of_week.timetuple()) * 1000)
return start_utc_ms, end_utc_ms
def get_utc_milliseconds_month():
now = get_current_time_in_beijing()
first_day = datetime.datetime(now.year, now.month, 1)
first_day_utc_milliseconds = int(time.mktime(first_day.timetuple()) * 1000)
if now.month == 12:
last_day = datetime.datetime(now.year + 1, 1, 1) - datetime.timedelta(seconds=1)
else:
last_day = datetime.datetime(now.year, now.month + 1, 1) - datetime.timedelta(seconds=1)
last_day_utc_milliseconds = int(time.mktime(last_day.timetuple()) * 1000)
return first_day_utc_milliseconds, last_day_utc_milliseconds
if __name__ == '__main__':
st, et = get_week_start_end()
print 'week:', st, et
print pubfun.ms_to_str(st), pubfun.ms_to_str(et)
st, et = get_utc_milliseconds_today()
print 'today:', st, et
print pubfun.ms_to_str(st), pubfun.ms_to_str(et)
st, et = get_utc_milliseconds_month()
print 'month:', st, et
print pubfun.ms_to_str(st), pubfun.ms_to_str(et)