-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunixtime2date.py
executable file
·55 lines (48 loc) · 1.67 KB
/
unixtime2date.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
########################################################################
# unixtime2date.py: Convert Unix Timestamp to Human-Readable Date
#
# Description:
# This script converts a Unix timestamp to a human-readable date format in ISO 8601.
# It uses the local timezone for conversion.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.2 2023-12-11
# Modified output format to ISO 8601 in local timezone.
# v1.1 2023-12-06
# Removed Python version check and added comments for clarity.
# v1.0 2014-07-29
# Initial release.
#
# Usage:
# unixtime2date.py <unix_timestamp>
#
# Example:
# unixtime2date.py 1609459200
# -> 2021-01-01T09:00:00+09:00 (if your local timezone is UTC+9)
#
########################################################################
import datetime
import sys
def unixtime2date(its):
"""Convert Unix timestamp to human-readable date in local timezone (ISO 8601 format)."""
local_datetime = datetime.datetime.fromtimestamp(
its, datetime.timezone.utc).astimezone()
return local_datetime.isoformat()
def main(args):
"""Process the command line arguments and output the result."""
if len(args) > 1:
try:
print(unixtime2date(int(args[1])))
except ValueError:
print("Invalid timestamp. Please enter a valid Unix timestamp.")
else:
print("Usage: unixtime2date.py <unix_timestamp>")
print("Example: unixtime2date.py 1609459200")
if __name__ == '__main__':
main(sys.argv)