0

I am new to python. Currently, I have a timedata like "2018-11-15 13:34:40.000 EST". I want to convert it into EpochSecond.

I know how to use dateparser to get this, however, I want to know is there a simple way to do this without dateparser?

import dateparser
from datetime import datetime, timezone

mytime = "2018-11-15 13:34:40.000 EST"

dateVar = dateparser.parse(mytime)

print(dateVar)

epoch = datetime(1970, 1, 1,  tzinfo=timezone.utc)

print((dateVar - epoch).total_seconds()) 
Amen King
  • 37
  • 7

2 Answers2

0

Try:

from datetime import datetime

epoch = datetime.datetime(1970,1,1)
i = datetime.now()

delta_time = (i - epoch).total_seconds()
0

datetime.datetime.timestamp() is what you're looking for (relevant part):

For aware datetime instances, the return value is computed as:

(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()

Example:

import datetime

now = datetime.datetime.now()
epoch = now.timestamp()

# 1542394106.155199

Implemented into your example, we'll have to use another approach as datetime.datetime.strptime() doesn't quite take timezone kindly due to a bug (relevant question where I found the info). So we'll have to use another builtin to parse it (example here):

from dateutil.parser import parse

mytime = "2018-11-12 00:30:20.000 EST"
dt = parse(mytime)
epoch = dt.timestamp()

The parsed string is still a datetime.datetime object so you can handle it the same after parsing.

Note: however parse might complain it read the timezone but not understood it:

 UnknownTimezoneWarning: tzname EDT identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.

You might end up needing to pass the tzinfos into the parse() method anyhow:

from dateutil.parser import parse
# from dateutil.tz import gettz # <-- can use if you know the tz to retrieve

tzinfos = {'EST': -18000, 'CET': +3600}
# another example: {"CST": gettz("America/Chicago")}
mytime = "2018-11-12 00:30:20.000 EST"
dt = parse(mytime, tzinfos=tzinfos)
epoch = dt.timestamp()
print(epoch)

So I guess in the end it's not as simple as you might want.

Community
  • 1
  • 1
r.ook
  • 13,466
  • 2
  • 22
  • 39
  • Hi Idel, could you be able to explain more about how to use this ? for example, my time is "2018-11-12 00:30:20.000 EST" – Amen King Nov 16 '18 at 19:00
  • You'll have to convert your string to a `datetime` object first of course. If you don't know a particular format or aren't sure how to convert it, using `dateparser` is one way. If the format is consistent you can just use `.strptime()` method from `datetime` module and convert the string first, *then* convert to epoch time. – r.ook Nov 16 '18 at 19:03
  • Note in your example with the `'EST'` time zone, it becomes a bit more problematic as there is a [bug in `strptime()` to process some time zone](https://bugs.python.org/issue22377). If `dateparser` is already working for you, why not rely on that instead? otherwise you'll have to manually parse the timezone and pass it into the `datetime` object itself. – r.ook Nov 16 '18 at 19:10
  • You are right, because I can not install the module on my company machine. So I am trying to see if I can parse the data manually. – Amen King Nov 16 '18 at 19:14
  • @AmenKing Okay I found another way to do this with a different built in. See my edited answer. – r.ook Nov 16 '18 at 19:23
  • @idlehands `dateutil` is not a builtin; it's something you installed. That example does not WFM, it fails with an `ImportError`. (just re: your edited answer, looks like the questioner got his code working) – AdamKG Nov 16 '18 at 19:28
  • @AdamKG seems it's a builtin for later version I believe. [Works fine on repl.it](https://repl.it/repls/LoosePapayawhipAudacity) without an external import. – r.ook Nov 16 '18 at 19:30
  • @AmenKing Just want to point out, for the timezone, parse might raise a `Warning` as the timezone can be identified but not understood, meaning the data is not passed correctly. You might need to add `tzinfos` argument into the `parse()` see my edits and additional links. – r.ook Nov 16 '18 at 19:38
  • @idlehands It's something repl.it installed in it's environment too, then; it reports it's version as 3.6.1, mine is 3.6.6 and does not have it, and just to make sure I [checked the source](https://gist.github.com/AdamG/52b6f40bc3eb66361498de58fa761642) :) – AdamKG Nov 18 '18 at 03:00
  • Hmm, I suppose you're right. Seems it's required by `pandas` and that's why it showed up on all three of my environments (I don't have a pure one installed). I'm surprised repl.it would have it though, I would have assumed it's a vanilla version by default. – r.ook Nov 18 '18 at 03:30