8000 [Bug #20929] Win32: Encode timezone name in UTF-8 · github/ruby@1c15f64 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1c15f64

Browse files
committed
[Bug #20929] Win32: Encode timezone name in UTF-8
1 parent 78762b5 commit 1c15f64

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

test/ruby/test_time.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,13 +717,15 @@ def test_inspect
717717
assert_equal("2000-01-01 09:12:34 +091234", t2000.localtime(9*3600+12*60+34).inspect)
718718
end
719719

720+
FIXED_ZONE_ENCODING = (Encoding::UTF_8 if /mswin|mingw/.match?(RUBY_PLATFORM))
721+
720722
def assert_zone_encoding(time)
721723
zone = time.zone
722724
assert_predicate(zone, :valid_encoding?)
723725
if zone.ascii_only?
724726
assert_equal(Encoding::US_ASCII, zone.encoding)
725727
else
726-
enc = Encoding.find('locale')
728+
enc = FIXED_ZONE_ENCODING || Encoding.find('locale')
727729
assert_equal(enc, zone.encoding)
728730
end
729731
end

time.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
# include <sys/time.h>
3232
#endif
3333

34+
#if defined(HAVE_LOCALE_H)
35+
# include <locale.h>
36+
#endif
37+
3438
#include "id.h"
3539
#include "internal.h"
3640
#include "internal/array.h"
@@ -950,7 +954,11 @@ zone_str(const char *zone)
950954
str = rb_usascii_str_new(zone, len);
951955
}
952956
else {
957+
#if defined(_WIN32)
958+
str = rb_utf8_str_new(zone, len);
959+
#else
953960
str = rb_enc_str_new(zone, len, rb_locale_encoding());
961+
#endif
954962
}
955963
return rb_fstring(str);
956964
}
@@ -1649,11 +1657,15 @@ localtime_with_gmtoff_zone(const time_t *t, struct tm *result, long *gmtoff, VAL
16491657
if (zone) {
16501658
#if defined(HAVE_TM_ZONE)
16511659
*zone = zone_str(tm.tm_zone);
1660+
#elif defined(_WIN32)
1661+
{
1662+
char buf[64];
1663+
_locale_t utf8_locale = _create_locale(LC_TIME, ".65001");
1664+
size_t n = _strftime_l(buf, numberof(buf), "%Z", &tm, utf8_locale);
1665+
_free_locale(utf8_locale);
1666+
*zon B19E e = zone_str((ssize_t)n < 0 ? NULL : buf);
1667+
}
16521668
#elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
1653-
# if defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 140
1654-
# define tzname _tzname
1655-
# define daylight _daylight
1656-
# endif
16571669
/* this needs tzset or localtime, instead of localtime_r */
16581670
*zone = zone_str(tzname[daylight && tm.tm_isdst]);
16591671
#else

win32/Makefile.sub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ $(CONFIG_H): $(MKFILES) $(srcdir)/win32/Makefile.sub $(win_srcdir)/Makefile.sub
779779
#define HAVE_STRUCT_TIMEVAL 1
780780
!if $(MSC_VER) >= 1900
781781
#define HAVE_STRUCT_TIMESPEC
782+
#define HAVE_LOCALE_H 1
782783
!endif
783784
!if $(MSC_VER) >= 1600
784785
#define HAVE_INTTYPES_H 1

0 commit comments

Comments
 (0)
0