Python date and time

function data and time

Python function  Time & Datetime are the primary Python modules to program date and time.  therefore ,this tutorial, we’ll explore the Python time module,and  will see examples and in addition, we will write code to perform time operations in Python.

in addition ,Python, time and datetime both are objects, not a plain string or a timestamp. therefore , So to handle them in a program, therefore ,you need to import the respective module or the class from the module.

therefore, Also, while programming with time and datetime classes, in addition ,you may need to set an appropriate time zone. Hence, therefore , we’ll cover the time zone related functionality as well in this tutorial.

in addition ,Let’s begin this tutorial and explore the essential Python time functions, structures, and finally their usage.

tick in Python

A tick refers to a time interval which is a floating-point number measured as units of seconds. therefore ,In python, in addition ,the time instants are counted since 12 AM, 1st January 1970. therefore ,The function time() of the module time returns the total number of ticks spent since 12 AM, 1st January 1970. therefore ,A tick can be seen as the smallest unit to measure the time.

therefore see a example:

import time;  

#prints the number of ticks spent since 12 AM, 1st January 1970  

print(time.time()) 

output :

1545124460.9151757

 

print(time.strftime(“%Y-%m-%d %H:%M:%S %Z”, now))

Output –

Sun Sep 10 01:36:28 2017

17/09/10 01:36

Sun Sep 10 Sun Sep 10

01:36:28 2017

01 AM

20170910 01:36:28 India Standard Time

In the first few lines, we are calling two time-related functions. therefore , in addition , The localtime() function creates a struct_time object using the time argument. And therefore in addition  the asctime() function converts a struct_time object into a time string.

Thereafter, in addition , you can see how the strftime() function is slicing up the time and in addition therefore return a formatted string.

Working with date and time using Pandas

While working with data, encountering time series data is very usual. Pandas is a very useful tool while working with time series data.

Pandas provide a different set of tools using which we can perform all the necessary tasks on date-time data. Let’s try to understand with the examples discussed below.

import
pandas as pd

 

 

# Create dates dataframe with frequency  

data
=
pd.date_range(
'1/1/2011'
, periods
=
10
, freq
=
'H'
)

 

 

data

Output:

Printing the calendar of whole year

The prcal() method of calendar module is used to print the calendar of the whole year. The year of which the calendar is to be printed must be passed into this method.

Example

import calendar  
#printing the calendar of the year 2019

calendar.prcal(2019)  

The calendar module

Python provides a calendar object that contains various methods to work with the calendars.

Consider the following example to print the Calendar of the last month of 2018.

Example

import calendar;  
cal = calendar.month(2018,12
#printing the calendar of December 2018
print(cal)  

Comparison of two dates

We can compare two dates by using the comparison operators like >, >=, <, and <=.

Consider the following example.

Example
from datetime import datetime as dt  

#Compares the time. If the time is in between 8AM and 4PM, then it prints working hours otherwise it prints fun hours  

if dt(dt.now().year,dt.now().month,dt.now().day,8)<dt.now()<dt(dt.now().year,dt.now().month,dt.now().day,16):  

    print(“Working hours….”)  

else:  

    print(“fun hours”

Output:

fun hours

 

The datetime Module

The datetime module enables us to create the custom date objects, perform various operations on dates like the comparison, etc.

To work with dates as date objects, we have to import datetime module into the python source code.

Consider the following example to get the datetime object representation for the current time.

Example
import datetime;  

#returns the current datetime object   

print(datetime.datetime.now())  

Output:

2018-12-18 16:16:45.462778

 Time.Time() Function

The time() is a core function of the time module. It measures the number of seconds since the epoch as a floating point value. Let’s take a look:

The time() function as shown in the above example returns time in Decimal format. It could be useful for storing or comparing dates but not suitable for displaying in reports.

Another common application of Python time function is to measure the elapsed Wall-clock time between two points.

Note – Wall clock time is the actual time lapsed in completing a job. It is same like timing your job with a stopwatch.

Time.Clock() Function

Unlike the time.time() function which computes the wall clock time, the time.clock() returns the processor clock time.

We can use time.clock() for performance testing/benchmarking and time.time() to schedule something.

The output of the clock() function reflects the right time taken by the program. And it is more accurate than its counterpart.

However, its exact behavior may vary with the OS you are using. It could return process time ignoring the time to sleep. And in some cases, it could count the time elapsed even when the process was inactive.

Note – The processor clock does not tick if a program is in the sleep state.

Let’s see an example to demonstrate what happens when we use both the time functions.

Example –

Output –

Please note that Python 3.3 has deprecated the clock() function and introduced two functions i.e. time.perf_counter() and time.process_time().

time.perf_counter() – It uses a clock with the highest precision to measure a short duration and factors in the process sleep time.

time.process_time() – Its output is the sum of the system and user CPU time of the current process. It ignores the time elapsed during sleep.

Time.Ctime() Function

It takes the time in “seconds since the epoch” as input and translates into a human readable string value as per the local time. In case of no argument, it calls the time() function and returns the current time.

We can even use it intuitively. Check out the below example.

Example –

Output –

 

https://javatpoint.com

%d bloggers like this: