本文实例讲述了python基础学习之时间转换函数用法。分享给大家供大家参考,具体如下:
前言
python的时间格式分为多种,几种格式之间的转换方法时常是我们遇到的而且是经常忘记的点,python不像php,时间字符串和datetime是一起的,只需要strtotime和date函数就可以相互转化。虽然网上已经有很多python时间转换的文章,但是由于作者本人经常做海外业务,需要各种时区之间的转换,所以这篇文章会对按时区转换各种时间格式做一个总结。
转换方法图示(图片转自网络):
一、字符串转时间戳
1、默认:
1
2
3
|
import time def time_str_to_timestamp(string_time, _format = "%y-%m-%d %h:%m:%s" ): return int (time.mktime(time.strptime(string_time, _format))) |
2、按时区转:
1
2
3
4
5
6
7
8
9
|
import time import datetime from pytz import timezone as tz def time_str_to_timestamp_by_timezone(string_time, _format = "%y-%m-%d %h:%m:%s”, from_tz=“utc”, to_tz=" america / los_angeles"): from_tz = tz(from_tz) to_tz = tz(to_tz) return int (time.mktime( datetime.datetime.strptime(string_time, _format).replace( tzinfo = from_tz).astimezone(to_tz).timetuple())) |
二、时间戳转字符串
1、默认:
1
2
3
|
import time def timestamp_to_str(timestamp, _format = "%y-%m-%d %h:%m:%s" ): return time.strftime(_format, time.localtime(timestamp)) |
2、按时区转:
1
2
3
4
5
|
import datetime from pytz import timezone as tz def timestamp_to_str_by_timezone(timestamp, _format = "%y-%m-%d %h:%m:%s”, to_tz=" america / los_angeles"): to_tz = tz(to_tz) return str (datetime.datetime.fromtimestamp(timestamp, to_tz).strftime(_format)) |
三、字符串转datetime
1、默认:
1
2
3
|
import datetime def datetime_str_to_datetime(string_time, _format = "%y-%m-%d %h:%m:%s" ): return datetime.datetime.strptime(string_time, _format) |
2、按时区转:
1
2
3
4
5
6
7
|
import datetime from pytz import timezone as tz def datetime_str_to_datetime_by_timezone(string_time, from_tz = “utc”, to_tz = "america/los_angeles”, _format=" % y - % m - % d % h: % m: % s",): from_tz = tz(from_tz) to_tz = tz(to_tz) return datetime.datetime.strptime(string_time, _format).replace( tzinfo = from_tz).astimezone(to_tz) |
四、datetime转字符串
1、默认:
1
2
3
|
import datetime def datetime_to_datetime_str(date, _format = "%y-%m-%d %h:%m:%s" ): return date.strftime(_format) |
2、按时区转:
1
2
3
4
5
6
7
|
import datetime from pytz import timezone as tz def datetime_to_datetime_str_by_timezone(date, from_tz = “utc”, to_tz = "america/los_angeles”, _format=" % y - % m - % d % h: % m: % s"): from_tz = tz(from_tz) to_tz = tz(to_tz) date = date.replace(tzinfo = from_tz).astimezone(to_tz) return date.strftime(_format) |
五、datetime转时间戳
1、默认:
1
2
3
|
import time def datetime_to_timestamp(date): return int (time.mktime(date.timetuple())) |
2、按时区转:
1
2
3
4
5
6
7
|
import time from pytz import timezone as tz def datetime_to_timestamp_by_timezone(date, from_tz = “utc”, to_tz = "america/los_angeles" ): from_tz = tz(from_tz) to_tz = tz(to_tz) return int (time.mktime(date.replace( tzinfo = from_tz).astimezone(to_tz).timetuple())) |
六、时间戳转datetime
1、默认:
1
2
3
|
import datetime def timestamp_to_datetime(time_stamp): return datetime.datetime.fromtimestamp(time_stamp) |
2、按时区转:
1
2
3
4
5
|
import datetime from pytz import timezone as tz def timestamp_to_datetime_by_timezone(time_stamp, to_tz = "america/los_angeles" ): to_tz = tz(to_tz) return datetime.datetime.fromtimestamp(time_stamp, to_tz) |
ps:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:
unix时间戳(timestamp)转换工具:https://tool.zzvips.com/t/timestamp/
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/dream_successor/article/details/90295343