Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - time Module

Python

python logo

In this tutorial we will learn about time module in Python.

We learned about modules in the Python - Modules tutorial. Feel free to check that out.

In one of the previous tutorial Python - datetime Module we learned how to work with date and time using the datetime module.

Importing the time module

To work with time we have to first import the time module by writing the following line.

import time

Display the current time

To display the current time using the time module we have to write the following code.

# import the module
import time

# get time
currTime = time.time()

# output
print(currTime)

The above Python code will give us a similar output.

1540080000.431106

The value we get is a floating-point number in seconds.

The time.time() returns the current time in seconds since the Epoch.

Epoch = 1st Jan 1970 12:00 am

The localtime method

We can convert the current time from seconds to time tuple using the localtime method.

In the following Python program we are converting the current time into time tuple.

# import the module
import time

# get time
currTime = time.time()

# local time tuple
lt = time.localtime(currTime)

# output
print(lt)

The above code will give us the following output.

time.struct_time(tm_year=2018, tm_mon=10, tm_mday=21, tm_hour=7, tm_min=18, tm_sec=48, tm_wday=6, tm_yday=294, tm_isdst=0)

The output is in struct_time structure and the attributes are in the following format.

AttributeValue
tm_yearFull year
Example: 2018
tm_monMonth
Value: 1 to 12
January = 1
...
December = 12
tm_mdayDay
Value: 1 to 31
tm_hourHour
Value: 0 to 23
tm_minMinute
Value: 0 to 59
tm_secSecond
Value: 0 to 61 (60 or 61 = leap seconds)
tm_wdayWeekday
Value: 0 to 6
where, 0 = Monday
tm_ydayDay count of the year
Value: 1 to 366
where, Jan 1st = 1
...
Dec 31st = 365
If leap year then, Dec 31st = 366
tm_isdstDaylight Saving Time
Value: -1, 0, 1,
-1 = library determines DST

The asctime method

We use the asctime method to coverter the time from seconds to human readable form.

This method takes a time-tuple and returns a human readable 24-character string like Sun Oct 21 07:35:33 2018.

In the following Python program we are converting the current time into human readable form.

# import the module
import time

# get time
currTime = time.time()

# local time tuple to human readable form
lt = time.asctime(time.localtime(currTime))

# output
print(lt)

The above code will give us a similar output.

Sun Oct 21 07:35:33 2018

The gmtime method

We use the gmtime method to get a time-tuple containing UTC time. This method takes time in seconds.

In the following Python program we are getting the UTC time using the gmtime method.

# import the module
import time

# get time
currTime = time.time()

# local time tuple
l = time.gmtime(currTime)

# output
print(l)

The above code will give us a similar output.

time.struct_time(tm_year=2018, tm_mon=10, tm_mday=21, tm_hour=10, tm_min=2, tm_sec=48, tm_wday=4, tm_yday=294, tm_isdst=0)

When using gmtime method we will always get tm_isdst=0.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT