1+ """##############################################################################
2+ Using Times, Dates & Timestamps in Python
3+
4+ Summary of Time and Date classes:
5+ datetime # superclass for most of the date and time libraries
6+ date # general purpose date library
7+ time # general purpose time library
8+ datetime # for date and time in one object
9+ timedelta # for a duration or elapsed time
10+ time # for Unix timestamp and process_time
11+ calendar # for calendars
12+ dateutil # extended datetime functionality, esp string parsing and delta calculation
13+ ##############################################################################"""
14+
15+ # Today's Date
16+ # ---------------------------
17+ # Use datetime.date.today()
18+ # datetime.date class has the following integer attributes, date(year, month, day)
19+ # get day of the week using date.weekday() # Monday is 0
20+
21+ from datetime import date
22+ d1 = date .today ()
23+ print (d1 )
24+ print (d1 .month , d1 .day , d1 .year )
25+ print (d1 .weekday ())
26+
27+ # ISO format is a string format, yyyy-mm-dd
28+ # ---------------------------
29+ # date_object.isoformat() does the same thing as str(date_object)
30+
31+ from datetime import date
32+ d1 = date .fromisoformat ('2011-11-23' )
33+ print (d1 )
34+ print (str (d1 ))
35+ print (d1 .isoformat ())
36+ d1
37+
38+ # Comparison, addition and sutraction of dates
39+ # ---------------------------
40+ # Comparison gives boolean result. Later date is greater than earlier date.
41+ # Date addition & subtraction give result as a datetime.timedelta object (explained more below).
42+ # The same comparison and add/subtract operations can be used with time objects.
43+
44+ from datetime import date
45+ d1 = date .today ()
46+ d2 = date (2015 , 5 , 14 )
47+ print (d1 > d2 )
48+ print (d1 - d2 )
49+
50+ # Time
51+ # ---------------------------
52+ # time objects have the following attributes, time(hour, minute, second, microsecond, tzinfo)
53+ # use datetime.time to compare time objects: t1 < t2 if t1 occurs before t2
54+ # attributes are all optional, so you can just work with hours and minutes if you want
55+ # daylight savings is handled by the time.dst() function (if tzinfo is set)
56+
57+ from datetime import time
58+ t1 = time (14 , 25 , 36 , 18625 )
59+ print (t1 )
60+
61+ t2 = time (2 , 19 )
62+ print (t2 )
63+ print (t1 < t2 )
64+
65+ # datetime.datetime combines date and time attributes into a datetime object
66+ # ---------------------------
67+ # datetime.datetime(year, month, day, hour, minute, second, microsecond, tzinfo)
68+ # datetime.datetime objects can be used as dictionary keys
69+ # includes functions: date(), time()
70+
71+ from datetime import datetime
72+ dt1 = datetime (1941 , 12 , 7 , 7 , 53 )
73+ print (dt1 )
74+ print (dt1 .date ())
75+ print (dt1 .time ())
76+
77+ # Use datetime.datetime.now() to get the current date and time
78+
79+ t3 = datetime .now ()
80+
81+ print (t3 .time ())
82+ print (t3 .date ())
83+ print (t3 .hour , t3 .minute )
84+ print (str (t3 .month ) + '-' + str (t3 .day ) + '-' + str (t3 .year ))
85+
86+ # Use datetime.strftime() to get names of months and weekdays.
87+
88+ t3 = datetime .now ()
89+ print (t3 .strftime ('%A' ))
90+ print (t3 .strftime ('%a, %A, %b, %B, %x' ))
91+
92+ # A timedelta is used for a duration, or the time difference between two dates or times
93+ # ---------------------------
94+ # datetime.timedelta(days, seconds, microseconds)
95+ # A timedelta can also be multiplied or divided by an integer or float
96+
97+ from datetime import timedelta , date , time
98+ d1 = date (2011 , 6 , 15 )
99+ d2 = date (2012 , 9 , 18 )
100+ td = d2 - d1
101+ print (td , type (td ))
102+ print (td .total_seconds ())
103+ print (td * 3 )
104+
105+ from datetime import datetime
106+ today = datetime .today ().date ()
107+ my_event = date (2021 , 11 , 6 )
108+ days_to_event = abs (my_event - today )
109+ print (days_to_event .days , 'days to event.' )
110+ print (days_to_event , 'days to event.' )
111+
112+ # Get a UNIX timestamp (time since the epoch)
113+ # ---------------------------
114+ # A timestamp is the time since Jan 1, 1970 in seconds
115+ # Use time.time() to get timestamp
116+ # Use datetime.fromtimestamp(ts) and datetime.timestamp(datetime_obj) to convert between timestamp and datetime
117+ # Use time.process_time() to get runtime of an operation on your computer
118+
119+ import time
120+ ts = time .time ()
121+ print (ts )
122+ print (time .ctime (ts ))
123+
124+
125+ from datetime import datetime
126+ now = datetime .fromtimestamp (ts )
127+ print (now )
128+ print (datetime .timestamp (now ))
129+
130+ start_time = time .process_time ()
131+ # do some stuff
132+ end_time = time .process_time ()
133+ print ('operation executed in ' , end_time - start_time )
0 commit comments