Header Ads

Header ADS

Python Time Module


Python time module.

In this article we will discuss time module , its methods / function and its general application.

time module ?

time module is a built-in library with python all versions and it is used for working with time related tasks.

A general Application  od time module is suppose you created a ATM machine GUI project and you want to record time on which the user withdraw the money then the time module is helpful for your project and their are many more basic application of it .

time module have lots of methods/functions which helps in time manipulation and In this article we discuss some common and most used functions.


As i earlier said time module is a built in library so we didn't  need to install it , just import it .

  import time   # import time module 





After importing time lets see some commonly used time related functions.





1. time.time( ) :
    
This function returns the number of seconds passed since epoch.
 ( for most of the operating system the epoch time is january 1, 1970, 00:00:00 at UTC )

So what is epoch ?
 
In computer context, an epoch is the date and time relative to which a computer's clock &        Timestamp values are determined.
    
The important concept to grasp here is that, when dealing with Python time, you’re considering a period of time identified by a starting point. In computing, you call this starting point the epoch.

The epoch, then, is the starting point against which you can measure the passage of time.

 For example, if you define the epoch to be midnight on January 1, 1970 UTC—the epoch as defined     on Windows and most UNIX systems—then you can represent midnight on January 2, 1970 UTC as     86400 seconds since the epoch.

Code to illustrate :
       			 
import time

# returns epoch time 

seconds = time.time()

print(seconds)
 
OUTPUT :

  1593627527.3428385   time( ) function returns the epoch time.






2. time.ctime( ) :

ctime means current time.
It takes number of seconds passed since epoch as an argument & returns  a string representing local time.
you can also leave the brackets empty because the ctime method takes epoch time by default, so You don't need to pass it .

Code to illustrate :
       			 
import time

# returns epoch time 
seconds = time.time()
print(seconds)

# -------------------------------------------------
current_time_1 = time.ctime(seconds)

current_time_2 = time.ctime()

print("Current time is  ",current_time_1)

print("Current time is  ",current_time_2)
 
OUTPUT :
       			
1593627527.3428385
Current time is   Wed Jul  1 11:18:47 2020
Current time is   Wed Jul  1 11:18:47 2020
As you can see  output of both variable current_time_1 and current_time_2 is same.






3. time.sleep( ) :

The sleep( ) method suspends/ delays the program execution for a given number of seconds passed as an argument in  it.

Code to illustrate :
       			 
import time

print("going for a 4 second sleep")

time.sleep(4)

# stops the program execution for 3 seconds
print("ohh i wake up")
 
OUTPUT :

going for a 4 second sleep ohh i wake up




TImestamp ?

The value returned by ctime() function is also know as timestamp.
The timestamp representation have some following structure as shown below.
  1.  Day of week : eg , mon (Monday )
  2.  Month of year : eg, jul ( July)
  3. Day of month : eg. 1
  4. Hours, minutes & seconds using 24 hour clock notation
  5. year : eg, 2020




time.struct_time Class

Some of the time module methods used a struct_time object as an argument for various time manipulation.
using struct_time class we can access a specified value of given time like Hour, second, Day instead of a full stinf timestamp value.

struct_time example .

Basically struct_time is a tuple containing 9 values.

time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, 
                    tm_hour=6, tm_min=35, tm_sec=17, 
                    tm_wday=3, tm_yday=361, tm_isdst=0)

IndexAttributeValues
0tm_year0000, ...., 2018, ..., 9999
1tm_mon1, 2, ..., 12
2tm_mday1, 2, ..., 31
3tm_hour0, 1, ..., 23
4tm_min0, 1, ..., 59
5tm_sec0, 1, ..., 61
6tm_wday0, 1, ..., 6; Monday is 0
7tm_yday1, 2, ..., 366
8tm_isdst0, 1 or -1

The values (elements) of the time.struct_time object are accessible using both indices and attributes.

The method like gmtime( ) and localtime( ) returns the struct_time tuple.



4. time.localtime( ) :

The localtime() function takes the number of seconds passed since epoch as an argument and returns struct_time in local time.

as Like other function passing epoch as an argument is optional in it.

Code to illustrate :
       			 
import time
epoch = time.time()
print(epoch)

result = time.localtime(epoch) 
print(result)
print("Current Day ",result.tm_mday)

print("Current Year ",result.tm_year)


 
OUTPUT :

1593638249.778398
time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=14, tm_min=17, tm_sec=29, tm_wday=2, tm_yday=183, tm_isdst=1)
Current Day 1
Current Year 2020





5. time.gmtime( ) :

The gmtime() function takes the number of seconds passed since epoch as an argument and returns struct_time in UTC

as Like other function passing epoch as an argument is optional in it.

What is UTC ?

UTC is the time standard against which all the world’s timekeeping is synchronized (or coordinated). It is not, itself, a time zone but rather a transcendent standard that defines what time zones are.

UTC time is precisely measured using astronomical time, referring to the Earth’s rotation, and atomic clocks.

Time zones are then defined by their offset from UTC. For example, in North and South America, the Central Time Zone (CT) is behind UTC by five or six hours and, therefore, uses the notation UTC-5:00 or UTC-6:00.


Code to illustrate :
       			 
       			 
import time
epoch = time.time()
print(epoch)

result = time.gmtime(epoch) 
print(result)
print("Current Day ",result.tm_mday)

print("Current Year ",result.tm_year)
 

OUTPUT :

1593638506.1140974
time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=21, tm_min=21, tm_sec=46, tm_wday=2, tm_yday=183, tm_isdst=0)
Current Day 1
Current Year 2020





6. time.mktime( ) :

The mktime( ) function converts the struct_time object into number of seconds after epoch
It is basically a reverse function for gmtime( ) and localtime( ).

Code to illustrate :
       			 
import time
epoch = time.time()
print(epoch)

result = time.gmtime(epoch) 
print(result)

new_epoch = time.mktime(result)
print(new_epoch)


 


OUTPUT :

1593638859.8717556

time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=21, tm_min=27, tm_sec=39, tm_wday=2, tm_yday=183, tm_isdst=0)

1593667659.0






7. time.asctime( ) :
 
it takes struct_time or tuple containing 9 values like struct_time and returns a string representing it.


Code to illustrate :
       			 
import time
epoch = time.time()
print(epoch)

result = time.gmtime(epoch) 
print(result)


timestamp = time.asctime(result)
print('Time stamp is ',timestamp)


 
OUTPUT :

1593639179.9078994

time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=21, tm_min=32, tm_sec=59, tm_wday=2, tm_yday=183, tm_isdst=0)

Timestamp is Wed Jul 1 21:32:59 2020









Thanks for Visiting here.
If you have any doubts and feedback please comment , we will try to reply you as fast as we can.


No comments

Powered by Blogger.