Time default format: How to override

(Jesper Rønn-Jensen) #1

I'm working on a Rails plugin that localizes default Time/Date formats.
I want to override the standard month and daynames with local variants,
but for some reason I can't override the default Time month and
daynames.

irb(main):010:0> Time.new
=> Mon Aug 21 20:47:52 Romance Daylight Time 2006
irb(main):011:0> Time::RFC2822_DAY_NAME
=> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
irb(main):012:0> Time::RFC2822_DAY_NAME = %{søn man tir ons tor fre
lør}
(irb):12: warning: already initialized constant RFC2822_DAY_NAME
irb(main):014:0> Time.new
=> Mon Aug 21 20:49:36 Romance Daylight Time 2006

As you see from the above: The dayname is not overridden. How do I
override it?

However, I can easily override the locale format (at leas when I do it
from Rails). But that doesn't help much when month and daynames are
still in English.

Please let me know your thoughts on how I change English month and
daynames to local.

(13) #2

Hi,

I am using Date library like this:

require 'date'

Date::DAYNAMES.replace %w(Svetdiena Pirmdiena Otrdiena Tresdiena
Ceturtdiena Piektdiena Sestdiena)

puts Date.today.strftime('%A') #=> Otrdiena

Otrdiena is Tuesday only in Latvian :wink:

···

--
Martins

On 8/22/06, Jesper Rønn-Jensen <jesperrr@gmail.com> wrote:

I'm working on a Rails plugin that localizes default Time/Date formats.
I want to override the standard month and daynames with local variants,
but for some reason I can't override the default Time month and
daynames.

irb(main):010:0> Time.new
=> Mon Aug 21 20:47:52 Romance Daylight Time 2006
irb(main):011:0> Time::RFC2822_DAY_NAME
=> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
irb(main):012:0> Time::RFC2822_DAY_NAME = %{søn man tir ons tor fre
lør}
(irb):12: warning: already initialized constant RFC2822_DAY_NAME
irb(main):014:0> Time.new
=> Mon Aug 21 20:49:36 Romance Daylight Time 2006

As you see from the above: The dayname is not overridden. How do I
override it?

However, I can easily override the locale format (at leas when I do it
from Rails). But that doesn't help much when month and daynames are
still in English.

Please let me know your thoughts on how I change English month and
daynames to local.

(Jesper Rønn-Jensen) #3

Martins wrote:

I am using Date library like this:

require 'date'
Date::DAYNAMES.replace %w(Svetdiena Pirmdiena Otrdiena Tresdiena
Ceturtdiena Piektdiena Sestdiena)
puts Date.today.strftime('%A') #=> Otrdiena
Otrdiena is Tuesday only in Latvian :wink:

Thanks Martins.
I know how to modify the Date with this and use the same technique
already in my Rails plugin Localization Simplified.

Only, on the Time class these constants don't exist but somehow on
Time.new they're used anyhow.

Problem is, that it doesn't help modifying constants on Date, because
when I do that, Time class has already access to the original DAYNAMES.

Try for yourself Time.new in your code.

Any suggestions on how to modify day/month names on Time class???

(13) #4

I see. Then you should look at your OS locale settings, because
Time#to_s (and maybe Time#strftime & etc too) are using strftime()
function to create formetted time string ...

···

--
Martins

On 8/22/06, jesperrr@gmail.com <jesperrr@gmail.com> wrote:

Martins wrote:
> I am using Date library like this:
>
> require 'date'
> Date::DAYNAMES.replace %w(Svetdiena Pirmdiena Otrdiena Tresdiena
> Ceturtdiena Piektdiena Sestdiena)
> puts Date.today.strftime('%A') #=> Otrdiena
> Otrdiena is Tuesday only in Latvian :wink:

Thanks Martins.
I know how to modify the Date with this and use the same technique
already in my Rails plugin Localization Simplified.

Only, on the Time class these constants don't exist but somehow on
Time.new they're used anyhow.

Problem is, that it doesn't help modifying constants on Date, because
when I do that, Time class has already access to the original DAYNAMES.

Try for yourself Time.new in your code.

Any suggestions on how to modify day/month names on Time class???