×

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

Sklearn linear Model in Python

The most effective and reliable Python machine learning library is Sklearn. Through a Python consistency interface, it offers a variety of effective tools for statistical modeling and machine learning, including...

5 minutes read.

Python len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.

Captcha Code in Python with Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Operator Module In Python

Introduction The operator module is used for performing operations using methods rather than utilizing operators in Python code. The operator module provides several methods for performing the operations.  The operator module contains...

7 minutes read.

How to Parse JSON in Python

JSON The JSON, the Java Script Object Notation, is an open standard file designed for data interchange; it is light-weighted text. The JSON uses human-readable text to store and transmit the...

4 minutes read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

First Unique Character in a String Python

This article aims to introduce you to strings and how to create a string in Python, and then we will solve a simple yet exciting DSA (Data Structures and Algorithms)...

3 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Python AIOHTTP

Python 3.5 introduced some new syntax that makes it simpler for developers to make asynchronous programmes and packages. Aiohttp, an HTTP client/server for asyncio, is one such package. In essence,...

3 minutes read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.

Covariance in Python

Covariance is defined as the estimate of the difference of change between two variables or more variables. It defines the changes of two variables together. In Python, The covariance can...

2 minutes read.

Python program to convert Celsius into Fahrenheit

Python program to convert Celsius into Fahrenheit This program explains how we can take the temperature in Celsius and convert them into Fahrenheit. Celsius Celsius, also known as centigrade, is a measurement unit...

1 minute read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Standard Scaler in SKLearn

The sci kit learns in python is a library thatch is used in machine learning which is used to work on data modeling.It is only focused on the data modeling,...

4 minutes read.

Python program to perform the arithmetic operation

Python program to perform the arithmetic operation This program will write a code to perform some basic arithmetic operations like addition, subtraction, multiplication, exponent, modulus, and division. Here we first need...

2 minutes read.

Python Knapsack problem

Python Knapsack problem Before we dig down about Knapsack problems in Python, first let's have a look at what is actually a knapsack problem. What is a knapsack problem? A problem from the...

5 minutes read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.