Skip to content

Instantly share code, notes, and snippets.

@tamago324
Created March 28, 2018 15:34
Show Gist options
  • Save tamago324/9d418f8e4bfdceb951e6f13865a5624f to your computer and use it in GitHub Desktop.
Save tamago324/9d418f8e4bfdceb951e6f13865a5624f to your computer and use it in GitHub Desktop.
2つのunixtimeから差を求めて、h:mm:ssの形式の文字列を返す
def diff_time_hmmss(unixtime1, unixtime2):
""" 2つのunixタイムスタンプの差を返す(h:mm:ss)
"""
# 絶対値の取得
diff = abs(unixtime2 - unixtime1)
h = int((diff / 60) / 60)
m = int((diff / 60) % 60)
s = int(diff % 60)
# hourが0の場合、表示しない
if h == 0:
h_str = ''
else:
h_str = f'{h}:'
return f'{h_str}{m:0>2}:{s:0>2}'
if __name__ == '__main__':
# 2018/03/29 00:00:00 と 2018/03/29 01:23:45
print(diff_time_hmmss(1522249200, 1522254225)) # 1:23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment