Skip to content

Instantly share code, notes, and snippets.

@gh640
Created February 6, 2016 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gh640/3297797c02ccda32e886 to your computer and use it in GitHub Desktop.
Save gh640/3297797c02ccda32e886 to your computer and use it in GitHub Desktop.
UNIX タイムスタンプを Y-m-d 形式の日付に変換するコマンドスクリプト
#!python3
# coding: utf-8
"""
usage: python convert_timestamp_to_date.py [timestamp]
If you add alias for this, it's much easier to use this.
"""
import sys
import datetime
# コマンドライン引数を取得
argv = sys.argv
if len(argv) != 2:
print('usage: (command) timestamp')
exit()
timestamp_string = argv[-1]
# 引数が正しい場合はタイムスタンプにして返す
try:
timestamp = int(timestamp_string)
except ValueError as e:
print(e)
print('timestamp must be an integer.')
exit()
else:
time_formatted = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
print(time_formatted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment