Python Datetime Utcnow

Posted on  by admin
August 11, 2018

The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation. Aware and Naive Objects ¶. Python’s standard library and some other low-level modules have near-complete date, time and timezone functionality, but don’t work very well from a usability perspective: Too many modules: datetime, time, calendar, dateutil, pytz and more. Too many types: date, time, datetime, tzinfo, timedelta, relativedelta, etc. Datetime.utcnow uses OS provided values. Datetime.utcnow uses gettimeofday (2) or time.time on Python 2 (and gmtime (3) to convert the result into broken-down time). Time.time uses gettimeofday (2), ftime (3), time (2). Newer CPython versions may use clockgettime (2), GetSystemTimeAsFileTime. Today および utcnow よりもこの関数を使う方が好ましいです。 classmethod datetime.utcnow ¶. Tzinfo が None である現在の UTC の日付および時刻を返します。 このメソッドは now と似ていますが、 naive な datetime オブジェクトとして現在の UTC 日付および時刻を返し. May 14, 2020 We’ll need two Python libraries for this, pytz and Python’s datetime. From datetime import datetime now = datetime.now - Returns a datetime object in your current timezone, but it is not.

Introduction

In Python, the datetime module can be used to get date and time. This is good but there is something annoying: the milliseconds or the fraction that gets printed by default. How can we get rid of that ? There are a couple of ways to do the trick.

Let us see…

Example code

The following code snippet shows the default behavior…

2
4
6
8
importdatetime
# Get current date and time
x=dt.replace(microsecond=0)

Splitting the string using the dot as a delimiter works perfectly…

2
4
6
8
10
12
14
importdatetime
importre
# Get current date and time
# Match datetime string then dot then milliseconds
# milliseconds

That was it for today. I hope it was useful. Thanks for visiting.

More from my site

In this article we are going to look at how we can see time in milliseconds, i.e. one thousandth (1000th) of a second, using Python. This could be for pure curiosity, benchmarking code or to get a very, very accurate time of day – whatever the reason, the focus of the article is how to get the time in milliseconds, rather than what it’s to be used for once we have it.

Method 1: Milliseconds and Fixed Time

The first method we are going to look at is based on measuring how much time has passed since a fixed point in time, which we call the ‘epoch’. For Python, and computing in general, the epoch is the point where time starts and the passage of time is therefore measured against this. Whilst the epoch can vary depending on operating system, for Unix and Windows it is midnight on January 1st 1970 in UTC time (Greenwich Mean Time without Daylight Savings time adjustments). If you want to find out what the epoch time is on a given platform you can use:

As I am using a Windows 10 machine the above output just confirms that the start time, or the epoch for Python on my machine is January 1, 1970, 00:00:00.

Using time.time()

The time Python Standard Library can be used to return the time in seconds since the epoch as a floating point number:

This essentially gives us the number of seconds that have passed since the epoch, so to get the time in milliseconds we have to multiply the value returned by 1000. For aesthetics we can round the number and convert to an integer:

Whilst this is a good, basic way of getting time, if you are measuring time in milliseconds you are probably looking for a level of precision this does not provide. Unfortunately, the time.time is designed to get time in seconds, so can not guarantee that the system you are using will return a time with a precision greater than that i.e 1 second.

time.time_ns()

Appreciating this need for more accuracy, Python versions 3.7 and later incorporate an additional time function that measures time to the nanosecond – a thousand -millionth of a second. You may, like me, just need a moment to think about that – a thousand-millionth of a second…

So, whilst this method clearly has the precision we are looking for, to get the time in milliseconds we still need to divide the number returned by 1,000,000 (i.e convert nanoseconds to milliseconds):

As the function automatically returns an integer we do not need to worry about rounding or converting.

Method 2: Milliseconds and Relative Time

If our aim is to measure speed in milliseconds, for benchmarking code for example, Python has multiple functions in the time library that can specifically help with this. We are going to look at two, in particular, which can be used to measure time in nanoseconds so give us the precision we require.

