Bug #5226

Encoding of Time#to_s

Added by Aaron Patterson over 2 years ago. Updated over 2 years ago.

[ruby-core:39092]
Status:Closed
Priority:Normal
Assignee:-
Category:M17N
Target version:-
ruby -v:ruby 1.9.4dev (2011-08-25 trunk 33051) [x86_64-darwin11.1.0] Backport:

Description

I think the encoding of Time#tos should default to US-ASCII, but respect Encoding.defaultinternal. Today it always returns a binary string:

irb(main):001:0> Time.now.tos.encoding
=> #Encoding:ASCII-8BIT
irb(main):002:0> Encoding.default
internal = Encoding::UTF8
=> #Encoding:UTF-8
irb(main):003:0> Time.now.to
s.encoding
=> #Encoding:ASCII-8BIT
irb(main):004:0>

I've attached a patch to make Time#tos respect Encoding.defaultinternal. The result is:

irb(main):001:0> Encoding.defaultinternal
=> nil
irb(main):002:0> Time.now.to
s.encoding
=> #Encoding:US-ASCII
irb(main):003:0> Encoding.defaultinternal = Encoding::UTF8
=> #Encoding:UTF-8
irb(main):004:0> Time.now.to_s.encoding
=> #Encoding:UTF-8
irb(main):005:0>

time_enc.patch Magnifier (1.32 KB) Aaron Patterson, 08/25/2011 06:02 AM

Associated revisions

Revision 33094
Added by Yui NARUSE over 2 years ago

  • strftime.c (rbstrftimewith_timespec): get enc argument to specify
    the encoding of the format. On Windows (at least Japanese Windows),
    Time#strftime("%Z") includes non ASCII in locale encoding (CP932).
    So convert locale to default internal. [Bug #5226]

  • strftime.c (rb_strftime): ditto.

  • strftime.c (rbstrftimetimespec): ditto.

  • internal.h (rbstrftimetimespec): follow above.

  • time.c (rbstrftimealloc): ditto.

  • time.c (strftimev): ditto.

  • time.c (time_strftime): ditto.

  • time.c (timetos): the resulted string of Time#to_s is always
    ascii only, so this should be US-ASCII.

  • time.c (time_asctime): ditto.

History

#1 Updated by Shyouhei Urabe over 2 years ago

  • Category set to M17N

+1.

As of 1.9.4dev it seems there exists no chance for Time#to_s to produce M17N-problematic result.

#2 Updated by Aaron Patterson over 2 years ago

OK. I will apply this patch.

#3 Updated by Yui NARUSE over 2 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r33094.
Aaron, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • strftime.c (rbstrftimewith_timespec): get enc argument to specify
    the encoding of the format. On Windows (at least Japanese Windows),
    Time#strftime("%Z") includes non ASCII in locale encoding (CP932).
    So convert locale to default internal. [Bug #5226]

  • strftime.c (rb_strftime): ditto.

  • strftime.c (rbstrftimetimespec): ditto.

  • internal.h (rbstrftimetimespec): follow above.

  • time.c (rbstrftimealloc): ditto.

  • time.c (strftimev): ditto.

  • time.c (time_strftime): ditto.

  • time.c (timetos): the resulted string of Time#to_s is always
    ascii only, so this should be US-ASCII.

  • time.c (time_asctime): ditto.

Also available in: Atom PDF