#π datetime.timestamp() and time.time()
19 messages Β· Page 1 of 1 (latest)
@nocturne nacelle
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.
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)
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
yes, it's the same as time.time()
Alright thank ya
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())
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Using .replace() on timezone aware:
002 | 2024-09-04 1725375600.0
003 | Using .date() and create local datetime:
004 | 2024-09-04 1725408000.0
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)
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.