Handling date-time in the Open Event Project

Handling date time in code becomes little tricky when the project is used internationally because then there comes additional term Timezone. Timezone is a property of a location which needs to be considered while comparing that time with the time in another location. For example - there are two villages A and B. One day Ram from village A calls his friend Shyam in village B at 8:00 am to wish "good morning". But Shyam receives Ram's call at 6pm on same day and he replies "good evening". That means village A's timezone is 10 hrs  behind village B's timezone. So here we need some reference timezone about which all other timezones can be declared. This is where UTC (Coordinated Universal Time) comes into play. UTC is reference timezone by which all the timezones are declared. For example - Indian timezone is 5 hrs and 30 mins ahead of UTC which is denoted as UTC+05:30. In languages, these timezones are declared in date time library using constants such as 'Asia/Kolkata' which is Indian Standard Time. I will be talking about working with  date time in python in this blog. In the FOSSASIA's Open Event project since it is event management system, handling date-time with the timezone is one of the important tasks. Here is the relevant code: >>> import datetime >>> import pytz >>> now = datetime.datetime.now() datetime.datetime.now()  returns naive datetime as of the time setting of the machine on which the code is running. Naive date means it doesn't contain any info about the timezone. It just contains some number values of year, month, hours etc. So just by looking at naive date we cannot actually understand the time. There comes aware datetime which contains timezone info. >> now datetime.datetime(2017, 5, 12, 21, 46, 16, 909983) >>> now.isoformat() '2017-05-12T21:46:16.909983' >>> aware_now = pytz.timezone('Asia/Kolkata').localize(now) >>> aware_now datetime.datetime(2017, 5, 12, 21, 46, 16, 909983, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD> Pytz provides timezone object which takes string argument for timezone which has localize method which adds timezone info to the datetime object. Hence now aware datetime has timezone info too. Now if we print the time. >>> aware_now.isoformat() '2017-05-12T21:46:16.909983+05:30 We get the +05:30 extra string at the end which gives timezone info. +05:30 means the timezone is 5 hrs and 30 mins ahead of UTC timezone. The comparison between datetimes can be made between naive-naive and aware-aware. If we try to compare between naive and aware, >>> now < aware_now Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't compare offset-naive and offset-aware datetimes >>> now2 = datetime.datetime.now() >>> now2 datetime.datetime(2017, 5, 15, 9, 44, 25, 990666) >>> now < now2 True >>> aware_now.tzinfo <DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD> tzinfo carries timezone info of the datetime object. We can make aware date to unaware by method replacing tzinfo to None. >>> unaware_now = aware_now.replace(tzinfo=None) >>> unaware_now.isoformat() '2017-05-12T21:46:16.909983' Formating datetime is done by mostly these two methods. One of which takes string format in which the result is required and another…

Continue ReadingHandling date-time in the Open Event Project