Skip to content

Instantly share code, notes, and snippets.

@nagishin
Created September 13, 2018 16:30
Show Gist options
  • Save nagishin/eb4002b06b088fd54d230b114cdc34d3 to your computer and use it in GitHub Desktop.
Save nagishin/eb4002b06b088fd54d230b114cdc34d3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-09-13 04:52:40.000098+00:00 <class 'datetime.datetime'>\n"
]
}
],
"source": [
"import time, pytz\n",
"from datetime import datetime, timedelta\n",
"\n",
"# UTC現在時刻(datetime)\n",
"dt_utc_now = datetime.now(pytz.utc)\n",
"print(dt_utc_now, type(dt_utc_now))"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1536814360 <class 'int'>\n"
]
}
],
"source": [
"# UNIX TIME(datetime -> int)\n",
"unixtime = int(dt_utc_now.timestamp())\n",
"print(unixtime, type(unixtime))"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-09-13T04:52:40.000098Z <class 'str'>\n"
]
}
],
"source": [
"# UTC現在時刻(datetime -> string)\n",
"dt_str = dt_utc_now.strftime(\"%Y-%m-%dT%H:%M:%S.%fZ\")\n",
"print(dt_str, type(dt_str))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-09-13 04:52:40.000098+00:00 <class 'datetime.datetime'>\n"
]
}
],
"source": [
"# UTC現在時刻(string -> datetime(time zone指定))\n",
"dt_utc = datetime.strptime(dt_str, \"%Y-%m-%dT%H:%M:%S.%fZ\").replace(tzinfo=pytz.utc)\n",
"print(dt_utc, type(dt_utc))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-09-13 13:52:40.000098+09:00 <class 'datetime.datetime'>\n"
]
}
],
"source": [
"# time zone変更(datetime(UTC -> JST))\n",
"dt_jst = dt_utc_now.astimezone(pytz.timezone(\"Asia/Tokyo\"))\n",
"print(dt_jst, type(dt_jst))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1536814370.2933867 <class 'float'>\n"
]
}
],
"source": [
"# UNIX TIME(time)\n",
"unix_time = time.time()\n",
"print(unix_time, type(unix_time))"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-09-13 05:52:40.000098+00:00 <class 'datetime.datetime'>\n"
]
}
],
"source": [
"# 1時間後(datetime)\n",
"dt_after_1h = dt_utc_now + timedelta(hours=1)\n",
"print(dt_after_1h, type(dt_after_1h))"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1536817960 <class 'int'>\n"
]
}
],
"source": [
"# 1時間後(UNIX TIME)\n",
"int_after_1h = unix_time + (60 * 60)\n",
"print(int_after_1h, type(int_after_1h))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment