[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

working around frozen hash

Dany

6/30/2008 6:12:00 PM

I need to parse the following date string (part of a file I need to
parse)

irb(main):001:0> date='Fri Jun 20 02:32:28 GST 2008'
=> "Fri Jun 20 02:32:28 GST 2008"

It seems like the DateTime parsing handles GST as Guam Standard time
UTC+10

irb(main):002:0> require 'date'
=> true
irb(main):003:0> DateTime.parse(date).zone
=> "+10:00"

In my case, GST is Gulf Standard Time UTC+4

My thought for a 'quick' workaround fix, was to modify the value in
the library/moduel

irb(main):004:0> puts Date::Format::ZONES['gst']
36000
=> nil
irb(main):005:0> Date::Format::ZONES['gst'] = 4*3600
TypeError: can't modify frozen hash
from (irb):5:in `[]='
from (irb):5

But as the output shows, the value is 'frozen' in that library. I am
looking for ideas how to workaround my issue. Any help appreciated.
BTW, I'm not a programmer and I have not done much with Ruby, yet.

Thanks,
Dany
5 Answers

F. Senault

6/30/2008 6:43:00 PM

0

Le 30 juin à 20:12, Dany a écrit :

> But as the output shows, the value is 'frozen' in that library. I am
> looking for ideas how to workaround my issue. Any help appreciated.
> BTW, I'm not a programmer and I have not done much with Ruby, yet.

Why don't you change the timezone in the sting beforehand ?

Something like :

>> date='Fri Jun 20 02:32:28 GST 2008'
=> "Fri Jun 20 02:32:28 GST 2008"
>> DateTime.parse(date.gsub(' GST ', ' UTC+04 ')).zone
=> "+04:00"

Of course, if you need to pase many dates at many points, you can either
redefine your own parsing routine (def DateTime.myparse(...)), or even
patch the DateTime.parse function with via alias and a new definition...

The only relative downside is that it will be a bit slower.

Fred
--
You always were the one to show me how Back then I couldn't do the
things that I can do now This thing is slowly taking me apart Grey
would be the color if I had a heart Come on tell me
(Nine Inch Nais, Something I Can Never Have)

Robert Dober

6/30/2008 7:26:00 PM

0

On Mon, Jun 30, 2008 at 8:12 PM, Dany <danyc18@gmail.com> wrote:
<snip>i
Date::Format::ZONES = Date::Format::ZONES.dup # ignore warning
Date::Format::ZONES['gst']= 42 * 343 - 42 /7
Date::Format::ZONES.freeze

That should do the trick ;)
HTH
Robert
--
http://ruby-smalltalk.blo...

---
AALST (n.) One who changes his name to be further to the front
D.Adams; The Meaning of LIFF

Dany

6/30/2008 7:40:00 PM

0

>
> Why don't you change the timezone in the sting beforehand ?
>
> Something like :
>
> >> date='Fri Jun 20 02:32:28 GST 2008'
>
> => "Fri Jun 20 02:32:28 GST 2008">> DateTime.parse(date.gsub(' GST ', ' UTC+04 ')).zone
>
> => "+04:00"
Because that is too easy and makes too much sense ;-) I did not know
about UTC+04 format. That is simple and easy to understand what is
going on. I am not too concern with performance, at this point.
Thanks!

>
> Of course, if you need to pase many dates at many points, you can either
> redefine your own parsing routine (def DateTime.myparse(...)), or even
> patch the DateTime.parse function with via alias and a new definition...
>
My thoughts were going into that direction... but I burned myself
going down a similar path for another 'library' before. I didn't
understand enough what I was 'patching' and my fix only cover some
specific cases, for me, but broke other.... I also don't like to mess
around with any code related to time and date. I thought my idea of
just changing a 'constant' in the library, was the less 'intrusive'
way to not change any code 'logic'.

Thanks again
Dany

Dany

6/30/2008 7:47:00 PM

0

> <snip>i
> Date::Format::ZONES = Date::Format::ZONES.dup # ignore warning
> Date::Format::ZONES['gst']= 42 * 343 - 42 /7
> Date::Format::ZONES.freeze>

Awesome. I was expecting a way to 'workaround' the freeze. Trying to
understand what is going.. I can't 'unfreeze' the hash/object, but I
can 're-assign' a constant (hence the warning).. So you assign the
original constant to new object/Hash which is a copy of the original
(dup). You can then change the value of interext and 're-freeze' (for
good measure).

Probably don't have the accurate terminology but hope I got the idea.
Thanks a lot!
Dany

Robert Dober

7/1/2008 6:40:00 AM

0

I believe that you described the process very well :).
Robert



--
http://ruby-smalltalk.blo...

---
AALST (n.) One who changes his name to be further to the front
D.Adams; The Meaning of LIFF