1

Have the following string:

"date Thursday June 03 12:02:56 2017"

What would be the proper way of convert it to epoch time?

martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

4

You can use datetime.strptime() to parse your date and then just do delta with the epoch:

from datetime import datetime as dt

epoch = dt(1970, 1, 1)
date = "date Thursday June 03 12:02:56 2017"

epoch_time = int((dt.strptime(date, "date %A %B %d %H:%M:%S %Y") - epoch).total_seconds())
# 1496491376
zwer
  • 24,943
  • 3
  • 48
  • 66
1

Another solution is to create a time.struct_time structure by parsing with time.strptime() and then pass it into calendar.timegm() to convert to epoch time.

import time
import calendar
timestr = "date Thursday June 03 12:02:56 2017"
calendar.timegm(time.strptime(timestr, "date %A %B %d %H:%M:%S %Y"))
# returns 1496491376
alkasm
  • 22,094
  • 5
  • 78
  • 94