Project

General

Profile

Actions

Bug #22416

closed

Respect timezone abbreviation encodings in `Time#strftime`

Bug #22416: Respect timezone abbreviation encodings in `Time#strftime`

Added by nobu (Nobuyoshi Nakada) about 10 hours ago. Updated about 9 hours ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:127007]

Description

Time#strftime treats non-ASCII timezone abbreviations as if their bytes use the locale encoding. However, a timezone object's abbr method can return a String in another encoding. In that case, formatting %Z can produce corrupted text or a result with an unexpected encoding.

Timezone objects have been supported since Ruby 2.6.0, so this issue affects Ruby 2.6.0 and later.

Reproduction:

zone = Object.new
def zone.local_to_utc(t) = t
def zone.utc_to_local(t) = t
def zone.abbr(t) = "東京標準時".encode(Encoding::Shift_JIS)

time = Time.new(2020, 1, 1, 0, 0, 0, zone)
result = time.strftime("☆%Z")
p result
p result.encoding

Expected: result contains ☆東京標準時 and is valid UTF-8. Instead, the text may be corrupted because the abbreviation's bytes are interpreted using the locale encoding.

Actions

Also available in: PDF Atom