-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_time.py
59 lines (43 loc) · 1.49 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
import pytz
from datetime import datetime
from dateutil import parser
def convert_to_ist(utc_dt):
# Define UTC timezone
utc = pytz.timezone('UTC')
# Define IST timezone
ist = pytz.timezone('Asia/Kolkata')
# Convert UTC datetime to IST
ist_dt = utc_dt.astimezone(ist)
# Format the DateTime object
formatted_dt = ist_dt.strftime('%d-%B-%Y %H:%M:%S')
# Get the time zone abbreviation
tz_abbr = ist_dt.strftime('%Z')
# Construct the final string with time zone
final_str = f"{formatted_dt} {tz_abbr}"
return final_str
def date_time():
"""return current date time formatted"""
now = datetime.now()
formatted_datetime = now.strftime("%d-%B-%Y %H:%M")
return formatted_datetime
# print(formatted_datetime)
def compare_dates(expiry_date_str):
# Get the current date and time in UTC in the same format
current_date_str = datetime.utcnow().isoformat() + 'Z'
# Parse the date strings into datetime objects
current_date = parser.isoparse(current_date_str)
expiry_date = parser.isoparse(expiry_date_str)
# Find the difference between the dates
time_difference = expiry_date - current_date
days = time_difference.days
# Return the results
return days
def main():
# to test the code #
# Sample DateTime object in UTC
utc_dt = datetime(2024, 7, 25, 18, 51, 25, 764458, tzinfo=pytz.utc)
# Convert to IST
ist_str = convert_to_ist(utc_dt)
print(ist_str)
if __name__ == "__main__":
main()