Python时间处理函数一览
Python中有很多用于处理时间和日期的函数和模块。下面是一些常用的时间处理函数:
1. time.time(): 返回当前时间的时间戳,即从1970年1月1日午夜(UTC/GMT的午夜)开始所经过的秒数。
2. time.localtime([secs]): 返回表示当前时间的时间元组。如果给定了参数secs,则返回指定时间的时间元组。
3. time.gmtime([secs]): 类似于time.localtime(),但返回表示UTC(协调世界时)的时间。
4. time.ctime([secs]): 返回表示当前时间的字符串,格式为"Tue Jun 22 13:41:00 2021"。
5. time.strftime(format[, t]): 根据指定的格式化字符串,返回表示时间t的字符串。
6. time.strptime(string[, format]): 解析由字符串表示的时间,并返回时间元组。
7. datetime.datetime.now([tz]): 返回当前本地时间的datetime对象。
8. datetime.datetime.utcnow(): 返回当前UTC时间的datetime对象。
9. datetime.datetime.strptime(date_string, format): 根据指定的格式化字符串,解析由字符串表示的时间,并返回datetime对象。
10. datetime.datetime.strftime(format): 格式化datetime对象为字符串。
11. datetime.timedelta(days=-1, seconds=-1, microseconds=-1, milliseconds=-1, minutes=-1, hours=-1, weeks=-1): 表示时间间隔的对象,可以用于计算时间的差值。
12. calendar.calendar(year, w=2, c=6, m=3): 返回一个多行字符串格式的年历。
13. calendar.month(year, month, w=2, l=1): 返回一个多行字符串格式的月历。
14. calendar.isleap(year): 判断给定的年份是否是闰年。
15. calendar.weekday(year, month, day): 返回指定日期的星期几(0为星期一,6为星期日)。
16. pytz.timezone(name): 创建一个时区对象,用于处理特定的时区。
17. timedelta.total_seconds(): 返回timedelta对象表示的时间总秒数。
18. timeit.timeit(stmt='pass', setup='pass', timer=<timer>, number=1000000): 测试语句的执行时间。
以上只是一些常用的时间处理函数和模块,Python中还有很多其他的时间处理工具和库,根据具体的需求选择适合的方法来处理时间和日期。
