#πŸ”’ datetime.timestamp() and time.time()

19 messages Β· Page 1 of 1 (latest)

nocturne nacelle
#

Just wondering, is datetime.timestamp and time.time comparable? Can I like, take timestamp and compare it to time.time to see if the datetime object is later than the current time?

arctic lakeBOT
#

@nocturne nacelle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mellow horizon
#

yes. they represent the same thing: a "unix timestamp"

#

you can also use datetime.now() and compare two datetimes directly (as datetime objects implement all the >, <= etc. operators)

nocturne nacelle
#

Yeah I know about the > <= operators but unfortunately I am working with a system that sorta already uses seconds, and I can't dig it up from the roots in one night so yeah. Gonna work with what I have. Okay so just to make sure, basically

todays_date = datetime.datetime.now(tz=zoneinfo.ZoneInfo("Europe/Stockholm")).date()
            date_ = datetime.datetime(todays_date.year,todays_date.month,todays_date.day).timestamp()

This should be comparable with time.time(). I assume thta's what wer'e both thinking

#

It appears to work

mellow horizon
#

yes, it's the same as time.time()

nocturne nacelle
#

Alright thank ya

mellow horizon
#

not sure about the method used there, with the timezone.

#

the second timestamp would be in the local timezone after all, which might not be the same as the stockholm one

#

if you want to get the 00:00:00 time of the "today" date in the specified timezone, Id' recommend using .replace instead

#
now = datetime.datetime.now(tz=zoneinfo.ZoneInfo("Europe/Stockholm"))
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
#

and then today.timestamp()

#

!e

import datetime
import zoneinfo

tz = zoneinfo.ZoneInfo("Asia/Tokyo")

now = datetime.datetime.now(tz=tz)
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
print('Using .replace() on timezone aware:')
print(today.date(), today.timestamp())

todays_date = datetime.datetime.now(tz=tz).date()
today = datetime.datetime(todays_date.year, todays_date.month, todays_date.day)
print('Using .date() and create local datetime:')
print(today.date(), today.timestamp())
arctic lakeBOT
mellow horizon
#

here you can see that it's seemingly the same date, but actually different timestamps.
The first one is the date in the specified timezone (Tokyo), and the second one is the date in the Bots timezone (UTC).

#

(alternatively you can pass in the tz to the datetime constructor)

arctic lakeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.