-1

I have two variables : One is epoch time and other is normal datetime,

df['epochtime']

0       1457586382
1       1457586382
2       1457586391
3       1457586692
4       1457586391
5       1457586391
6       1457586692
7       1457586692
8       1457572611

df['time']
0      2016-03-10 00:06:21.903
1      2016-03-10 00:06:21.908
2      2016-03-10 00:06:30.895
3      2016-03-10 00:06:30.895
4      2016-03-10 00:06:30.899
5      2016-03-10 00:06:30.899
6      2016-03-10 00:06:31.045
7      2016-03-10 00:06:31.094
8      2016-03-10 01:16:51.390 

I want the difference between these two times in seconds. The following is my trying,

pd.to_datetime(df['epochtime'], unit ='s' ) - df['time']

The following is my output,

0               00:00:00.097000
1               00:00:00.092000
2               00:00:00.105000
3               00:05:01.105000
4               00:00:00.101000
5               00:00:00.101000
6               00:05:00.955000
7               00:05:00.906000
8      -1 days +23:59:59.610000   

I want to make this as integer in seconds. Also for the 8th value for some strange reason getting difference as -1 days, where it should be seconds only. Can anybody help me getting the seconds difference as integer ?

Observer
  • 641
  • 2
  • 14
  • 31
  • I suggest you should first understand the definition of 'epoch time'. From there your solution flows naturally. – ChrisC73 Mar 16 '16 at 22:45
  • check it here http://stackoverflow.com/questions/8777753/converting-datetime-date-to-utc-timestamp-in-python – Haifeng Zhang Mar 16 '16 at 22:56
  • @ChrisC73 I agree that I am new to epoch time. But I have read about that and felt that i know a little about it. I didn't get why you are saying that. – Observer Mar 16 '16 at 22:57
  • @haifzhan how does that answer my question? – Observer Mar 16 '16 at 23:03
  • 1
    @Observer: Or not so naturally, perhaps. Epoch is in seconds => convert datetime to epoch and do your arithmetic. – ChrisC73 Mar 16 '16 at 23:10
  • @Observer because you cannot use epoch time minus datetime. You haveto convert datetime to epoch and then do the math – Haifeng Zhang Mar 16 '16 at 23:28

2 Answers2

1

You have to convert datetime to epoch time, and then do the arithmetic subtraction.

As you said you are always use UTC time, you need to use calendar.timegm() because it takes UTC time as parameter.

import time
import datetime
import calendar

TIME_FORMAT = '%Y-%m-%d %H:%M:%S'

def local_datetime_2_timestamp(local_datetime):
    '''strptime takes local datetime as parameter'''
    timestamp = str(time.mktime(datetime.datetime.
        strptime(local_datetime, TIME_FORMAT).timetuple()))[:-2]
    return timestamp

def utc_2_timestamp(utc_datetime):
    '''timegm takes UTC time as parameter'''
    return calendar.timegm(utc_datetime.timetuple())

print local_datetime_2_timestamp("2016-03-10 00:06:21.903".split(".")[0])
print utc_2_timestamp(datetime.datetime(2016,3,10,0,6,21))
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
0
from time import mktime, strptime

date_a = '20-01-2015 11:05:02'
epoch_b = 1457586692

epoch_a = int(mktime(strptime(date_a, '%d-%m-%Y %H:%M:%S')))

print(epoch_a - epoch_b)
lpszBar
  • 11
  • 3
  • `This is the inverse function of localtime(). Its argument is the struct_time or full 9-tuple (since the dst flag is needed; use -1 as the dst flag if it is unknown) which expresses the time in local time, not UTC.` quoted from python documents – Haifeng Zhang Mar 16 '16 at 23:36
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Ajean Mar 17 '16 at 01:01