{"id":856,"date":"2015-04-10T15:30:56","date_gmt":"2015-04-10T06:30:56","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=856"},"modified":"2018-12-18T01:30:35","modified_gmt":"2018-12-17T16:30:35","slug":"python-datetime-unix-time","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2015\/04\/python-datetime-unix-time\/","title":{"rendered":"Python \u306e datetime \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068 Unix \u6642\u9593\u306e\u76f8\u4e92\u5909\u63db\u65b9\u6cd5\u306e\u307e\u3068\u3081"},"content":{"rendered":"

Python \u306e datetime \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068 Unix \u6642\u9593 (Unix time \/ Epoch time) \u3092\u76f8\u4e92\u5909\u63db\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u307e\u3068\u3081\u307e\u3059. Python \u306e datetime \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u306f\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u304c\u4ed8\u3044\u3066\u3044\u308b\u3082\u306e (aware) \u3068\u4ed8\u3044\u3066\u3044\u306a\u3044\u3082\u306e (naive) \u304c\u3042\u308b\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059.
\n<\/p>\n

Unix \u6642\u9593<\/h2>\n

Unix \u6642\u9593 (Unix time) \u306f UTC<\/strong> \u3067\u306e 1970\/1\/1 00:00:00 \u304b\u3089\u306e\u7d4c\u904e\u79d2\u6570\u3067\u8868\u3055\u308c\u307e\u3059. \u591a\u304f\u306e\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u958f\u79d2\u306e\u5b58\u5728\u3092\u8003\u616e\u3057\u306a\u3044\u5b9f\u88c5\u3068\u306a\u3063\u3066\u3044\u308b\u3088\u3046\u3067, \u3053\u306e\u5834\u5408 Unix time \u3068 UTC, JST \u306e\u5bfe\u5fdc\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n\n\n\n\n\n
Unix time<\/th>\nUTC<\/th>\nJST (UTC+9)<\/th>\n<\/tr>\n
0<\/td>\n1970\/1\/1 00:00:00<\/td>\n1970\/1\/1 09:00:00<\/td>\n<\/tr>\n
1429000000<\/td>\n2015\/4\/14 08:26:40<\/td>\n2015\/4\/14\u00a017:26:40<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

“naive” \u3068 “aware”<\/h2>\n

Python \u306e datetime \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u306f “naive” \u3068 “aware” \u306e2\u7a2e\u985e\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u3042\u308a\u307e\u3059. aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u6301\u3064\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067 tzinfo \u306b\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u306e\u5c5e\u6027\u3092\u6301\u3061\u307e\u3059. \u3053\u308c\u306b\u5bfe\u3057\u3066, naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u6301\u305f\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059.<\/p>\n

\u3053\u308c\u306b\u3064\u3044\u3066\u306e\u8a73\u7d30\u306f Python \u306e\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u3082\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044.<\/p>\n

\u30ed\u30fc\u30ab\u30eb\u30bf\u30a4\u30e0\u306e naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/h2>\n
import datetime\nimport time\n\n\n# \u30ed\u30fc\u30ab\u30eb\u30bf\u30a4\u30e0\u306e naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\nnow = datetime.datetime.now()\nprint(now)\n\n# \u30ed\u30fc\u30ab\u30eb\u30bf\u30a4\u30e0\u306e naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8 -> Unix time\nunix = int(time.mktime(now.timetuple()))\nprint(unix)\n\n# Python 3.3 \u4ee5\u964d\u306e\u307f\u3067\u306f time \u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u308f\u305a\u306b\u66f8\u304f\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\n# \u30ed\u30fc\u30ab\u30eb\u30bf\u30a4\u30e0\u306e naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8 -> Unix time\nunix = int(now.timestamp())\nprint(unix)\n\n# Unix time -> \u30ed\u30fc\u30ab\u30eb\u30bf\u30a4\u30e0\u306e naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\nnow = datetime.datetime.fromtimestamp(unix)\nprint(now)<\/code><\/pre>\n

UTC \u306e naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/h2>\n
import calendar\nimport datetime\n\n\n# UTC \u306e naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\nnow = datetime.datetime.utcnow()\nprint(now)\n\n# UTC \u306e naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8 -> Unix time\nunix = calendar.timegm(now.utctimetuple())\nprint(unix)\n\n# Python 3.3 \u4ee5\u964d\u3067\u3082, UTC \u306e naive \u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3057\u3066,\n# now.timestamp() \u3092\u4f7f\u3046\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\n\n# Unix time -> UTC \u306e naive \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\nnow = datetime.datetime.utcfromtimestamp(unix)\nprint(now)<\/code><\/pre>\n

aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/h2>\n

aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5834\u5408\u306f, \u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u7528\u3044\u3066\u4e00\u65e6 UTC \u306b\u5909\u63db\u3057\u3066 Unix time \u3092\u6c42\u3081, Unix time \u304b\u3089 datetime \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u623b\u3059\u969b\u306b\u518d\u3073\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u4ed8\u52a0\u3059\u308b\u3088\u3046\u306a\u5b9f\u88c5\u306b\u306a\u308a\u307e\u3059.<\/p>\n

\u3053\u3053\u3067\u306f\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u6271\u3046\u305f\u3081\u306b pytz<\/a> \u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3059.<\/p>\n

import calendar\nimport datetime\n\nimport pytz\n\n\n# UTC \u306e aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\n# Python 3.2 \u4ee5\u964d\u3067\u306f tz = datetime.timezone.utc \u3082\u53ef\ntz = pytz.utc\nnow = datetime.datetime.now(tz)\nprint(now)\n\n# UTC \u306e aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8 -> Unix time\nunix = calendar.timegm(now.utctimetuple())\nprint(unix)\n\n# Python 3.3 \u4ee5\u964d\u3067\u306f calendar \u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u308f\u305a\u306b\u66f8\u3051\u307e\u3059\n# UTC \u306e aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8 -> Unix time\nunix = int(now.timestamp())  # calendar.timegm \u3068\u7570\u306a\u308a float \u306e\u5024\u304c\u8fd4\u3055\u308c\u308b\nprint(unix)\n\n# Unix time -> UTC \u306e aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\nnow = datetime.datetime.fromtimestamp(unix, tz=tz)\nprint(now)\n\n\n# PST \u306e aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\ntz = pytz.timezone(\"US\/Pacific\")\nnow = datetime.datetime.now(tz)\nprint(now)\n\n# PST \u306e aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8 -> Unix time\nunix = calendar.timegm(now.utctimetuple())\nprint(unix)\n\n# Python 3.3 \u4ee5\u964d\u3067\u306f calendar \u3092\u4f7f\u308f\u305a\u306b\u66f8\u3051\u307e\u3059\n# PST \u306e aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8 -> Unix time\nunix = int(now.timestamp())  # calendar.timegm \u3068\u7570\u306a\u308a float \u306e\u5024\u304c\u8fd4\u3055\u308c\u308b\nprint(unix)\n\n# Unix time -> PST \u306e aware \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\nnow = datetime.datetime.fromtimestamp(unix, tz=tz)\nprint(now)<\/code><\/pre>\n

\u307e\u3068\u3081<\/h2>\n

Python \u3067\u306f datetime \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c “naive” \u304b “aware” \u304b\u3092\u610f\u8b58\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u306b\u306a\u308a\u307e\u3059. \u8a73\u7d30\u306a\u90e8\u5206\u306b\u3064\u3044\u3066\u306f\u4ee5\u4e0b\u306e\u30ea\u30f3\u30af\u3082\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044.<\/p>\n

\u5909\u66f4\u5c65\u6b74<\/h2>\n