Python的標準模塊datetime模塊,在咱們的工做中應用很是頻繁,下面對datetime中經常使用的方法進行了總結和測試;對每個方法都使用了單元測試框架Unittest來配合測試。框架
主要的類型有:單元測試
首先要導入datetime模塊:from datetime import datetime, timedelta, timezone測試
1、本身定義時間格式:一、datetime(2017,2,22,16,5,26);二、datetime.strptime('2017-02-22 16:05:26', '%Y-%m-%d %H:%M:%S')spa
2、將datetime轉化成timestamp格式:datetime(2017,2,22,16,5,26).timestamp()code
3、將timestamp轉化成datetime格式:datetime.fromtimestamp(1487750726.0)blog
4、將datetime轉化成str格式:datetime(2017,2,22,16,5,26).strftime('%Y-%m-%d %H:%M:%S')utf-8
5、利用timedelta進行時間相加:datetime.strptime('2017-02-22 16:05:26', '%Y-%m-%d %H:%M:%S') + timedelta(hours=10, days=2)get
6、利用timedelta進行時間相減:datetime.strptime('2017-02-22 14:05:26', '%Y-%m-%d %H:%M:%S') - timedelta(hours=1)it
8、轉換時區:io
utc_datetime = datetime.strptime('2017-02-22 17:05:26', '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone(timedelta(hours=8)))
# 北京時間
bj_datetime = utc_datetime.astimezone(timezone(timedelta(hours=8)))
# 東京時間
dj_datetime = bj_datetime.astimezone(timezone(timedelta(hours=9)))
下面的就是代碼,testDatetime.py:
# coding=utf-8 """ testDatetime.py datetime模塊練習 """ import unittest from datetime import datetime, timedelta, timezone class TestDatetime(unittest.TestCase): def test_get_datetime(self): """指定日期和時間datetime""" expected = "2017-02-22 16:05:26" actual = datetime(2017,2,22,16,5,26) # print(actual) self.assertEqual(expected, str(actual)) def test_datetime_translation_timestamp(self): """將datetime轉化成timestamp,返回的是秒""" expected = 1487750726.0 actual = datetime(2017,2,22,16,5,26).timestamp() # print(type(actual)) self.assertEqual(expected, actual) def test_timestamp_translation_datetime(self): """將timestamp轉化成datetime格式""" expected = "2017-02-22 16:05:26" actual = datetime.fromtimestamp(1487750726.0) # print(actual) self.assertEqual(expected, str(actual)) def test_str_translation_datetime(self): """將str轉化成datetime格式""" expected = datetime(2017,2,22,16,5,26) actual = datetime.strptime('2017-02-22 16:05:26', '%Y-%m-%d %H:%M:%S') # print(actual) self.assertEqual(expected, actual) def test_datetime_translation_str(self): """將datetime轉化成str""" excepted = '2017-02-22 16:05:26' actual = datetime(2017,2,22,16,5,26).strftime('%Y-%m-%d %H:%M:%S') # print(type(actual)) self.assertEqual(excepted, actual) def test_datetime_add(self): """利用timedelta進行時間相加""" # 加1小時 excepted1 = datetime.strptime('2017-02-22 17:05:26', '%Y-%m-%d %H:%M:%S') actual1 = datetime.strptime('2017-02-22 16:05:26', '%Y-%m-%d %H:%M:%S') + timedelta(hours=1) # 加2天,10小時 excepted2 = datetime.strptime('2017-02-25 02:05:26', '%Y-%m-%d %H:%M:%S') actual2 = datetime.strptime('2017-02-22 16:05:26', '%Y-%m-%d %H:%M:%S') + timedelta(hours=10, days=2) self.assertEqual(excepted1, actual1) self.assertEqual(excepted2, actual2) def test_datetime_plus(self): """利用timedelta進行時間相減""" # 減1小時 excepted1 = datetime.strptime('2017-02-22 16:05:26', '%Y-%m-%d %H:%M:%S') actual1 = datetime.strptime('2017-02-22 17:05:26', '%Y-%m-%d %H:%M:%S') - timedelta(hours=1) self.assertEqual(excepted1, actual1) def test_timezone_translation(self): """轉換時區""" utc_datetime = datetime.strptime('2017-02-22 17:05:26', '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone(timedelta(hours=8))) # 北京時間 bj_datetime = utc_datetime.astimezone(timezone(timedelta(hours=8))) # 東京時間 dj_datetime = bj_datetime.astimezone(timezone(timedelta(hours=9))) # print(dj_datetime) self.assertEqual((bj_datetime + timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S'), dj_datetime.strftime('%Y-%m-%d %H:%M:%S')) if __name__ == '__main__': unittest.main()
還有測試結果圖(所有測試經過):