Unlike the examples in Method 1, neither of these functions have a defined relationship to the real-world time – the epoch, they look at what we call relative time. The value they return is not defined against any starting point, but can be used to measure time intervals – such as how long it takes to run a task or specific piece of code. .

time.monotonic_ns()

This function is used to get the value of a monotonic clock, a clock that can not go backwards and is not affected by system clock updates. As the return value is undefined, i.e we don’t know what starting point it relates to, it has limited use on it’s own. However, we can sandwich some code between consecutive calls to measure the time between the calls and therefore how long it took to execute our code.

In the example below, we will print the start monotonic time and the finish monotonic time for illustration purposes as they have no value on their own. We can however use them to measure the time between the two calls to see how long it took for Python to perform the print function. As the return value is in nanoseconds, we have to divide by 1,000,000 to get our millisecond response time

The output is:

time.perf_counter_ns()

This method is similar to the time.monotonic function previously outlined but, as the name suggests, is a performance counter. It does not include time elapsed during sleep and is system-wide. This returns the absolute value of the counter but as this return is undefined, as with the time.monotonic function, it has limited use on it’s own.

So, as with the time.monotonic function we can use consecutive calls to measure time between the calls, and as the return is in nanoseconds we have to divide by 1,000,000 to get milliseconds:

The output is:

With both the time.perf_counter and time.monotonic functions, the start and finish times we have printed are irrelevant on their own, because we do not know what they relate to. Their only use is to show us the time elapsed between the calls.

Method 3: Milliseconds and Time of Day

Finally, we are going to look at how we can get Python to tell us the time of day in milliseconds, and to do this we will be using the Python Standard Library datetime.

datetime.datetime.now()

This function returns the current system date and time with the following attributes:

year, month, day, hour, minute, second, microsecond

If we just call the function by itself it will show us the current date and time at my location, down to the microsecond (one millionth of a second) :

The above output tells me current local time is

  • Year: 2021
  • Month: 2 (February)
  • Day: 18
  • Hour: 12
  • Minutes: 21
  • Seconds: 7
  • Microseconds: 163392

The thing to bear is that the micorsecond attribute being returned is always in relation to the seconds attribute.

Now if we want to convert the microseconds to milliseconds we have to divide by 1000, we can also use a print statement to help format the function for readability:

The output is:

The first line of the output prints the current date and time, the second line prints the number of milliseconds at this point.

Python Datetime Utcnow With Timezone

datetime.datetime.utcnow()

A slight variation on the previous example is using utcnow() instead of just now(). UTC refers to time in Coordinated Universal Time or Greenwich Mean Time(GMT) and is useful to calculate absolute time differences without the confusion of time zones. As I live in London, there is no difference in my code between now() and utcnow() but we will see it in action anyway.

If we call the function without any formatting we get a similar output to what we saw before, so the attributes are the same :

We could get our milliseconds as we did with the now() function, and it would work just as well, but let’s have a look at another method. In this example we are going to use the strftime() method to convert our datetime object into a string:

As the last number represents microseconds if we want to display the current time in milliseconds we can disregard the last 3 digits, by slicing. This has the same effect as dividing by 1000 thus converting or time to microseconds:

Python Datetime Timezone

Summary

Python Convert String To Date

We have looked at several ways of using Python to show us time in milliseconds. Whilst the update from the simple time.time to time.time_ns definitely helps with our precision, it is difficult to provide recommendations on best practice. All the methods we have seen provide an answer, but whether it’s the right answer really depends on the question we are asking.

Python Datetime Module

The key is finding the right method to fit your particular requirements, factors like level of precision required, geographical location(s) and level of complexity will all determine the right approach. Luckily Python has a number of ways to help us get milliseconds, so it could come down to personal preference.

Datetime

Python Datetime Utcnow

Related Posts