Found 27104 Articles for Server Side Programming

How to convert timestamp string to datetime object in Python?

Vikram Chiluka
Updated on 27-Aug-2023 12:36:51

22K+ Views

In this article, we will show you how to convert a timestamp string to DateTime object in Python. Below are the various methods to accomplish this task − Using datetime.fromtimestamp() function Using datetime.fromtimestamp() & strftime Using datetime.strptime() Convert timestamp to a date time object with format codes with mixed text Using datetime.fromtimestamp() function To obtain a date from a UNIX timestamp, use the datetime module's fromtimestamp() function. This function accepts a timestamp as input and returns the datetime object corresponding to that timestamp. Syntax fromtimestamp(timestamp, tz=None) Algorithm (Steps) Following are the Algorithm/steps to be followed to perform ... Read More

How to measure elapsed time in python?

Rajendra Dharmkar
Updated on 07-Jun-2020 17:37:24

2K+ Views

To measure time elapsed during program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes. exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1) # CPU seconds elapsed (floating point)OutputThis will give the output −Time elapsed:  1.2999999999999123e-05You can also use the time module to get proper statistical analysis of a code snippet's execution time.  It runs the snippet multiple times and then it tells you how long the shortest run took. You can use it as follows:Exampledef f(x):   return x * x ... Read More

How to compare Python string formatting: % with .format?

Rajendra Dharmkar
Updated on 19-Feb-2020 07:33:31

184 Views

% can either take a variable or a tuple. So you'd have to be very explicit about what you want it to do. For example, if you try formatting such that −Examplemy_tuple = (1, 2, 3) "My tuple: %s" % my_tuple You'd expect it to give the output: My tuple: (1, 2, 3)OutputBut it will throw a TypeError. To guarantee that it always prints, you'd need to provide it as a single argument tuple as follows −"hi there %s" % (name, )   # supply the single argument as a single-item tupleRemembering such caveats every time is not that easy ... Read More

How to get the timing Execution Speed of Python Code?

Rajendra Dharmkar
Updated on 19-Feb-2020 07:44:46

2K+ Views

To measure time of a program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes. exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)OutputThis will give the output −Time elapsed:  0.0009403145040156798You can also use the timeit module to get proper statistical analysis of a code snippet's execution time.  It runs the snippet multiple times and then it tells you how long the shortest run took. You can use it as follows:Exampledef f(x):   return x * x ... Read More

How to find if 24 hrs have passed between datetimes in Python?

Rajendra Dharmkar
Updated on 19-Feb-2020 07:44:15

2K+ Views

To find out if 24 hrs have passed between datetimes in Python, you will need to do some date math in Python. So if you have 2 datetime objects, you'll have to subtract them and then take the timedelta object you get as a result and use if for comparision. You can't directly compare it to int, so you'll need to first extract the seconds from it. examplefrom datetime import datetime NUMBER_OF_SECONDS = 86400 # seconds in 24 hours first = datetime(2017, 10, 10) second = datetime(2017, 10, 12) if (first - second).total_seconds() > NUMBER_OF_SECONDS:   print("its been over a day!")OutputThis ... Read More

How do I get time of a Python program's execution?

Rajendra Dharmkar
Updated on 12-Jun-2020 08:43:33

358 Views

To measure time time of a program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes. Exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)OutputThis will give the output −Time elapsed:  0.0009403145040156798You can also use the timeit module to get proper statistical analysis of a code snippet's execution time.  It runs the snippet multiple times and then it tells you how long the shortest run took. You can use it as follows −Exampledef f(x):   return x ... Read More

How to measure time with high-precision in Python?

Rajendra Dharmkar
Updated on 12-Jun-2020 08:40:43

4K+ Views

To measure time with high precision, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes.Exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)OutputThis will give the output −Time elapsed:  0.0009403145040156798Note that different systems will have different accuracy based on their internal clock setup (ticks per second). but it's generally at least under 20ms. Also note that clock returns different things on different platforms. On Unix, it returns the current processor time as a floating point number expressed ... Read More

How to get min, seconds and milliseconds from datetime.now() in Python?

Pranav Indukuri
Updated on 02-Nov-2023 02:38:08

14K+ Views

In this article, we will look at different ways to get minutes, seconds, and milliseconds from datetime now in Python. The datetime.now() method is used to get the current minutes, seconds, and milliseconds. This is defined under the datetime module. Syntax The syntax of now() method is as follows − datetime.now() Returns the current date and time in time format. Using the datetime.now() and epoctime() Here we use the strftime() method which is provided by the datetime module. We have used the datetime.now() method to get the current date. Then we format this date by using the strftime() method. ... Read More

How to get current time in milliseconds in Python?

Pranav Indukuri
Updated on 23-Aug-2023 13:07:48

73K+ Views

In this article, we will discuss the various way to retrieve the current time in milliseconds in python. Using time.time() method The time module in python provides various methods and functions related to time. Here we use the time.time() method to get the current CPU time in seconds. The time is calculated since the epoch. It returns a floating-point number expressed in seconds. And then, this value is multiplied by 1000 and rounded off with the round() function. NOTE : Epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows ... Read More

How to prepare a Python date object to be inserted into MongoDB?

Rajendra Dharmkar
Updated on 12-Jun-2020 08:39:05

1K+ Views

You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo.Examplefrom pymongo import MongoClient # This will try to connect to MongoDB on the default port and host client = MongoClient() db = client.test_database # Insert the given dictionary to the objects collection: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")OutputThis will give the output −Object inserted!Note − Always use datetime.datetime.utcnow(), which returns ... Read More

Advertisements