Python 如何检查字符串是否为UTC 时间戳?
Python How to check the string is a UTC timestamp?
我需要检查时间戳字符串是否属于'UTC'时间戳
我有以下代码
def time_stamp():
utc = timezone('UTC')
time_stamp = datetime.now(utc)
utc_time_stmap = time_stamp.strftime("%Y-%m-%dT%H:%M:%S.%f")
return utc_time_stmap
上述函数return字符串格式的utc时间
print(type(time_stamp()))
<class 'str'>
print(time_stamp())
'2021-02-10 15:49:57.906168'
'time_stamp()' return 'string type' 中的时间戳。
#预期:
我需要检查 return 值是否在紧迫日期 UTC 日期范围内?
感谢您的帮助?
谢谢
这里是一些代码,您可以在其中找到答案。
import re
from datetime import datetime
DATETIME_ISO8601 = re.compile(
r'^([0-9]{4})' r'-' r'([0-9]{1,2})' r'-' r'([0-9]{1,2})' # date
r'([T\s][0-9]{1,2}:[0-9]{1,2}:?[0-9]{1,2}(\.[0-9]{1,6})?)?' # time
r'((\+[0-9]{2}:[0-9]{2})| UTC| utc)?' # zone
)
def datetime_iso(string):
""" verify rule
Mandatory is: 'yyyy-(m)m-(d)dT(h)h:(m)m'
"""
string = string.strip()
return bool(re.fullmatch(DATETIME_ISO8601, string))
def utc_timezone(datetime_string):
datetime_string = datetime_string.strip()
return datetime_string.endswith("00:00") or datetime_string.upper().endswith("UTC")
check_this = ["2020-1-1 22:11", "2020-1-1 22:11:34 00:00", "2020-1-1 22:11:34+00:00", "2020-1-1 22:11+01:00", "2020-1-1 22:11 UTC", "does this help?"]
for datetime_string in check_this:
print(".........................")
print(f"This date time string '{datetime_string}' is in datetime iso format: {datetime_iso(datetime_string)}")
if datetime_iso(datetime_string):
print(f"Is it the UTC time zone? {utc_timezone(datetime_string)}")
我需要检查时间戳字符串是否属于'UTC'时间戳
我有以下代码
def time_stamp():
utc = timezone('UTC')
time_stamp = datetime.now(utc)
utc_time_stmap = time_stamp.strftime("%Y-%m-%dT%H:%M:%S.%f")
return utc_time_stmap
上述函数return字符串格式的utc时间
print(type(time_stamp()))
<class 'str'>
print(time_stamp())
'2021-02-10 15:49:57.906168'
'time_stamp()' return 'string type' 中的时间戳。
#预期:
我需要检查 return 值是否在紧迫日期 UTC 日期范围内?
感谢您的帮助?
谢谢
这里是一些代码,您可以在其中找到答案。
import re
from datetime import datetime
DATETIME_ISO8601 = re.compile(
r'^([0-9]{4})' r'-' r'([0-9]{1,2})' r'-' r'([0-9]{1,2})' # date
r'([T\s][0-9]{1,2}:[0-9]{1,2}:?[0-9]{1,2}(\.[0-9]{1,6})?)?' # time
r'((\+[0-9]{2}:[0-9]{2})| UTC| utc)?' # zone
)
def datetime_iso(string):
""" verify rule
Mandatory is: 'yyyy-(m)m-(d)dT(h)h:(m)m'
"""
string = string.strip()
return bool(re.fullmatch(DATETIME_ISO8601, string))
def utc_timezone(datetime_string):
datetime_string = datetime_string.strip()
return datetime_string.endswith("00:00") or datetime_string.upper().endswith("UTC")
check_this = ["2020-1-1 22:11", "2020-1-1 22:11:34 00:00", "2020-1-1 22:11:34+00:00", "2020-1-1 22:11+01:00", "2020-1-1 22:11 UTC", "does this help?"]
for datetime_string in check_this:
print(".........................")
print(f"This date time string '{datetime_string}' is in datetime iso format: {datetime_iso(datetime_string)}")
if datetime_iso(datetime_string):
print(f"Is it the UTC time zone? {utc_timezone(datetime_string)}")