3

I want to find the last mid-night time stamp (only input is current timestamp). What is the best way?

I am writing python script for a global mobile application. The user request with the current timestamp, and in server side I want to find the last mid-night timestamp of the user with out affect time zone parameters.

I searched for it, I got a solution

import time
etime = int(time.time())
midnight = (etime - (etime % 86400)) + time.altzon

Its worked for me. But I am confused with time.altzon function, Is it create any problem for the users in different timezones.

Jisson
  • 3,566
  • 8
  • 38
  • 71
  • related: [How do I get the UTC time of “midnight” for a given timezone?](http://stackoverflow.com/q/373370/4279) – jfs Aug 09 '12 at 11:52

1 Answers1

5

To get the midnight timestamp of the client(mobile) you need to know the client's timezone.

from datetime import datetime
import pytz # pip install pytz

fmt = '%Y-%m-%d %H:%M:%S %Z%z'
tz = pytz.timezone("America/New_York") # supply client's timezone here

# Get correct date for the midnight using given timezone.

# due to we are interested only in midnight we can:

# 1. ignore ambiguity when local time repeats itself during DST change e.g.,
# 2012-04-01 02:30:00 EST+1100 and
# 2012-04-01 02:30:00 EST+1000
# otherwise we should have started with UTC time

# 2. rely on .now(tz) to choose timezone correctly (dst/no dst)
now = datetime.now(tz)
print(now.strftime(fmt))

# Get midnight in the correct timezone (taking into account DST)
midnight = tz.localize(now.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None),
                       is_dst=None)
print(midnight.strftime(fmt))

# Convert to UTC (no need to call `tz.normalize()` due to UTC has no DST transitions)
dt = midnight.astimezone(pytz.utc)
print(dt.strftime(fmt))

# Get POSIX timestamp
print((dt - datetime(1970,1,1, tzinfo=pytz.utc)).total_seconds())

Output

2012-08-09 08:46:29 EDT-0400
2012-08-09 00:00:00 EDT-0400
2012-08-09 04:00:00 UTC+0000
1344484800.0

Note: on my machine @phihag's answer produces 1344470400.0 that is different from the above (my machine is not in New York).

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • @ J.F. Sebastian I got client timestamp as a url parameter,Using that timestamp am going to find last midnight timestamp – Jisson Aug 09 '12 at 13:10
  • @Jisson: How do you know what time is a midnight without knowing client's timezone? It is midnight somewhere on Earth right now. – jfs Aug 09 '12 at 13:13
  • @J.F.Sebastian Umm, he may want to record some kind of daily stats (similar to how reputation *today* works at stackoverflow, if I'm not mistaken). Oh, and why are you using `print` as a statement here? Just by including parentheses, this should work fine on Python 3 as well. – phihag Aug 09 '12 at 13:15
  • @phihag: The OP explicitly said: "I want to get the midnight timestamp of the client(mobile". The question has tag `python-2.7` so `print` is not a function. – jfs Aug 09 '12 at 13:17
  • @ J.F. Sebastian , am not an an expert programmer. Can I find the midnight timestamp using following algorithm t= datetimeofthe get parameter timestamp; midnight = t.replace(hour=0, minute=0, second=0, microsecond=0)? – Jisson Aug 09 '12 at 13:19
  • that is first find datetime of the time I got from client and use the '.replace' fn ? is it possible its my guess. – Jisson Aug 09 '12 at 13:22
  • @phihag: there is not harm in this case to make it both Python2/3 compatible. I've updated the answer. – jfs Aug 09 '12 at 13:27
  • @Jisson: `.replace()` only works if the corresponding datetime object has correct client's timezone. So the question is can you get a datetime object in correct timezone given POSIX timestamp? (I guess, you can't) – jfs Aug 09 '12 at 13:31
  • @Jisson: If you can't get client's timezone and just want to do something daily you could use UTC midnight: `utc_dt = datetime.utcfromtimestamp(your_timestamp); utc_midnight = utc_dt.replace(hour=0,minute=0,...); utc_midnight_timestamp = (utc_midnight - datetime(1970,1,1)).total_seconds()` – jfs Aug 09 '12 at 14:05