What is the easiest way to get current GMT time in Unix timestamp format?

PythonTimeUnix Timestamp

Python Problem Overview


Python provides different packages (datetime, time, calendar) as can be seen here in order to deal with time. I made a big mistake by using the following to get current GMT time time.mktime(datetime.datetime.utcnow().timetuple())

What is a simple way to get current GMT time in Unix timestamp?

Python Solutions


Solution 1 - Python

I would use time.time() to get a timestamp in seconds since the epoch.

import time

time.time()

Output:

1369550494.884832

For the standard CPython implementation on most platforms this will return a UTC value.

Solution 2 - Python

import time

int(time.time()) 

Output:

1521462189

Solution 3 - Python

Does this help?

from datetime import datetime
import calendar

d = datetime.utcnow()
unixtime = calendar.timegm(d.utctimetuple())
print unixtime

How to convert Python UTC datetime object to UNIX timestamp

Solution 4 - Python

python2 and python3

it is good to use time module

import time
int(time.time())

> 1573708436

you can also use datetime module, but when you use strftime('%s'), but strftime convert time to your local time!

python2

from datetime import datetime
datetime.utcnow().strftime('%s')

python3

from datetime import datetime
datetime.utcnow().timestamp()

Solution 5 - Python

Python 3 seconds with microsecond decimal resolution:

from datetime import datetime
print(datetime.now().timestamp())

Python 3 integer seconds:

print(int(datetime.now().timestamp()))

WARNING on datetime.utcnow().timestamp()!

datetime.utcnow() is a non-timezone aware object. See reference: https://docs.python.org/3/library/datetime.html#aware-and-naive-objects

For something like 1am UTC:

from datetime import timezone
print(datetime(1970,1,1,1,0,tzinfo=timezone.utc).timestamp())

or

print(datetime.fromisoformat('1970-01-01T01:00:00+00:00').timestamp())

if you remove the tzinfo=timezone.utc or +00:00, you'll get results dependent on your current local time. Ex: 1am on Jan 1st 1970 in your current timezone - which could be legitimate - for example, if you want the timestamp of the instant when you were born, you should use the timezone you were born in. However, the timestamp from datetime.utcnow().timestamp() is neither the current instant in local time nor UTC. For example, I'm in GMT-7:00 right now, and datetime.utcnow().timestamp() gives a timestamp from 7 hours in the future!

Solution 6 - Python

Or just simply using the datetime standard module

In [2]: from datetime import timezone, datetime
   ...: int(datetime.now(tz=timezone.utc).timestamp() * 1000)
   ...: 
Out[2]: 1514901741720

You can truncate or multiply depending on the resolution you want. This example is outputting millis.

If you want a proper Unix timestamp (in seconds) remove the * 1000

Solution 7 - Python

At least in python3, this works:

>>> datetime.strftime(datetime.utcnow(), "%s")
'1587503279'

Solution 8 - Python

I like this method:

import datetime, time

dts = datetime.datetime.utcnow()
epochtime = round(time.mktime(dts.timetuple()) + dts.microsecond/1e6)

The other methods posted here are either not guaranteed to give you UTC on all platforms or only report whole seconds. If you want full resolution, this works, to the micro-second.

Solution 9 - Python

from datetime import datetime as dt
dt.utcnow().strftime("%s")

Output:

1544524990

Solution 10 - Python

#First Example:
from datetime import datetime, timezone    
timstamp1 =int(datetime.now(tz=timezone.utc).timestamp() * 1000)
print(timstamp1)

> Output: 1572878043380

#second example:
import time
timstamp2 =int(time.time())
print(timstamp2)

> Output: 1572878043

  • Here, we can see the first example gives more accurate time than second one.
  • Here I am using the first one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCoryView Question on Stackoverflow
Solution 1 - PythonEdmond BurnettView Answer on Stackoverflow
Solution 2 - PythonMaxView Answer on Stackoverflow
Solution 3 - PythonNick CaplingerView Answer on Stackoverflow
Solution 4 - PythonMikAView Answer on Stackoverflow
Solution 5 - PythonStan KurdzielView Answer on Stackoverflow
Solution 6 - PythonMareshView Answer on Stackoverflow
Solution 7 - PythonusernamenumberView Answer on Stackoverflow
Solution 8 - PythonvschmidtView Answer on Stackoverflow
Solution 9 - Python2niView Answer on Stackoverflow
Solution 10 - PythonChandan SharmaView Answer on Stackoverflow