1

I am trying to convert time stamp to human date. i am doing that in order to get the local time of a message and insert it into our database as human date.

currently if i use the following code:

from datetime import datetime, timedelta
     reference_date1 = datetime(2001, 1, 1, 0, 0, 0)
    delta_since_reference = timedelta(seconds=530023775)
    print str(reference_date1 + delta_since_reference)

i get:

2017-10-18 12:49:35

which there the date is correct but the hour is wrong. and if i use:

print datetime.fromtimestamp(530023775)

i get:

1986-10-18 14:49:35

which here the hour is correct but the year is wrong. there is a way to merge between the 2 results? or there is a better approach to convert time stamp?

Matan Tubul
  • 774
  • 3
  • 11
  • 33

3 Answers3

2

You are forgetting to add in reference_date1 when calculating the the datetime using fromtimestamp().

Here is the updated code

from datetime import datetime, timedelta
reference_date1 = datetime(2001, 1, 1, 0, 0, 0)
delta_since_reference = timedelta(seconds=530023775)
print(str(reference_date1 + delta_since_reference))
print(datetime.fromtimestamp(530023775 + float(datetime.strptime(str(reference_date1), "%Y-%m-%d %H:%M:%S").strftime('%s'))))

Which prints

2017-10-18 12:49:35
2017-10-18 12:49:35    
  • the second print is print a wrong time, the correct time should be 14:49:35 – Matan Tubul Oct 19 '17 at 06:04
  • i get Traceback "'datetime.datetime' object has no attribute 'timestamp'" for print(datetime.fromtimestamp(530023775 + reference_date1.timestamp())) – Matan Tubul Oct 19 '17 at 06:08
  • Are you sure that you're supposed to get 14 as the hour? `(530023775/(60*60))%24 = 12.83` This indicates that the remaining hours after taking off a number of days is 12.83. So, it would make sense that the hours is 12 since the time on your reference date is 00:00. As for the Traceback error, it turns out that datetime.datetime.timestamp is only available in Python 3. I will update my answer to be compatible with Python 2. – user2471379 Oct 19 '17 at 07:06
  • currently i see in https://www.epochconverter.com/ that in GMT time is 12 but in local time is 14. it is bit confusing me because the diffrent between my time to the local time should be 1 hour. this is the local time there, https://www.timeanddate.com/worldclock/zimbabwe/harare and now on my clock is 10:37 – Matan Tubul Oct 19 '17 at 07:37
  • You may need to do some timezone conversion after converting the seconds since reference date to a datetime – user2471379 Oct 19 '17 at 07:45
  • yes probably i will have to do that. thank you very much for your help – Matan Tubul Oct 19 '17 at 07:48
1

In your first try you use 1.1.2001 as your reference date:

reference_date1 = datetime(2001, 1, 1, 0, 0, 0)

Where as according to Python docs, datetime.fromtimestamp:

Return the local date corresponding to the POSIX timestamp, such as is returned by time.time(). [..] Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by fromtimestamp()

Emphasis mine

And a POSIX timestamp is:

Unix time (also known as POSIX time or epoch time)[citation needed] is a system for describing a point in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970

So:

  • The dates are different because of the different reference date. Simply decide what standard you want to adhere to
  • The hours on my PC are the same (I use a POSIX-compliant macOS), but they might differ on your PC if you use a non-POSIX system

Solutions:

  • If you only need to print the date

    datetime.now()

  • If you need the date both as timestamp and printable

    datetime.fromtimestamp(time.time())

Daniel Trugman
  • 8,186
  • 20
  • 41
1

As you want to convert time stamp to "human" date, try to do like this:

from datetime import datetime

your_time_stamp = int("1284101485")
human_date = datetime.fromtimestamp(your_time_stamp).strftime('%Y-%m-%d %H:%M:%S')

This solution has been taken here (another stackoverflow post).

Serge Kishiko
  • 466
  • 1
  • 6
  • 13