3

I have a text file ,want to open in python and take the date and time (in header) then change it to seconds (timestamp). but result is not the same as online websites. Code:

with open('sample for north - Kopie.txt', "r") as f:
    content = f.readlines()

    for x in content :
        day = float (content [0][:2])
        month = float (content [0][3:5])
        year = float (content [0][6:10])
        hour = float (content [0][10:13])
        minn = float (content [0][14:16])
        second = float (content [0][17:19])
        time = float ((((year-1970)*31556926)+((month-1)*2629743)+((day-1)*86400)+((hour)*3600)+((minn)*60)+second))

for 13.07.2015 09:38:17 result of program is 1436911625 but result of websites are 1436780297.

kian
  • 87
  • 1
  • 11
  • What is `re`? Also, the file related code seems irrelevant to the question. Could you just create a function to process a line? – Peter Wood Aug 28 '15 at 07:02
  • Are you accounting for leap years, etc? – Peter Wood Aug 28 '15 at 07:03
  • @PeterWood I am really sorry. Corrected. I just found websites say that each year is 31556926 second and each month is 2629743 seconds and... and just put it in equation. – kian Aug 28 '15 at 07:08

2 Answers2

4

You may use time module:

import time

>>>print(time.mktime(time.strptime("13.07.2015 09:38:17", "%d.%m.%Y %H:%M:%S")))
1436769497.0
mmachine
  • 896
  • 6
  • 10
  • it is a good idea.but if you put the result in (http://www.epochconverter.com/) it give 07:38:17 – kian Aug 28 '15 at 07:18
  • 2
    It is because timestamp based on my computer time zone, and your computer on your time zone. – mmachine Aug 28 '15 at 07:22
  • it is right. last question: if i want to ad the time exactly like website, what should i do? – kian Aug 28 '15 at 07:27
  • 1
    You need to know website time zone and add or deduct difference from your timestamp – mmachine Aug 28 '15 at 07:30
  • @mmachine: timestamp is not based on your computer time zone. `1436780297` corresponds to `2015-07-13T09:38:17Z` whatever your local time zone is. On the other hand, **the input time string** may be interpreted differently: it may represent time in an arbitrary timezone. But if OP wants `1436780297` timestamp than it means that the input is in UTC (or some other timezone that has the zero utc offset at that time). – jfs Sep 01 '15 at 11:08
  • Thank you for clarification. – mmachine Sep 01 '15 at 11:56
2

There are two parts:

  1. Convert a time string into a time object in Python. You could use .strptime() for that:

    from datetime import datetime
    
    dt = datetime.strptime("13.07.2015 09:38:17", "%d.%m.%Y %H:%M:%S")
    # -> datetime.datetime(2015, 7, 13, 9, 38, 17)
    
  2. Convert the time object into (POSIX) timestamp (Unix time). 1436780297 result suggests that your input time is in UTC and therefore you could use calendar.timegm() to get the timestamp:

    from calendar import timegm
    
    timestamp = timegm(dt.timetuple())
    # -> 1436780297
    

See Converting datetime.date to UTC timestamp in Python

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670