Created
February 6, 2016 08:40
-
-
Save gh640/3297797c02ccda32e886 to your computer and use it in GitHub Desktop.
UNIX タイムスタンプを Y-m-d 形式の日付に変換するコマンドスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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