×

Python Time Library

  • We will consider various functions given by the python module library with examples.
  • This python time module helps to work on time in python to get the current time.
  • Before going with the example we have to import the library.

Import the time module:

  • There is no need to install any module we have just importedthem using the import statement.
import time

Epoch:

  • Where the time starts it is called epoch which is platform-dependent.
  • The epoch is January 1, 1970, 00:00:00 for windows and most Unix systems.
  • We can check the epoch using time.gmtime(0).
Import time
Print( time.gmtime(0) )

Output:

Time.strut_time (tm_year=1970, tm_mon=1,  tm_mday=1, tm.hour=0, tm_min=0,tn_sec=0,tm_wday=3, tm_yday=1, tm_isdst=0 )

Time.ctime

  • It is a function that returns a string of 24 characters, and time.
  • This function takes seconds as an argument and computes until the provided time.
  • If the argument is not provided then it will consider the current time and compute till the current time.

Consider an example of getting time in the form of string:

import time
/ / getting current time by passing 
/ / seconds passes since epoch
sec = 1545925769.9618232
current = time.ctime ( sec )
Print(“current time:”,current

Output:

Current time: Tue Aug 22 12:33:54 2022

Program Delaying Execution

  • The program execution can be delayed using time.sleep ( ) method.
  • For a specified amount of time program execution can be delayed by using this function.

Consider an example to implement the above scenario:

import time
for i in range (5):
/ / using the sleep method 
	time.sleep(2)
	print(i)

Output:

0
1
2
3
4

Time.struct_time Class

  • This function helps in accessing the local time which is non-epochal timestamps.
  • It returns a tuple with a name, where both index and attribute name is used to access the value.
  • The objects contain the attribute as follows:
IndexAttribute nameValues
0tm_year0000, ….., 9999
1tm_mon1,2,3,4,5,…. 11,12
2tm_mday1,2,3,…,30,31
3tm_hour0,1,2,3,4,….,21,22,23
4tm_min0, 1, 2, 3, …, 57, 58, 59
5tm_sec0, 1, 2, 3, …., 60, 61
6tm_wday0, 1, 2, 3, 4, 5, 6 ;6 is Sunday
7tm_yday1, 2, 3, 4, …, 365, 366
8tm_isdst0, 1 or -1

Python time.localtime ( )

  • This function returns struct time in local time, it takes a number of seconds from epoch as an argument.
  • If the argument is not given then time.time( ) is used.
  • Let us assume a program for the above scenario:
/ /import the time module
import time
/ / convert the time 
/ /from the epoch  
/ / time.strurct_time object in local time
obj = time.localtime(1545925769)
print(“Result:”,obj)
print(“/n Year:”,obj.tm_year)
print(“Hour:”obj.tm_hour)

Output:

Result: time.struct_time( tm_year=2022, tm_mo = 8, tm_mday = 3, tm_hour = 16, tm_min = 15,tm_sec = 8,tm_wday = 1,tm_yday = 215, tm_isdst = 0)
Year: 2022
Hour: 15

Python time.gmtime( )

  • This function returns the struct_time in UTC.
  • It takes a number of seconds from the epoch as an argument.
  • This function is used to convert seconds from epoch to struct time.
  • If the argument is not given then time.time( ) is used.
  • Let us consider an example program:
/ /importing the time module
import time
/ / convert the time in seconds
/ /since the epoch to a 
/ / time.struct_time object in local time
obj = time.gmtime(1545925769)
print(“Result:”,obj)
print(“/n Year:”,obj.tm_year)
print(“Hour:”obj.tm_hour)

Output:

Result: time.struct_time( tm_year=2022, tm_mo = 8, tm_mday = 3, tm_hour = 16, tm_min = 15,tm_sec = 8,tm_wday = 1,tm_yday = 215, tm_isdst = 0)
Year: 2022
Hour: 15

Python time.strptime ( )

  • This method converts the time which is in form of a string to struct time.
  • Let us consider a program for the above case:
/ /importing the time module
import time
/ / declare a variable with current date
time_string = “22 Aug 2022”
obj = time.strptime(time_string, “%d %B,%Y”)
/ / printing the object
print(obj)

Output:

time.struct_time( tm_year=2022, tm_mo = 8, tm_mday = 22, tm_hour = 0, tm_min = 0,tm_sec = 0,tm_wday = 1,tm_yday = 172, tm_isdst = -1)

Python time.mktime ( )

  • This function returns the seconds from the beginning of the epoch from local time.
  • The struct_time is taken as argument in the mktime( ) function.
  • The mktime( ) function is a inverse of localtime( ) function.
  • Let us consider a program for the above function:
/ / import the python time module
import time
/ / declaring a variable 
time = 1627988053
/ / returning the struct_time
obj1 = time.localtime( time ) 
print(“obj1:”, obj1 )
 / / returning the time in seconds from struct_time
obj = time.mktime( time )
print(“obj:”, obj )

Output:

Obj1 :time.struct_time( tm_year=2022, tm_mo = 8, tm_mday = 22, tm_hour = 0, tm_min = 0,tm_sec = 0,tm_wday = 1,tm_yday = 172, tm_isdst = -1)
Obj :1627988053

Python time.asctime( )

  • This function returns a string representing the time given.
  • The function asctime( ), takes struct_time as an argument.
  • It converts a struct_time in to string object which represents the time.
  • The return string is represented as:
    Day Mon Date Hour: Min: Sec Year
  • Let us consider an example program for the above function.
/ / import the python time module
import time
/ / declaring a variable 
sec = 1627988053
/ / convert the struct time 
/ / into string in the form “Day Mon Date Hour: Min: Sec Year”
/ / using the asc.time( ) function 
obj = time.gmtime( sec ) 
time_str = time.asctime( obj )
print( time_str)
/ / convert the struct time 
/ / into string in the form “Day Mon Date Hour: Min: Sec Year”
/ / using the asc.time( ) function 
Obj1 = time.localtime( sec )
time_str = time.asctime( obj )
print(time_str )

Output:

Mon Aug 22 10:23:09 2022
Mon Aug 22 10:23:09 2022

Python time.strftime( )

  • This function returns a string representing the time given.
  • The function strftime( ), takes struct_time as an argument.
  • The string which the function returns is based on the format codes.
  • Let us consider a program for the above function:
/ / import the python time module
import time
/ / declaring a variable
obj = time.local time( )
/ / converting the time in a string using the strf.time ( ) function
stringobj = time.strftime ( “%m %d %Y, %H : %M : %S “, obj )
/ / displaying the time
print( stringobj )

Output:

08/22/2022, 09: 23: 34

Related Topics

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

Arithmetic Expressions in Python

What is Python Expression? Expressions are collections of operands and operators. Python expressions are translated by the Python interpreter into some value or outcome. In Python, an expression is made up...

13 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.

Exit program in python

Exit program in python The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback...

2 minutes read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

Python If-else statement

In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing...

4 minutes read.

Check Palindrome in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

Python Packages

A package is a collection of Python modules that share a similar namespace and are generated by putting all of the modules in a single directory with certain special files...

6 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.

Application to get live USD/INR rate Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Artificial intelligence mini projects ideas in python

Artificial intelligence "The human race started from the invention of the wheel", and today we are about to advance to the Industrial level 4.0, where machines can work, talk, think and...

6 minutes read.

After Python, What Should I Learn

Python is a programming language used to create websites, software, and other projects. It is easy to code and easy to learn. After learning Python, there are many different directions...

4 minutes read.

Python program to sort the array element into ascending order

Python program to sort the array element into ascending order In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a...

2 minutes read.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

Python Support

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.