-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_time.py
67 lines (51 loc) · 2.05 KB
/
date_time.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
64
65
66
67
#!/usr/bin/python3
"""
date_time.py
Date and time functions.
"""
import calendar
import time
from datetime import datetime, timedelta
def convert_timestamp(timestamp):
"""
Function to convert timestamp to readable datetime
"""
return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
def get_last_mont():
"""
Get the month before this mont, ex 2024-05 for year 2024 nad month may if it is year 2024 and july now
"""
# Get the current date
current_date = datetime.now()
# Calculate the first day of the current month
first_day_of_current_month = current_date.replace(day=1)
# Subtract one day from the first day of the current month to get the last day of the previous month
last_day_of_last_month = first_day_of_current_month - timedelta(days=1)
# Extract the year and month of the last day of the previous month
year = last_day_of_last_month.year
month = last_day_of_last_month.month
# Format the result as "YYYY-MM"
last_month = f"{year}-{month:02d}"
return last_month
def get_first_last_timestamps(year_month):
"""
Get Unix timestamps for the first and last day of a given year-month.
year_month should be like 2024-05 for year 2024 and may.
"""
year, month = map(int, year_month.split('-'))
first_day = datetime(year, month, 1)
first_day_timestamp = int(first_day.timestamp())
last_day = datetime(year, month, calendar.monthrange(year, month)[1], 23, 59, 59)
last_day_timestamp = int(last_day.timestamp())
return first_day_timestamp, last_day_timestamp
## Start
if __name__ == "__main__":
current_timestamp = time.time() # Get the current Unix timestamp
print("")
readable_date = convert_timestamp(current_timestamp)
print(f"Todays date and time: {readable_date}")
last_month = get_last_mont()
print(f"Last month: {last_month}")
first_day_timestamp, last_day_timestamp = get_first_last_timestamps(last_month)
print(f"First day timestamp = {first_day_timestamp}. Last day timestamp = {last_day_timestamp}")
print("")