Python Time

Python Time :  In this article, we’ll see how to use Python’s modules which are related to the time and date.

In this python time article we will cover lots of problem like current time, previous time, time gap, epoch time etc.

Let us start with the python time first module time.

Python Time – Time:

First import the time module. Then we will study some important functions of time modules.

The time() function

The time() function returns the float value of time. The float value is called Epoch or Unix Timestamp, defined as an approximation of the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,

To know more about Epoch go to the website https://www.epochconverter.com/

>>> import time

>>> time.time()

1534587871.267

Now you can see the float value. This float value is very significant in some cases. For example, time comparison. Consider you have 2 dates like 15 Aug 2018 and 26 Jan 2018. Which one is bigger or newer?  Both the dates are in the string format. Better to convert the dates into float or int values then perform the comparison.

First, convert then understand the underlying logic.

>>> int(time.mktime(time.strptime(’15 Aug 2018′, ‘%d %b %Y’)))

1534271400

>>>

>>> int(time.mktime(time.strptime(’26 Jan 2018′, ‘%d %b %Y’)))

1516905000

>>>

Now you can compare the dates.

The strptime() parse a string representing a time according to a format. The return value is a struct_time as returned by gmtime() or localtime(). The gmtime() returns the Greenwich mean time and localtime() returns the local time according to your timezone.

See the following example of gmtime() and localtime().

>>> time.gmtime()

time.struct_time(tm_year=2018, tm_mon=8, tm_mday=18, tm_hour=13, tm_min=55, tm_sec=13, tm_wday=5, tm_yday=230, tm_isdst=0)

>>>

>>> time.localtime()

time.struct_time(tm_year=2018, tm_mon=8, tm_mday=18, tm_hour=19, tm_min=25, tm_sec=26, tm_wday=5, tm_yday=230, tm_isdst=0)

>>>

The mktime() function take time.struct_time

Its argument is the struct_time or full 9-tuple. It returns a floating-point number means epoch time.

Let’s get back to the time.strptime function.

The time.strptime function has the ability to convert the human readable date to struct_time.

The human dates may be of different types. The human dates may be of different types like ’15 Aug 2018′, ’15 08 2018′,’15 08 18′ etc. With the help of directives, we can convert the different types of dates in the common format.

Let us understand by the examples.

>>> time.strptime(’15 Aug 2018′, ‘%d %b %Y’)

time.struct_time(tm_year=2018, tm_mon=8, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=227, tm_isdst=-1)

>>>

>>>

>>> time.strptime(’15 08 2018′, ‘%d %m %Y’)

time.struct_time(tm_year=2018, tm_mon=8, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=227, tm_isdst=-1)

>>>

>>> time.strptime(’15 08 18′, ‘%d %m %y’)

time.struct_time(tm_year=2018, tm_mon=8, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=227, tm_isdst=-1)

>>>

Why did I used %b, %y, %Y? See the below.

Learn Python with our Experts

Directive

  • %a – abbreviated weekday name
  • %A – full weekday name
  • %b – abbreviated month name
  • %B – full month name
  • %c – preferred date and time representation
  • %C – century number (the year divided by 100, range 00 to 99)
  • %d – day of the month (01 to 31)
  • %D – same as %m/%d/%y
  • %e – day of the month (1 to 31)
  • %g – like %G, but without the century
  • %G – 4-digit year corresponding to the ISO week number (see %V).
  • %h – same as %b
  • %H – hour, using a 24-hour clock (00 to 23)
  • %I – hour, using a 12-hour clock (01 to 12)
  • %j – day of the year (001 to 366)
  • %m – month (01 to 12)
  • %M – minute
  • %n – newline character
  • %p – either am or pm according to the given time value
  • %r – time in a.m. and p.m. notation
  • %R – time in 24 hour notation
  • %S – second
  • %t – tab character
  • %T – current time, equal to %H:%M:%S
  • %u – weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1
  • %U – week number of the current year, starting with the first Sunday as the first day of the first week
  • %V – The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week
  • %W – week number of the current year, starting with the first Monday as the first day of the first week
  • %w – day of the week as a decimal, Sunday=0
  • %x – preferred date representation without the time
  • %X – preferred time representation without the date
  • %y – year without a century (range 00 to 99)
  • %Y – year including the century
  • %Z or %z – time zone or name or abbreviation
  • %% – a literal % character

If you give abbreviated month name like Aug, Jan, then %b directive would be used.

Next, let us convert the epoch time to human readable format.

Let us do step by step.

First convert the epoch into localtime. As shown below.

>>> t1 = time.localtime(1534607410.058)

>>> t1

time.struct_time(tm_year=2018, tm_mon=8, tm_mday=18, tm_hour=21, tm_min=20, tm_sec=10, tm_wday=5, tm_yday=230, tm_isdst=0)

>>>

So, by using localtime() function the epoch time has been converted to time_struct format.

Next step is to convert the time_struct format to human-readable form. For this, we would use time.strftime() function. The time.strftime() takes two arguments, first is format and second is the time in struct_time format or tuple format as shown below.

>>>

>>> time.strftime(“%b %d %Y %H:%M:%S”, t1)

‘Aug 18 2018 21:20:10′

>>>

>>> time.strftime(“%m %d %Y %H:%M:%S”, t1)

’08 18 2018 21:20:10’

>>>

So far, you got the idea how to obtain epoch and human readable format.

Let us explorer different functions.

time.asctime([t])

The time.asctime() function converts the time tuple or struct_time  to a 24-character string of the following form: “Mon Jun 20 23:21:05 1994”. It returns current time if no argument gets passed.

See the example below.

>>> time.asctime()

‘Sat Aug 18 22:08:52 2018’

>>>

>>> t = time.localtime()

>>> time.asctime(t)

‘Sat Aug 18 23:07:07 2018’

>>>

So, time.asctime() is a handy thing to get the current time in human-readable format.

time.ctime([secs])

The time.ctime() converts the seconds to a 24-character string of the following form: “Mon Jun 20 23:21:05 1994”. If no argument is provided then time.ctime() returns the current time.

Let us understand by the examples. Print the current time.

>>> time.ctime()

‘Sat Aug 18 23:32:03 2018’

>>>

When t= 0

>>> time.ctime(0)

‘Thu Jan 01 05:30:00 1970′

It is supposed to print Thu, 1 January 1970 00:00:00. But due to my time zone (IST). Being GMT+05:30 the time.ctime(0) returns ‘Thu Jan 01 05:30:00 1970’

Let us give 3600 seconds as arguments.

>>> time.ctime(3600)

‘Thu Jan 01 06:30:00 1970’

>>>

Let us provide current timestamp.

>>> t=time.time()

>>> t

1534615399.061

>>> time.ctime(1534615399.061)

‘Sat Aug 18 23:33:19 2018’

>>>

Let us produce new time by using current time.

1 Hour later

>>> t=time.time() + 1*60*60

>>> time.ctime(t)

‘Sun Aug 19 00:41:28 2018’

>>> time.ctime()

‘Sat Aug 18 23:41:33 2018’

>>>

1 Hour earlier

>>> t=time.time() – 1*60*60

>>> time.ctime(t)

‘Sat Aug 18 22:41:55 2018’

>>>

time.sleep(second)

The time.sleep() function suspend the execution of the current thread for the given number of seconds. See the code example below.

import time

print time.asctime()

time.sleep(10)

print time.asctime()

Output.

python time output

I hope you enjoyed the python time chapter ?.

Leave a reply:

Your email address will not be published.

Site Footer

{"wp_error":"cURL error 60: SSL certificate problem: unable to get local issuer certificate"}