| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 | 
							- # update: 2021-10-21
 
- """
 
- %a 星期的简写。如 星期三为Web
 
- %A 星期的全写。如 星期三为Wednesday
 
- %b 月份的简写。如4月份为Apr
 
- %B月份的全写。如4月份为April
 
- %c: 日期时间的字符串表示。(如: 04/07/10 10:43:39)
 
- %d: 日在这个月中的天数(是这个月的第几天)
 
- %f: 微秒(范围[0,999999])
 
- %H: 小时(24小时制,[0, 23])
 
- %I: 小时(12小时制,[0, 11])
 
- %j: 日在年中的天数 [001,366](是当年的第几天)
 
- %m: 月份([01,12])
 
- %M: 分钟([00,59])
 
- %p: AM或者PM
 
- %S: 秒(范围为[00,61],为什么不是[00, 59],参考python手册~_~)
 
- %U: 周在当年的周数当年的第几周),星期天作为周的第一天
 
- %w: 今天在这周的天数,范围为[0, 6],6表示星期天
 
- %W: 周在当年的周数(是当年的第几周),星期一作为周的第一天
 
- %x: 日期字符串(如:04/07/10)
 
- %X: 时间字符串(如:10:43:39)
 
- %y: 2个数字表示的年份
 
- %Y: 4个数字表示的年份
 
- %z: 与utc时间的间隔 (如果是本地时间,返回空字符串)
 
- %Z: 时区名称(如果是本地时间,返回空字符串)
 
- """
 
- import datetime
 
- import time
 
- UTC_PATTERN = '%Y-%m-%dT%H:%M:%S.%fZ'
 
- LOCAL_PATTERN = '%Y-%m-%d %H:%M:%S'
 
- def string_to_dt(string, pattern='%Y-%m-%d'):
 
-     """
 
-     字符串转为日期
 
-     """
 
-     return datetime.datetime.strptime(string, pattern)
 
- def dt_to_string(date, pattern='%Y-%m-%d %H:%M:%S'):
 
-     """
 
-     日期转为字符串
 
-     """
 
-     return date.strftime(pattern)
 
- def string_to_ts(string, pattern='%Y-%m-%dT%H:%M:%S.%f'):
 
-     """
 
-     日期转为时间戳
 
-     Example:
 
-         string_to_ts(2015-01-01)
 
-         1420041600
 
-     """
 
-     return int(time.mktime(time.strptime(string, pattern)))
 
- def ts_to_string(int_time, pattern='%Y-%m-%dT%H:%M:%S.%f'):
 
-     """
 
-     时间戳->字符串
 
-     """
 
-     return time.strftime(pattern, time.localtime(int_time))
 
- def now_string(pattern='%Y-%m-%d %H:%M:%S'):
 
-     """
 
-     获取当前时间戳
 
-     """
 
-     return datetime.datetime.now().strftime(pattern)
 
- def iso_string_to_ts(string):
 
-     """
 
-     iso标准时间转时间戳
 
-     """
 
-     return time.mktime(time.strptime(string, '%Y-%m-%dT%H:%M:%S.%f'))
 
- def now_iso_string():
 
-     """
 
-     iso标准时间
 
-     """
 
-     return datetime.datetime.now().isoformat('T')
 
- def now_utc_iso_string():
 
-     """
 
-     iso标准时间
 
-     """
 
-     return datetime.datetime.utcnow().isoformat('T')
 
- def now_utc_dt():
 
-     """
 
-     获取当前utc时间
 
-     """
 
-     return datetime.datetime.utcnow()
 
- def now_dt():
 
-     """
 
-     获取当前时间
 
-     Example:
 
-         now_dt()
 
-         datetime.datetime(2020, 5, 16, 6, 15, 7, 39060)
 
-     """
 
-     return datetime.datetime.now()
 
- def now_ts(unit='s', ndigits=0):
 
-     """
 
-     获取当前时间戳
 
-     unit: 单位 (s: 秒 ms: 毫秒)
 
-     ndigits: 小数点位数
 
-     """
 
-     unit_dict = {
 
-         'ms': 1,
 
-         's': 0,
 
-     }
 
-     ts = time.time()
 
-     if unit_dict.get(unit):
 
-         ts = ts * (1000 ** unit_dict.get(unit))
 
-     if ndigits:
 
-         return round(ts, ndigits)
 
-     return int(ts)
 
- def today_ts():
 
-     """
 
-     获取当日时间戳
 
-     """
 
-     today_dt = datetime.date.today()
 
-     return int(time.mktime(today_dt.timetuple()))
 
- def utc_to_beijing(utc_string, pattern='%Y-%m-%dT%H:%M:%S.%fZ'):
 
-     """UTC时间转北京时间(+8:00)"""
 
-     utc_ts = string_to_ts(utc_string, pattern=pattern)
 
-     beijing_ts = utc_ts + (3600 * 8)
 
-     return ts_to_string(beijing_ts, pattern='%Y-%m-%d %H:%M:%S')
 
- def beijing_to_utc(beijing_string, pattern='%Y-%m-%d %H:%M:%S'):
 
-     """本地时间转UTC时间(-8:00)"""
 
-     beijing_ts = string_to_ts(beijing_string, pattern=pattern)
 
-     utc_ts = beijing_ts - (3600 * 8)
 
-     return ts_to_string(utc_ts, pattern='%Y-%m-%dT%H:%M:%S.%fZ')
 
- if __name__ == '__main__':
 
-     # t = '2020-08-02T01:00:00.000Z'
 
-     # print(utc_to_beijing(t))
 
-     # t = "2020-08-02T16:00:00.000Z"
 
-     # d = string_to_dt(t, "%Y-%m-%dT%H:%M:%S.%fZ")
 
-     # print(d)
 
-     # print(datetime.datetime.now() + datetime.timedelta(minutes=-1)).strftime("%Y-%m-%d %H:%M:%S")
 
-     # print(today_ts())
 
-     # print(string_to_ts('2021-03-25-09-53-39', '%Y-%m-%d-%H-%M-%S'))
 
-     # print(now_ts(unit='ms'))
 
-     # print(now_ts(unit='s'))
 
-     print(now_ts())
 
-     # out = string_to_ts('2021-07-16', '%Y-%m-%d')
 
-     # print(out)
 
-     # print(string_to_ts('2022-06-01 22:00:00', pattern='%Y-%m-%d %H:%M:%S'))  # 1654092000
 
 
  |