Change language

Time Functions in Python | Set to 1 (time (), time (), sleep () & # 8230;)

|

Operations by time:

1. time () : — this function is used to count the number of seconds since the beginning of an epoch .

2. gmtime (sec) : — this function returns a structure with 9 values, each of which is a temporary attribute in a sequence. It converts seconds to time attributes (days, years, months, etc.) Until the specified seconds from the beginning of the epoch. If no seconds are specified, the time is calculated up to the present time. The structure attribute table is shown below.

 Index Attributes Values ​​0 tm_year 2008 1 tm_mon 1 to 12 2 tm_mday 1 to 31 3 tm_hour 0 to 23 4 tm_min 0 to 59 5 tm_sec 0 to 61 (60 or 61 are leap -seconds) 6 tm_wday 0 to 6 7 tm_yday 1 to 366 8 tm_isdst -1, 0, 1 where -1 means Library determines DST 

# Python code to demonstrate how it works
# time () and gmtime ()

  
# importing a time module for time operations

import time

  
# using time () to display time since epoch

print ( "Seconds elapsed since the epoch are: " , end = " ")

print (time.time ())

 

 
# using gmtime () to return an attribute structure time

print ( "Time calculated acc ... to given seconds is: " )

print (time.gmtime ())

Output:

 Seconds elapsed since the epoch are: 1470121951.9536893 Time calculated acc. to given seconds is: time.struct_time (tm_year = 2016, tm_mon = 8, tm_mday = 2, tm_hour = 7, tm_min = 12, tm_sec = 31, tm_wday = 1, tm_yday = 215, tm_isdst = 0) 

3.asctime : — this function takes a string with a time attribute, generated by gmtime (), and returns a string of 24 characters for the time .

4.ctime (sec) : — this function returns a 24-character temporary string, but takes seconds as an argument and calculates the time until the specified seconds . If no argument is passed, time is calculated to date.

# Python code to demonstrate how it works
# asctime () and ctime ()

 
# import a time module for time operations

import time

 
# time initialization with gmtime ()

ti = time.gmtime ()

 
# using asctime () to display the time acc. by the mentioned time

print ( "Time calculated using asctime () is: " , end = " ")

print (time.asctime (ti))

 

 
# using ctime ( ) to display the time string using seconds

print ( " Time calculated using ctime () is: " , end = " ")

print (time.ctime ())

Output:

 Time calculated using asctime () is: Tue Aug 2 07:47:02 2016 Time calculated using ctime () is: Tue Aug 2 07:47:02 2016 

5. sleep (sec) : — This method is used to track program execution over the time specified in the arguments.

# Python code to demonstrate how it works
# sleep ()

 
# import a time module for time operations

import time

 
# using ctime () to display the current time

print ( "Start Execution:" , end = "")

print (time.ctime ())

  
# using sleep () to call execution

time.sleep ( 4 )

 
# using ctime () to display the current time

print ( "Stop Execution:" , end = "")

print ( time.ctime ())

Output:

 Start Execution: Tue Aug 2 07:59:03 2016 Stop Execution: Tue Aug 2 07:59:07 2016 

Link:
http://www.tutorialspoint.com/python/python_date_time.htm

This article courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically