0

I want to calcualte the total seconds between two arbitrary timestamps.

I have the following:

import datetime
import dateparser


now = datetime.datetime.now(datetime.timezone.utc);  

now_bst = dateparser.parse("26April 11:55 am BST");

before = dateparser.parse("25 April 11pm BST");

now.timestamp() - before.timestamp(); # prints  82510.36681008339

now_bst.timestamp() - before.timestamp() #prints  46500.0


Why are the results so different? I was expecting the result to be the same (or super close) because timestamp() resolves to a posix timestamp, which should be the same value for UTC for bst.

martineau
  • 119,623
  • 25
  • 170
  • 301
nz_21
  • 6,140
  • 7
  • 34
  • 80

1 Answers1

-1

This is because, you have some assumptions wrong. Yes the timestamp() returns the POSIX timestamp, but not of UTC time unless your system is configured with UTC time stamp.

From the documentation ,

datetime.timestamp() Return POSIX timestamp corresponding to the datetime instance. The return value is a float similar to that returned by time.time()

So, the function returns the timestamp, which is in accordance to the datetime object you hold. If the instance is in UTC, then you get POSIX timestamp for UTC.

Also mentioned clearly in docs

Note: There is no method to obtain the POSIX timestamp directly from a naive datetime instance representing UTC time. If your application uses this convention and your system timezone is not set to UTC, you can obtain the POSIX timestamp by supplying tzinfo=timezone.utc:

So, its kind of expected behavior in your case, as the BST is -1 hour from UTC.

Kris
  • 8,680
  • 4
  • 39
  • 67