[AstroPy] datetime and timestamp

Evert Rol evert.rol at gmail.com
Thu Oct 14 06:02:21 EDT 2021


That will be due to daylight savings: the difference between the second and third time is 3000 seconds, or 50 minutes, which is 1 hour (daylight savings) - 10 minutes (difference given).

If you use utctimetuple(), you'll get what you want:

In [24]: time.mktime(datetime(2021, 3, 28, 3, 00).utctimetuple()) - time.mktime(datetime(2021, 3, 28,  2, 50).utctimetuple())
Out[24]: 600.0


An alternative is to use the .timestamp() method of a datetime object, with a specific UTC timezone:

In [33]: datetime(2021, 3, 28, 3, 00, tzinfo=timezone.utc).timestamp() - datetime(2021, 3, 28, 2, 50, tzinfo=timezone.utc).timestamp()
Out[33]: 600.0


> On 14 Oct 2021, at 11:17, eperez <eperez at iaa.es> wrote:
> 
> I get an inconsistent result when translating datetime to timestamp:
> A later date gives a lower timestamp: tehere is a jump backwards:
> 
> 
> In : time.mktime(datetime(2021, 3, 28, 2, 40).timetuple())
> Out: 1616895600.0
> 
> In : time.mktime(datetime(2021, 3, 28, 2, 50).timetuple())
> Out: 1616896200.0
> 
> In : time.mktime(datetime(2021, 3, 28, 3, 0).timetuple())
> Out: 1616893200.0
> In : time.mktime(datetime(2021, 3, 28, 3, 10).timetuple())
> Out: 1616893800.0
> 
> 
> I would very much appreciate your insight.
> 
> I am not sure this is the right list to ask; apologies if it isn’t.
> 
> Cheers.
> ES.Enrique
> 
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/astropy/attachments/20211014/4fc733db/attachment-0001.html>


More information about the AstroPy mailing list