Title: Python Time
1Python Time
2(No Transcript)
3Python Time In this article, well see how
to use Pythons 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.
4The 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 000000 Coordinated Universal Time
(UTC), Thursday, 1 January 1970, To know more
about Epoch go to the website https//www.epochcon
verter.com/ gtgtgt import time gtgtgt
time.time() 1534587871.267
5Now 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. gtgtgt int(time.mktime(time.strptime(15 Aug
2018', d b Y)))
1534271400 gtgtgt gtgtgt int(time.mktime(time.strptime
(26 Jan 2018', d b Y)))
6 1516905000 gtgtgt 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.
7See the following example of gmtime() and
localtime(). gtgtgt time.gmtime() time.struct_time(
tm_year2018, tm_mon8, tm_mday18, tm_hour13,
tm_min55, tm_sec13, tm_wday5, tm_yday230,
tm_isdst0) gtgtgt gtgtgt time.localtime() time.struc
t_time(tm_year2018, tm_mon8, tm_mday18,
tm_hour19, tm_min25, tm_sec26, tm_wday5,
tm_yday230, tm_isdst0)
8gtgtgt 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. Lets get back to the
time.strptime function. The time.strptime
function has the ability to convert the human
readable date to struct_time.
9The 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. gtgtgt
time.strptime(15 Aug 2018', d b
Y) time.struct_time(tm_year2018, tm_mon8,
tm_mday15, tm_hour0, tm_min0, tm_sec0,
tm_wday2, tm_yday227, tm_isdst-1) gtgtgt gtgtgt
10gtgtgt time.strptime(15 08 2018', d m
Y) time.struct_time(tm_year2018, tm_mon8,
tm_mday15, tm_hour0, tm_min0, tm_sec0,
tm_wday2, tm_yday227, tm_isdst-1) gtgtgt gtgtgt
time.strptime(15 08 18', d m
y) time.struct_time(tm_year2018, tm_mon8,
tm_mday15, tm_hour0, tm_min0, tm_sec0,
tm_wday2, tm_yday227, tm_isdst-1) gtgtgt
11Why 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)
12- 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
13- 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 HMS
- u weekday as a number (1 to 7), Monday1.
Warning In Sun Solaris Sunday1 - U week number of the current year, starting
with the first Sunday as the first day of the
first week
14- 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, Sunday0
- 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
15If 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. gtgtgt t1
time.localtime(1534607410.058) gtgtgt t1
16time.struct_time(tm_year2018, tm_mon8,
tm_mday18, tm_hour21, tm_min20, tm_sec10,
tm_wday5, tm_yday230, tm_isdst0) gtgtgt
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. gtgtgt
17gtgtgt time.strftime(b d Y HMS, t1) Aug
18 2018 212010' gtgtgt gtgtgt time.strftime(m d Y
HMS, t1) 08 18 2018 212010 gtgtgt 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 232105 1994. It returns current time if
no argument gets passed.
18See the example below. gtgtgt time.asctime() Sat
Aug 18 220852 2018 gtgtgt gtgtgt t
time.localtime() gtgtgt time.asctime(t) Sat Aug
18 230707 2018
19gtgtgt 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 232105 1994. If no argument
is provided then time.ctime() returns the current
time. Let us understand by the examples. Print
the current time.
20gtgtgt time.ctime() Sat Aug 18 233203
2018 gtgtgt When t 0 gtgtgt time.ctime(0) Thu
Jan 01 053000 1970'
21It is supposed to print Thu, 1 January 1970
000000. But due to my time zone (IST). Being
GMT0530 the time.ctime(0) returns Thu Jan 01
053000 1970
Let us give 3600 seconds as arguments. gtgtgt
time.ctime(3600) Thu Jan 01 063000
1970 gtgtgt Let us provide current timestamp.
22gtgtgt ttime.time() gtgtgt t
1534615399.061 gtgtgt time.ctime(1534615399.061) S
at Aug 18 233319 2018 gtgtgt Let us produce new
time by using current time.
231 Hour later gtgtgt ttime.time() 16060 gtgtgt
time.ctime(t) Sun Aug 19 004128 2018 gtgtgt
time.ctime() Sat Aug 18 234133 2018 gtgtgt
241 Hour earlier gtgtgt ttime.time() 16060 gtgtgt
time.ctime(t) Sat Aug 18 224155 2018 gtgtgt
25time.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.sl
eep(10) print time.asctime()
26Output.
I hope you enjoyed the python time chapter
.
27For more Online Training Courses, Please
contact Email info_at_learntek.org USA 1734
418 2465 India 91 40 4018 1306 91
7799713624