0

I wonder if there's any way to get the UNIX timestamp at the beginning of a certain day, i.e the midnight timestamp of each day given its timezone.

Preferably without relying on other pip modules.

Cardin
  • 5,148
  • 5
  • 36
  • 37
J Freebird
  • 3,664
  • 7
  • 46
  • 81
  • Seconds since epoch or microseconds? – Two-Bit Alchemist Oct 20 '15 at 19:11
  • If you have a `datetime` object, you can just call [`datetime.timestamp()`](https://docs.python.org/3.4/library/datetime.html#datetime.datetime.timestamp). – Kevin Oct 20 '15 at 19:11
  • ^ This, plus it's pretty easy to create a datetime for midnight local time if you don't have one already. – Two-Bit Alchemist Oct 20 '15 at 19:13
  • Yeah, just use [`datetime.combine()`](https://docs.python.org/3.4/library/datetime.html#datetime.datetime.combine) with the date you want and a midnight-at-my-timezone time. You might need pytz to make that work right if you're not using UTC, though. – Kevin Oct 20 '15 at 19:13
  • @Two-BitAlchemist seconds is enough. – J Freebird Oct 20 '15 at 19:21
  • In what form do you have "its timezone"? The name of the timezone? That day's UTC offset? – Robᵩ Oct 20 '15 at 19:22
  • 1
    @Robᵩ an integer, meaning the UTC offset. – J Freebird Oct 20 '15 at 19:29
  • Do you care if the timezone [has changed](https://www.youtube.com/watch?v=-5wpm-gesOY)? Are you handling daylight savings time? – Kevin Oct 20 '15 at 19:40
  • 2
    Do keep in mind that not all local days start at midnight. For example, [`2015-10-18` in the south half of Brazil starts at 1:00 AM](http://www.timeanddate.com/time/change/brazil/sao-paulo?year=2015). 00:00 through 00:59 (and all sub-seconds thereof) do not exist. – Matt Johnson-Pint Oct 20 '15 at 19:58
  • @Kevin I have DST information. And I don't want to take care of timezone changes now. – J Freebird Oct 20 '15 at 20:55
  • @MattJohnson Wow, I see. Thanks for that info. – J Freebird Oct 20 '15 at 21:01
  • related: [python - datetime with timezone to epoch](http://stackoverflow.com/q/12165691/4279) – jfs Oct 20 '15 at 21:26
  • related: [How to get system timezone setting and pass it to pytz.timezone?](http://stackoverflow.com/q/13218506/4279) – jfs Oct 20 '15 at 21:29
  • related: [How to find next day's Unix timestamp for same hour, including DST, in Python?](http://stackoverflow.com/q/20276508/4279) – jfs Oct 20 '15 at 21:33
  • Not a duplicate (of that question, anyway). OP is asking about a **UTC offset** rather than a timezone name. Voting to reopen. – Kevin Oct 20 '15 at 22:12
  • @Kevin: you don't need the time zone name e.g., you could use `tzlocal.get_localzone()`, to get the local time zone, see the related question that I've linked. Though if OP will [edit] the question and **replace the word "timezone"** with something else then I reopen the question. Unrelated: *your answer encourages wrong results* (UTC offset at midnight may differ from the current UTC offset, moreover 00:00 may not exist in the local time zone on some days, see my answer to the duplicate). – jfs Oct 21 '15 at 09:00
  • @JF: OP said in a comment that they have a UTC offset, so that's the question I answered. – Kevin Oct 21 '15 at 12:22
  • @Kevin: Given that your interpretation (and the corresponding solution) is harmful in most cases (don't equate "timezone" and "utf offset": most timezones may have different utc offset at different times), OP should demonstrate the intent of the question and therefore OP should [edit] the question if necessary, not you. – jfs Nov 17 '15 at 02:01
  • 1
    @J.F.Sebastian: Don't be absurd. OP specifically said *in the comments* that they have a UTC offset. Why should it matter which box they put it in? – Kevin Nov 17 '15 at 02:29
  • @Kevin: a passing remark in comments is enough for innocent statements. On the other hand, brain-dead definitions such as `timezone=utc offset` require explicit consent. To understand the emotion replace ["you can't parse html with regexes" with "time zone is not utc offset" here](http://stackoverflow.com/a/1732454/4279) – jfs Nov 17 '15 at 03:22
  • @J.F.Sebastian IMHO, it's the OP's responsibility to clarify their issue. OP clearly said that [he has UTC offset](http://stackoverflow.com/questions/33244911/get-the-timestamp-of-the-start-of-a-day-local-time?noredirect=1#comment54292676_33244911) and [doesn't care about timezone change](http://stackoverflow.com/questions/33244911/get-the-timestamp-of-the-start-of-a-day-local-time?noredirect=1#comment54295444_33244911). The problem is, OP doesn't realize that given UTC offset only, it cannot determine the correct "named timezone" (even with DST). IMHO, the answer fulfills OP's requirement. – Andrew T. Nov 17 '15 at 03:31
  • @J.F.Sebastian on the other hand, [it's not OP who wrote "local time" on the title](http://stackoverflow.com/revisions/33244911/2). So, basically the question is: "get midnight timestamp given a 'timezone' based on its UTC offset (no timezone change, no DST)" – Andrew T. Nov 17 '15 at 03:35

1 Answers1

4

Assuming you do not care about daylight savings time and you know the UTC offset which was in effect at that time (as opposed to the UTC offset which is in effect now), you can just do this:

import datetime as dt
return dt.datetime(year, month, day, tzinfo=dt.timezone(utc_offset)).timestamp()

The hour, minute, and second default to zero, so you can skip them. The timezone class does not handle daylight savings time, historical changes in timezone definitions (e.g. British Double Summer Time), or any other temporal anomalies (e.g. there was no December 30, 2011 in Samoa); it is a "dumb" offset. It is equivalent (in this case) to adding or subtracting the offset directly onto the timestamp and then working in UTC. You must ensure this is correct for your use case. If you need better timekeeping, you should install and make use of pytz.

Kevin
  • 28,963
  • 9
  • 62
  • 81
  • 1
    The downvoter is welcome to comment on whatever's wrong with this answer. Since OP is specifically asking about a UTC offset, it's rather difficult for me to give a more "correct" answer to the question as it stands. – Kevin Oct 21 '15 at 14:47