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
  • 112,593
  • 23
  • 157
  • 280

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
  • 23,595
  • 3
  • 41
  • 57
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
  • 20,395
  • 4
  • 73
  • 90