How to Get the Utc Time of "Midnight" for a Given Timezone

How do I get the UTC time of midnight for a given timezone?

I think you can shave off a few method calls if you do it like this:

>>> from datetime import datetime
>>> datetime.now(pytz.timezone("Australia/Melbourne")) \
.replace(hour=0, minute=0, second=0, microsecond=0) \
.astimezone(pytz.utc)

BUT… there is a bigger problem than aesthetics in your code: it will give the wrong result on the day of the switch to or from Daylight Saving Time.

The reason for this is that neither the datetime constructors nor replace() take DST changes into account.

For example:

>>> now = datetime(2012, 4, 1, 5, 0, 0, 0, tzinfo=pytz.timezone("Australia/Melbourne"))
>>> print now
2012-04-01 05:00:00+10:00
>>> print now.replace(hour=0)
2012-04-01 00:00:00+10:00 # wrong! midnight was at 2012-04-01 00:00:00+11:00
>>> print datetime(2012, 3, 1, 0, 0, 0, 0, tzinfo=tz)
2012-03-01 00:00:00+10:00 # wrong again!

However, the documentation for tz.localize() states:

This method should be used to construct localtimes, rather
than passing a tzinfo argument to a datetime constructor.

Thus, your problem is solved like so:

>>> import pytz
>>> from datetime import datetime, date, time

>>> tz = pytz.timezone("Australia/Melbourne")
>>> the_date = date(2012, 4, 1) # use date.today() here

>>> midnight_without_tzinfo = datetime.combine(the_date, time())
>>> print midnight_without_tzinfo
2012-04-01 00:00:00

>>> midnight_with_tzinfo = tz.localize(midnight_without_tzinfo)
>>> print midnight_with_tzinfo
2012-04-01 00:00:00+11:00

>>> print midnight_with_tzinfo.astimezone(pytz.utc)
2012-03-31 13:00:00+00:00

No guarantees for dates before 1582, though.

Given a UTC time, get a specified timezone's midnight

Your example highlights the perils of doing datetime arithmetic in a local time zone.

You can probably achieve this using pytz's normalize() function, but here's the method that occurs to me:

point_in_time = datetime(2017, 3, 12, 16, tzinfo=pytz.utc)
pacific = pytz.timezone("US/Pacific")
pacific_time = point_in_time.astimezone(pacific)
pacific_midnight_naive = pacific_time.replace(hour=0, tzinfo=None)
pacific_midnight_aware = pacific.localize(pacific_midnight_naive)
pacific_midnight_aware.astimezone(pytz.utc) # datetime(2017, 3, 12, 8)

In other words, you first convert to Pacific time to figure out the right date; then you convert again from midnight on that date to get the correct local time.

How to correctly convert Midnight in a specific timezone to Utc time?

Consider the following code:

var cestZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");

var cestNow = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cestZone);
var cestMidnight = cestNow.Date;
var cestMidnightInUTC = TimeZoneInfo.ConvertTimeToUtc(cestMidnight, cestZone);

When DateTime.UtcNow is 12/29/2018 - 11:30:00 PM Will result in:

If you live in CEST and look at clock
cestNow: 12/30/2018 - 12:30:00 AM

If you live in CEST and had a look at clock at midnight (start of today, 30 minutes ago)
cestMidnight: 12/30/2018 - 12:00:00 AM

At your midnight, UTC was
cestMidnightInUTC: 12/29/2018 - 11:00:00 PM

Note: 12:00:00 AM is start of the day. So for example if utc now is 12/29/2018 - 11:30:00 PM, midnight was 23:30 hours ago at 12/29/2018 - 12:00:00 AM utc.

Get today midnight as milliseconds for given timezone java

Formatting to string and then parsing from it is a horrible solution, especially since you also have to also construct all these parsers every time, and your format effectively has hacks in it.

There are simpler ways to both construct a midnight ZDT:

ZonedDateTime zdt = LocalDate.now(zoneId).atTime(LocalTime.MIDNIGHT).atZone(zoneID);

And then extracting the epoch-millis value from it:

long epoch = zdt.toInstant().toEpochMilli();

Alternatively, since you know that milliseconds and nanoseconds are always 0 at midnight, this is marginally faster:

long epoch = zdt.toEpochSecond() * 1000;

The second approach wouldn't work if your time is arbitrary, as it will always ignore milli-of-second value.

UTC representation of midnight on the UTC day that was in progress 24 hours ago in python

IIUC, you want date/time now in a certain time zone, subtract one day, then convert to UTC, then 'floor' that date/time to midnight.

Here's a step-by-step example how you can do that. Note that I use zoneinfo since pytz is deprecated and time zone name "America/New_York" as "US/Eastern" is obsolte as well.

from datetime import datetime, timedelta
from zoneinfo import ZoneInfo

now = datetime.now(ZoneInfo("America/New_York"))

daybefore = now - timedelta(days=1)

daybefore_UTC = daybefore.astimezone(ZoneInfo("UTC"))

daybefore_UTC_midnight = daybefore_UTC.replace(hour=0, minute=0, second=0, microsecond=0)

print(now)
print(daybefore_UTC_midnight)
# 2022-08-15 03:38:42.006215-04:00
# 2022-08-14 00:00:00+00:00

Finding midnight of a timezone given offset in minutes [PHP]

I had to think through it quite a bit, but I think this was the solution I was looking for. Let me know if you think this algorithm is incorrect.

function getLastMidnightForOffset( $minuteOffset ) {
$today = mktime( 0, 0, 0 );
$tomorrow = $today + 86400;
$yesterday = $today - 86400;
$offset = $minuteOffset * 60;

if ( time() + $offset >= $tomorrow ) {
$localMidnight = $tomorrow - $offset;
} elseif ( time() + $offset >= $today ) {
$localMidnight = $today - $offset;
} else {
$localMidnight = $yesterday - $offset;
}

return $localMidnight;
}


Related Topics



Leave a reply



Submit