What is Time stamp?
In Python, Date and Time are data types not object
Python Date Time Module: It has classes to work on date and time. These classes have number of function to deal with data and time.
Date Time Module categorized in 6 part:
Date:
List of Date Class Methods
Function Name | Description |
---|---|
ctime() | Return a string representing the date |
fromisocalendar() | Returns a date corresponding to the ISO calendar |
fromisoformat() | Returns a date object from the string representation of the date |
fromordinal() | Returns a date object from the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1 |
fromtimestamp() | Returns a date object from the POSIX timestamp |
isocalendar() | Returns a tuple year, week, and weekday |
isoformat() | Returns the string representation of the date |
isoweekday() | Returns the day of the week as an integer where Monday is 1 and Sunday is 7 |
replace() | Changes the value of the date object with the given parameter |
strftime() | Returns a string representation of the date with the given format |
timetuple() | Returns an object of type time.struct_time |
today() | Returns the current local date |
toordinal() | Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1 |
weekday() | Returns the day of the week as integer where Monday is 0 and Sunday is 6 |
fromisocalendar()
method transforms a date in the Gregorian calendar format to an equivalent date in the ISO calendar format. This date is defined by the year, the week number, and the day of the week.Time:
It represents a local time of the day which is independent of any particular day, This class can have the tzinfo object which represents the time zone of the given time.
ex:
from datetime import time
mytime=time(12,4,5)
mytimem=time(minute=12)
print(mytimem,mytime)
Attributes provided by this Time class
Attribute Name | Description |
---|---|
min | Minimum possible representation of time |
max | Maximum possible representation of time |
resolution | The minimum possible difference between time objects |
hour | The range of hour must be between 0 and 24 (not including 24) |
minute | The range of minute must be between 0 and 60 (not including 60) |
second | The range of second must be between 0 and 60 (not including 60) |
microsecond | The range of microsecond must be between 0 and 1000000 (not including 1000000) |
tzinfo | The object containing timezone information |
fold | Represents if the fold has occurred in the time or not |
1. from datetime import time
print(time.min,time.max)
2.
from datetime import time
mytime=time(12,34,56)
print(mytime,mytime.minute,mytime.hour,mytime.second)
Time Class Functions
Function Name | Description |
---|---|
dst() | Returns tzinfo.dst() is tzinfo is not None |
fromisoformat() | Returns a time object from the string representation of the time |
isoformat() | Returns the string representation of time from the time object |
replace() | Changes the value of the time object with the given parameter |
strftime() | Returns a string representation of the time with the given format |
tzname() | Returns tzinfo.tzname() is tzinfo is not None |
utcoffset() | Returns tzinfo.utcffsets() is tzinfo is not None |
DateTime: It contains information date as well as time. it contain information regarding time zone as well.
Class Attributes of DateTime
Let’s see the attributes provided by this class:
Attribute Name | Description |
---|---|
min | The minimum representable DateTime |
max | The maximum representable DateTime |
resolution | The minimum possible difference between datetime objects |
year | The range of year must be between MINYEAR and MAXYEAR |
month | The range of month must be between 1 and 12 |
day | The range of days must be between 1 and the number of days in the given month of the given year |
hour | The range of hours must be between 0 and 24 (not including 24) |
minute | The range of minutes must be between 0 and 60 (not including 60) |
second | The range of second must be between 0 and 60 (not including 60) |
microsecond | The range of microseconds must be between 0 and 1000000 (not including 1000000) |
tzinfo | The object containing timezone information |
fold | Represents if the fold has occurred in the time or not |
https://www.geeksforgeeks.org/python-datetime-module/
TimeDelta: It is used for calculating differences between dates and represents a duration. The difference can both be positive as well as negative.
from datetime import date,timedelta
mytime=date(12,3,5)
mytime=mytime-timedelta(days=5)
mytime
tzinfo:
tzinfo is an abstract base class. It cannot be instantiated directly. A concrete subclass has to derive it and implement the methods provided by this abstract class.
The instance of the tzinfo class can be passed to the constructors of the datetime and time objects. It finds its applications in situations such as the conversion of local time to UTC or to account for daylight saving time.
The methods available for implementation in tzinfo base class are :
- utcoffset(self, dt): It return the offset of date time instance passed as an argument.
- dst(self, dt):
- tzname(self, dt)
Timezone: It is used when we have to display time according to the time of specific region.
from datetime import datetime,tzinfo,timedelta
from pytz import timezone
print(datetime.now(timezone('UTC')))
print(datetime.now(timezone('Asia/Kolkata')))
print(datetime.now(timezone('Europe/Kiev')))
convert time zone
from datetime import datetime,tzinfo,timedelta
from pytz import timezone
ut=datetime.now(timezone('UTC'))
now_asia = ut.astimezone(timezone('Europe/Kiev'))
print(now_asia,ut)