Project

General

Profile

Bug #13231

DateTime.strftime("%Z") does not return time zone abbreviation, returns hour and minute offset from UTC with colon

Added by thornomad (Damon Timm) over 3 years ago. Updated almost 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]
[ruby-core:79602]

Description

First time here; hope I capture everything.

It appears to me that either the documentation is incorrect or the expected functionality with DateTime.strftime("%Z").

From: http://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/DateTime.html#method-i-strftime

Time zone:
%z - Time zone as hour and minute offset from UTC (e.g. +0900)
%:z - hour and minute offset from UTC with a colon (e.g. +09:00)
%::z - hour, minute and second offset from UTC (e.g. +09:00:00)
%:::z - hour, minute and second offset from UTC
(e.g. +09, +09:30, +09:30:30)
%Z - Time zone abbreviation name or something similar information.

However, the last one on the list %Z appears to replicate %:z.

See output of each below (last one is unexpected):

~ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]
~ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> DateTime.now.strftime("%z")
=> "-0800"
irb(main):003:0> DateTime.now.strftime("%:z")
=> "-08:00"
irb(main):004:0> DateTime.now.strftime("%::z")
=> "-08:00:00"
irb(main):005:0> DateTime.now.strftime("%:::z")
=> "-08"
irb(main):006:0> DateTime.now.strftime("%Z")
=> "-08:00"

For me the expected behavior, based on the documentation, is the behavior shown by Time.strftime("%Z").

irb(main):008:0> Time.now.strftime("%Z")
=> "PST"

Thanks for your time.

Updated by natan3 (尚美 日高) over 3 years ago

I think it contains a deep problem.

$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Time.now.strftime('%Z')
=> "JST"
irb(main):003:0> DateTime.now.to_time.strftime('%Z')
=> ""
irb(main):004:0> 

So, for the moment, I will change the documentation here.
I changed this expression

%Z - Time zone abbreviation name or something similar information.

to

%Z - Time zone the same as above %:z (e.g. +09:00)

https://github.com/ruby/ruby/pull/1565

Updated by sorah (Sorah Fukumori) over 3 years ago

  • Assignee set to tadf (tadayoshi funaba)

Updated by naruse (Yui NARUSE) over 3 years ago

  • Assignee deleted (tadf (tadayoshi funaba))

DateTime wrongly calls timezone offset as "timezone".
It is considered as a documentation issue at this time.

Updated by stomar (Marcus Stollsteimer) over 3 years ago

I'd suggest

%Z - Equivalent to %:z (e.g. +09:00)

Updated by MSP-Greg (Greg L) over 3 years ago

Re docs, Time#zone seems to indicate that a time zone abbreviation is the return.

On my system (ruby 2.5.0dev (2017-03-19 trunk 58021) [x64-mingw32]), "Central Daylight Time" is returned, not "CDT". Probably OS dependent...

Updated by stomar (Marcus Stollsteimer) over 3 years ago

MSP-Greg (Greg L) wrote:

Re docs, Time#zone seems to indicate that a time zone abbreviation is the return.

On my system (ruby 2.5.0dev (2017-03-19 trunk 58021) [x64-mingw32]), "Central Daylight Time" is returned, not "CDT". Probably OS dependent...

Probably: for me it's an abbreviation (on Linux).

But I don't see the relevance for this issue, which is about DateTime#strftime.

#7

Updated by stomar (Marcus Stollsteimer) about 3 years ago

  • Status changed from Open to Closed

Applied in changeset trunk|r58643.


date_core.c: fix docs for %Z format

  • ext/date/date_core.c: [DOC] fix documentation for %Z format of {Date,DateTime}.strftime.

Reported by Damon Timm. Based on a patch by nano.
[ruby-core:79602] [Bug #13231] [Fix GH-1565]

#8

Updated by usa (Usaku NAKAMURA) about 3 years ago

  • Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: WONTFIX, 2.3: REQUIRED, 2.4: REQUIRED

Updated by usa (Usaku NAKAMURA) about 3 years ago

  • Backport changed from 2.2: WONTFIX, 2.3: REQUIRED, 2.4: REQUIRED to 2.2: WONTFIX, 2.3: DONE, 2.4: REQUIRED

ruby_2_3 r59237 merged revision(s) 58643.

Updated by nagachika (Tomoyuki Chikanaga) almost 3 years ago

  • Backport changed from 2.2: WONTFIX, 2.3: DONE, 2.4: REQUIRED to 2.2: WONTFIX, 2.3: DONE, 2.4: DONE

ruby_2_4 r59397 merged revision(s) 58643.

Also available in: Atom PDF