-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunits.py
63 lines (56 loc) · 2.32 KB
/
units.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
56
57
58
59
60
61
62
63
#!/usr/local/bin/python3
# Units Converter Script
# Authors: Kedar Vaijanapurkar, Denis Subbota
# GitHub: https://github.com/kedarvj
# Website: https://kedar.nitty-witty.com/blog
# Description: This script converts file sizes between sec, min, hour, bytes, kilobytes, megabytes, gigabytes, and terabytes.
# It provides a simple command-line tool for converting and comparing file sizes.
# Basically this was created to be used as an Alfred Workflow for quick units conversion.
import sys
import datetime
ab = sys.argv[1:]
a_string = "".join(ab)
a = int(a_string)
Hours_from_sec = float(a) / 60 / 60
Hours_from_min = float(a) / 60
days_from_sec = float(a) / 60 / 60 / 24
days_from_min = float(a) / 60 / 24
days_from_hours = float(a) / 24
years_from_hours = float(a)/24/365
years_from_days = float(a) / 365
try:
dt = datetime.datetime.fromtimestamp(a)
except ValueError:
dt="not a date"
format_strings = [
" sec = {0:.2f} hours".format(Hours_from_sec),
" sec = {0:.2f} days".format(days_from_sec),
" min = {0:.2f} hours".format(Hours_from_min),
" min = {0:.2f} days".format(days_from_min),
" hours = {0:.2f} days".format(days_from_hours),
" hours = {0:.2f} years".format(years_from_hours),
" years = {0:.2f} days".format(years_from_days)
]
for format_str in format_strings:
print("{0}{1}".format(a, format_str)) # Corrected the print statement
print("----------------------------------------------------------------------------------------------------------------------");
print("Unix time: {} ".format(dt))
print("----------------------------------------------------------------------------------------------------------------------");
MB_from_bytes = float(a) / (1024 ** 2)
GB_from_bytes = float(a) / (1024 ** 3)
TB_from_bytes = float(a) / (1024 ** 4)
GB_from_kb = float(a) / (1024 ** 2)
GB_from_MB = float(a) / 1024
TB_from_MB = float(a) / (1024 ** 3)
TB_from_GB = float(a) / 1024
format_strings = [
" bytes = {0:.2f} MB".format(MB_from_bytes),
" bytes = {0:.2f} GB".format(GB_from_bytes),
" bytes = {0:.2f} TB".format(TB_from_bytes),
" KB = {0:.2f} GB".format(GB_from_kb),
" MB = {0:.2f} GB".format(GB_from_MB),
" MB = {0:.2f} TB".format(TB_from_MB),
" GB = {0:.2f} TB".format(TB_from_GB)
]
for format_str in format_strings:
print("{0}{1}".format(a, format_str))