1.time
-
时间戳:它代表了从格林尼治时间1970年01月01日00时00分00秒(即北京时间的1970年01月01日08时00分00秒)开始到现在经过的总秒数。
-
struct_time:用一个包含9个序列的元组组成(tm_year=2024, tm_mon=1, tm_mday=26, tm_hour=2, tm_min=49, tm_sec=56, tm_wday=4, tm_yday=26, tm_isdst=-1),参数通过tuple展示
-
字符串时间:多种格式的字符串时间 2024-01-26 03:00:29;Fri Jan 26 03:00:28 2024
-
1.1.struct_time
- 时间戳转struct_time
localtime(时间戳))--》返回+8h的struct_time,时间戳为空时,默认为当前时间
gmtime(时间戳))--》返回utc的struct_time,时间戳为空时,默认返回utc的struct_time
- 字符串转struct_time
strptime('2024-01-26 11:44:45','%Y-%m-%d %H:%M:%S)--》返回字符串对应的struct_time
- struct_time([2024,01,26,11,44,45,4,26,0]) --》自身赋值
1 #!/usr/bin/python 2 #常用模块学习 3 import time ,datetime 4 # print(dir(time)) 5 #structime 6 print(time.localtime())#将时间戳转为struct_time格式 +8小时 7 print(time.gmtime(1706234296))#将时间戳转为struct_time格式的UTC时间 8 print(time.strptime('2024-01-26 02:49:56','%Y-%m-%d %H:%M:%S'))#字符串转为指定格式struct_time 9 print(time.struct_time([2024,1,26,14,54,34,5,26,0]))#给struct_time赋值View Code
1.2.时间戳
- time()--》获取当前时间戳
- mktime(stuple)--》将struct_time转为时间戳
1 #!/usr/bin/python 2 #常用模块学习 3 import time ,datetime 4 print(time.mktime(time.gmtime(1706234296)))#将struct_time转为时间戳 5 print(time.time())#时间戳View Code
1.3.字符串
- strftime('%Y-%m-%s %H:%M:%S','时间戳')
- ctime(时间戳)
- asctime(tuple)
1 #!/usr/bin/python 2 #常用模块学习 3 import time ,datetime 4 #字符串 5 print(time.asctime(time.gmtime()))#将struct_time转为字符串e.g. 'Sat Jun 06 16:26:11 1998' 6 print(time.ctime(1706234296))#将时间戳转为字符串e.g. 'Sat Jun 06 16:26:11 1998' 7 print(time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime()))#struct_time转为指定格式字符串View Code
1.4.sleep
def sleep(seconds): # real signature unknown; restored from __doc__
"""
sleep(seconds)
Delay execution for a given number of seconds. The argument may be
a floating point number for subsecond precision.
"""
pass
翻译:延迟给定的秒数执行,参数可以时浮点数
1 #!/usr/bin/python 2 #常用模块学习 3 import time ,datetime 4 print(time.sleep(0.40))#延迟执行参数View Code
1.5.process_time
def process_time(): # real signature unknown; restored from __doc__
"""
process_time() -> float
Process time for profiling: sum of the kernel and user-space CPU time.
"""
return 0.0
翻译:分析进程处理时间:包含内核和cpu
1 #!/usr/bin/python 2 #常用模块学习 3 import time ,datetime 4 print(time.process_time())#用于分析进程时间浮点数View Code
1.6.thread_time
def thread_time(): # real signature unknown; restored from __doc__
"""
thread_time() -> float
Thread time for profiling: sum of the kernel and user-space CPU time.
"""
return 0.0
翻译:分析线程处理时间:包含内核和cpu
2.datetime
2.1 datetime.datetime.now()
def now(cls, tz=None):
"Construct a datetime from time.time() and optional time zone info."
t = _time.time()
return cls.fromtimestamp(t, tz)
翻译:通过时间戳time.time()和一个可选时间区,构建一个日期;打印当前时间
2.2 datetime.date.fromtimestamp(时间戳)
def fromtimestamp(cls, t):
"Construct a date from a POSIX timestamp (like time.time())."
y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t)
return cls(y, m, d)
翻译:通过一个时间戳构建一个日期;打印当前日期 2024-01-26
2.3 datetime.datetime.fromtimestamp(时间戳)
def fromtimestamp(cls, t, tz=None):
"""Construct a datetime from a POSIX timestamp (like time.time()).
A timezone info object may be passed in as well.
"""
_check_tzinfo_arg(tz)
return cls._fromtimestamp(t, tz is not None, tz)
翻译:通过一个时间戳构建一个日期时间,打印日期时间如2024-01-26 11:26:32
2.4 datetime.datetime.now()+datetime.timedelta()
if sys.version_info >= (3, 6):
def __init__(self, days: float = ..., seconds: float = ..., microseconds: float = ...,
milliseconds: float = ..., minutes: float = ..., hours: float = ...,
weeks: float = ..., *, fold: int = ...) -> None: ...
翻译:就是在now基础上加减给定参数值(正数为加,负数为减)
1 #!/usr/bin/python 2 #常用模块学习 3 import time ,datetime 4 print(datetime.datetime.now()) 5 print(datetime.date.fromtimestamp(1706234296)) #时间戳转日期 6 print(datetime.datetime.fromtimestamp(1706234296))#时间戳转日期时间格式 7 print(datetime.datetime.now()+datetime.timedelta(5))#当前时间加5天 8 print(datetime.datetime.now()+datetime.timedelta(-5))#当前时间减5天 9 print(datetime.datetime.now()+datetime.timedelta(days=-5,hours=3,minutes=30))#当前时间加3小时30分钟View Code
标签:常用,struct,python,26,datetime,标准,时间,time,print From: https://www.cnblogs.com/Little-Girl/p/17989513