-
Notifications
You must be signed in to change notification settings - Fork 1
/
general_funcs.py
69 lines (53 loc) · 1.94 KB
/
general_funcs.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
68
69
from datetime import datetime, date, timedelta
from dateutil import parser
def get_today_iso():
"""
Get the current date in ISO format.
Returns:
str: The current date as a string in the format 'YYYY-MM-DD'.
Examples:
>>> get_today_iso()
'2023-08-15'
"""
return date.today().isoformat()
def robust_parse_date(date_string):
"""
Robustly parses a date string and returns the date in ISO format.
Utilizes fuzzy parsing to handle various date formats.
Args:
date_string (str): The date string to be parsed.
Returns:
str: The parsed date as a string in the format 'YYYY-MM-DD'.
Raises:
ValueError: If the date_string can't be parsed.
Examples:
>>> robust_parse_date("Aug 15, 2023")
'2023-08-15'
"""
# Attempt to parse the date_string
dt = parser.parse(date_string, fuzzy=True)
# Return the date in the ISO format
return dt.date().isoformat()
def get_date_str(date_str):
"""
Converts a date string in the format 'YYYY-MM-DD' to a more readable format 'Mon DD, YYYY'.
Args:
date_str (str): The date string in the format 'YYYY-MM-DD'.
Returns:
str: The date in the format 'Mon DD, YYYY'.
Raises:
ValueError: If the input date string is not in the 'YYYY-MM-DD' format.
Examples:
>>> get_date_str("2023-08-15")
'Aug 15, 2023'
"""
# Parse the input date string to a datetime object
date_obj = datetime.strptime(date_str, '%Y-%m-%d')
# Format the datetime object to the desired output format
return date_obj.strftime('%b %d, %Y')
def get_time_minus_4h():
now = datetime.now()
four_hours_ago = now - timedelta(hours=4)
time_minus_4h = four_hours_ago.strftime("%H:%M:%S")
date_of_time_minus_4h = four_hours_ago.strftime("%Y-%m-%d")
return date_of_time_minus_4h, time_minus_4h