There's at least one time zone that skips from 23:59:59 to 1:00:00 when "springing forward" for DST. Does anyone know what it is?
The following normally gets today's date, but it fails one day a year for time zones matching the above criterion.
$ perl -MDateTime -E'say DateTime->today( time_zone => $ARGV[0] )->ymd;' \
America/New_York
2013-08-28
I need the time zone for testing purposes. I'm not trying to get the above code to work.
There are several. As of 2020-04, there are 14 such time zones in 10 countries: Paraguay, Cuba, Chile, Greenland/Denmark, Jordan, Lebanon, Syria, Palestine, Iran, and Azores/Portugal.
Before 2019-04, the list included Brazil using the America/Sao_Paulo time zone.
$ perl -MDateTime -E'say DateTime->new(
year => 2013, month => 10, day => 20, hour => 12,
time_zone => "America/Sao_Paulo")->truncate( to => "day" )->ymd;'
Invalid local time for date in time zone: America/Sao_Paulo
You can get around the problem by switching to the "floating" tome zone before getting the date:
$ perl -MDateTime -E'say DateTime->new(
year => 2013, month => 10, day => 20, hour => 12,
time_zone => "America/Sao_Paulo")
->set_time_zone("floating")
->truncate( to => "day" )
->ymd;'
2013-10-20
See this documentation.
I've thrown together a small program in Java to find all zones that contain a date in 2020 where the start of the date is not midnight. Its output lists all the time zones on my machine and version of Java matching your requirements as of 2020, as well as the date & time of the non-midnight start of day.
import java.time.*;
import java.time.zone.ZoneRulesProvider;
import java.util.stream.Stream;
public class ListTimeZonesWithoutMidnight {
public static void main(String[] args) {
System.out.printf("Java vendor & version: %s %s\n", System.getProperty("java.vendor"), Runtime.version());
System.out.printf("Zone rules versions: %s\n", ZoneRulesProvider.getVersions("UTC").keySet());
System.out.println();
ZoneId.getAvailableZoneIds().stream().sorted().map(ZoneId::of)
.flatMap(zoneId -> getNonMidnightStartOfDays(Year.of(2020), zoneId))
.forEachOrdered(System.out::println);
}
private static Stream<LocalDate> getAllDates(Year year) {
return year.atDay(1).datesUntil(year.plusYears(1).atDay(1));
}
private static Stream<ZonedDateTime> getNonMidnightStartOfDays(Year year, ZoneId zoneId) {
return getAllDates(year).map(d -> d.atStartOfDay(zoneId))
.filter(d -> !d.toLocalTime().equals(LocalTime.MIDNIGHT));
}
}
Output:
Java vendor & version: AdoptOpenJDK 14.0.1+7
Zone rules versions: [2019c]
2020-10-04T01:00-03:00[America/Asuncion]
2020-03-08T01:00-04:00[America/Havana]
2020-09-06T01:00-03:00[America/Santiago]
2020-03-29T01:00Z[America/Scoresbysund]
2020-03-27T01:00+03:00[Asia/Amman]
2020-03-29T01:00+03:00[Asia/Beirut]
2020-03-27T01:00+03:00[Asia/Damascus]
2020-03-27T01:00+03:00[Asia/Gaza]
2020-03-27T01:00+03:00[Asia/Hebron]
2020-03-21T01:00+04:30[Asia/Tehran]
2020-03-29T01:00Z[Atlantic/Azores]
2020-09-06T01:00-03:00[Chile/Continental]
2020-03-08T01:00-04:00[Cuba]
2020-03-21T01:00+04:30[Iran]
Those 14 zone names are in 10 countries: Paraguay, Cuba, Chile, Greenland/Denmark, Jordan, Lebanon, Syria, Palestine, Iran, and Azores/Portugal.
Related
I have two input variables: an epoch time in UTC time zone and the name of the actual time zone. How do I get a formatted day/time using moment.js that would take in account the DST changes. I tried this code but it doesn't do the trick. What am I doing wrong, please?
var abs_time = 1611188219.277; // this is UTC coresponding to 1/21/2021 18:31:37 UTC
var timezone = "America/New_York"; // this the actual time zone
var mom = moment(abs_time * 1000).format();
var date_time = moment.tz(mom, timezone).format('ddd, MMM DD YYYY - HH:mm');
console.log(date_time);
//actual result: Thu, Jan 21 2021 - 18:31
//desired result: Thu, Jan 21 2021 - 13:31 - in the summer this should only be 4 hour difference
First, 1611188219.277 actually corresponds to 2021-01-21T00:16:59.277Z, not the time you gave in your question (assuming it is a Unix timestamp with seconds precision). This can be seen with the following code:
const d = new Date(1611188219.277 * 1000);
const s = d.toISOString();
console.log(s);
You can get the equivalent local time in a specific time zone without any libraries, as long as you're satisfied with the output produced by the toLocaleString function.
const d = new Date(1611188219.277 * 1000);
const s = d.toLocaleString(undefined, { timeZone: 'America/New_York' });
console.log(s);
Note that undefined in the above code will use the browser's current language. If you want a specific language, you could pass its language code there instead (such as en or en-US, etc.)
In general, due to its project status, you should avoid using Moment unless it's already being used in an existing project. If however, you must use Moment and Moment-TimeZone for this, you can do the following to get the same result:
const m = moment.tz(1611188219.277 * 1000, 'America/New_York');
const s = m.format('ddd, MMM DD YYYY - HH:mm');
console.log(s);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.32/moment-timezone-with-data-10-year-range.min.js"></script>
I used the same format from your question, but of course you could change this as desired.
You might also consider using Luxon (the successor to Moment), or Date-fns, or several other libraries.
And yes, all of the above will correctly take daylight saving time into account.
How to return time in RFC 3339 format (2014-06-01T12:00:00Z). I read docs about calendar module, but there was no explanation how to generate time format like this. My program should work in different time zones, so please give me advices.
The Erlang Central page Converting Between struct:time and ISO8601 Format has this example:
Unfortunately, no Erlang libraries provide this functionality. Luckily, the native Erlang date and time formats are very easy to format for display or transmission, even in ISO-8601 format:
-module(iso_fmt).
-export([iso_8601_fmt/1]).
iso_8601_fmt(DateTime) ->
{{Year,Month,Day},{Hour,Min,Sec}} = DateTime,
io_lib:format("~4.10.0B-~2.10.0B-~2.10.0B ~2.10.0B:~2.10.0B:~2.10.0B",
[Year, Month, Day, Hour, Min, Sec]).
format_iso8601() ->
{{Year, Month, Day}, {Hour, Min, Sec}} =
calendar:universal_time(),
iolist_to_binary(
io_lib:format(
"~.4.0w-~.2.0w-~.2.0wT~.2.0w:~.2.0w:~.2.0wZ",
[Year, Month, Day, Hour, Min, Sec] )).
Using the above module:
1> {{Year,Month,Day},{Hour,Min,Sec}} = erlang:localtime().
{{2004,8,28},{1,19,37}}
2> io:fwrite("~s\n",[iso_fmt:iso_8601_fmt(erlang:localtime())]).
2004-08-28 01:48:48
To make it output time in UTC, just pass it the return value of erlang:universaltime() instead of erlang:localtime().
Two computer install centos 6.5, kernel is 3.10.44, have different result.
one result is [u'Asia/Shanghai', u'Asia/Urumqi'], and the other is ['Asia/Shanghai', 'Asia/Harbin', 'Asia/Chongqing', 'Asia/Urumqi', 'Asia/Kashgar'].
Is there any config that make the first result same as the second result?
I have following python code:
def get_date():
date = datetime.utcnow()
from_zone = pytz.timezone("UTC")
to_zone = pytz.timezone("Asia/Urumqi")
date = from_zone.localize(date)
date = date.astimezone(to_zone)
return date
def get_curr_time_stamp():
date = get_date()
stamp = time.mktime(date.timetuple())
return stamp
cur_time = get_curr_time_stamp()
print "1", time.strftime("%Y %m %d %H:%M:%S", time.localtime(time.time()))
print "2", time.strftime("%Y %m %d %H:%M:%S", time.localtime(cur_time))
When use this code to get time, the result of one computer(have 2 results) is:
1 2016 04 20 08:53:18
2 2016 04 20 06:53:18
and the other(have 5 results) is:
1 2016 04 20 08:53:18
2 2016 04 20 08:53:18
I don't know why?
You probably just have an outdated version of pytz on the system returning five time zones (or perhaps on both systems). You can find the latest releases here. It's important to stay on top of time zone updates, as the various governments of the world change their time zones often.
Like most systems, pytz gets its data from the tz database. The five time zones for China were reduced to two in version 2014f (corresponding to pytz 2014.6). From the release notes:
China's five zones have been simplified to two, since the post-1970
differences in the other three seem to have been imaginary. The
zones Asia/Harbin, Asia/Chongqing, and Asia/Kashgar have been
removed; backwards-compatibility links still work, albeit with
different behaviors for time stamps before May 1980. Asia/Urumqi's
1980 transition to UTC+8 has been removed, so that it is now at
UTC+6 and not UTC+8. (Thanks to Luther Ma and to Alois Treindl;
Treindl sent helpful translations of two papers by Guo Qingsheng.)
Also, you may wish to read Wikipedia's Time in China article, which explains that the Asia/Urumqui entry is for "Ürümqi Time", which is used unofficially in some parts of the Xinjiang region. This zone is not recognized by the Chinese government, and is considered a politically charged issue. As such, many systems choose to omit the Urumqi time zone, despite it being in listed in the tz database.
I am working on scheduler module, for which I want to know if any X time zone is in midnight currently. Is there any library/gem for this purpose? If not, what would be a good logic to find out?
Your phrasing is a little ambiguous ("any X time zone"), so I'll address another possible interpretation:
If you'd like to check whether "it's midnight somewhere", with "midnight" understood broadly to mean any time from 00:00 to 00:59, a simple one-liner is
ActiveSupport::TimeZone.all.any?{ |time| time.now.hour.zero? }
To check that it's precisely 00:00 (or 00:00:00, although I can't imagine why you would want to do that...), add the appropriate conditions to the boolean block:
ActiveSupport::TimeZone.all.any?{ |time| time.now.hour.zero? && time.now.min.zero? }
You can probably leave off the ActiveSupport:: when calling TimeZone.all from within your Rails app.
[source]
You could do this:
> Time.zone
=> (GMT-05:00) Eastern Time (US & Canada)
> given_time_zone = "Central Time (US & Canada)"
> Time.zone.now.in_time_zone(given_time_zone)
=> Sun, 23 Mar 2014 23:55:57 CDT -05:00
Then you could check if the hour is zero to check if it's within 12 midnight till 1AM:
> Time.zone.now.in_time_zone(given_time_zone).hour == 0
=> false
Good day. I'm new to symfony and I'm using symfony doctrine 1.4. my question is how to convert the 24 hour format into a 12 hour format in symfony? here some code(based on my project):
**project\reservation\config\doctrine\schema.yml**
startime: { type: time, notnull: true }
endtime: { type: time, notnull: true }
as you can see here in my schema.yml format code. I set my startime and endtime into time data type so I can use for time checking on my project. and also I set this code to remove seconds and I set the range the hours from 1 to 12 (no meridiem)
**project\reservation\lib\form\doctrine\reservationApplicationForm.class.php**
$hour = range(1, 12);
$this->widgetSchema['startime'] = new sfWidgetFormTime(array(
'with_seconds' => false,
'hours' => array_combine($hour, $hour),
'format_without_seconds' => '%hour% : %minute%'
));
$this->widgetSchema['endtime'] = new sfWidgetFormTime(array(
'with_seconds' => false,
'hours' => array_combine($hour, $hour),
'format_without_seconds' => '%hour% : %minute%'
));
but when I press the submit button to display, I get this format, (assume I type 7:00 in startime and 5:00 in endtime i get this 7:00:00 and 5:00:00 on my display)
well i was expecting that my output should be 7:00 and 5:00 but no meridiem.
Can anyone help me to convert the 24hr format in 12 hour with a meridiem format(am/pm). I was expecting to display this format H:i (am/pm).
thanks
P.S
Sorry for a bad english I'm a filipino
You should install the sfFormtasticPlugin, see: http://www.symfony-project.org/plugins/sfFormtasticPlugin
In this plugin you find this class handling 12 hour format: http://svn.symfony-project.com/plugins/sfFormtasticPlugin/trunk/lib/widget/sfWidgetasticFormTime.class.php
PS: My trip to camiguin was awesome! :)