-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcountdown.py
More file actions
executable file
·79 lines (65 loc) · 2.03 KB
/
countdown.py
File metadata and controls
executable file
·79 lines (65 loc) · 2.03 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""
Author: FelipeCRamos
"""
import sys
import datetime
def form_title():
"""
This will form the title, getting all the titles on:
./countdown.py DD/MM/YYYY [title] [second title] [ third title ] ...
* adding a space between them
"""
size = len(sys.argv)
title = str()
for i in range(2, size):
title += sys.argv[i]
if( i != size - 1 ):
title += " "
return title
def form_date(date_string):
"""
Getting a kind of regex on the date_string, getting the datetime object
DD/MM/YYYY
"""
try:
real_date = datetime.datetime.strptime(date_string, "%d/%m/%Y")
except:
print("Incorrect date format, please rerun with the format DD/MM/YYYY!")
exit()
return real_date.date()
# Escape sequences (make code simple)
blue_bold = "\033[36;1m"
normal = "\033[0m"
# Main
if(len(sys.argv) >= 3):
try:
# if the sys.argv got success
# forms the title
date_title = form_title()
# get's today info
today = datetime.date.today()
# get's desired date
foward_date = form_date(sys.argv[1])
# makes the difference between them
diff = (foward_date - today).days
# showcase the result in a pretty way
print("> {}{:0>3}{} days | "
"{}{:0>5.2f}{} weeks | "
"{}{:0>5.2f}{} months "
"-> {}!".format(
blue_bold, diff, normal,
blue_bold, diff / 7, normal,
blue_bold, diff / 30, normal,
date_title))
except Exception as exp :
print("Ops, didn't saw that coming...")
print("Error: {}".format(exp))
print("Please, feel free to drop that exception on my github page!")
exit()
else:
if( len(sys.argv) == 2 ):
print("\n\nYou forgot the title for the countdown!\n")
# wrong parameters
print("Please, (re)run the program with needed parameters!")
print("\nex:\t./countdown.py 10/1/2019 Math Test\n\n")