![]() ![]() Here is an example of such a timestamp as generated by python(updated thanks to and Paul Ganssle) from datetime import datetime, timezoneĮpoch = datetime.now(tz=timezone.utc).timestamp() # example: 1520020585.536527 Return the local date corresponding to the POSIX timestamp the timestamp is converted to tz’s time zone. Use fromtimestamp as referenced in the documentation If you have an earlier version, you'll have to do the calculation by hand, or pip install datetime To convert back from a timedelta to the number of seconds, there's timedelta.total_seconds, assuming you're using python 2.7. Instead, you should use omtimestamp first, attach a timezone to each datetime, and only then subtract them to get the timedelta. If your timestamps are in local time, don't subtract them directly, as you'll get potentially incorrect results. You may then want to create a timedelta, which will allow you to easily manipulate datetimes with it (you can sum or subtract a timedelta to/from a datetime, for example). If your timestamps are already in UTC (Unix time), you can use Marc B's suggestion of subtract two unix timestamps directly to give you the number of seconds of difference between them. You must check against a known reference to be sure. Unix time is unambiguously UTC, but it's not unusual to store local time in Unix-style timestamps. It's worthwhile to remember that a timestamp has no associated timezone information. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |