working around frozen hash

Discussion in 'Ruby' started by Dany, Jun 30, 2008.

  1. Dany

    Dany Guest

    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
    Dany, Jun 30, 2008
    #1
    1. Advertising

  2. Dany

    F. Senault Guest

    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)
    F. Senault, Jun 30, 2008
    #2
    1. Advertising

  3. Dany

    Robert Dober Guest

    On Mon, Jun 30, 2008 at 8:12 PM, Dany <> 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.blogspot.com/

    ---
    AALST (n.) One who changes his name to be further to the front
    D.Adams; The Meaning of LIFF
    Robert Dober, Jun 30, 2008
    #3
  4. Dany

    Dany Guest

    >
    > 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, Jun 30, 2008
    #4
  5. Dany

    Dany Guest

    > <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
    Dany, Jun 30, 2008
    #5
  6. Dany

    Robert Dober Guest

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



    --
    http://ruby-smalltalk.blogspot.com/

    ---
    AALST (n.) One who changes his name to be further to the front
    D.Adams; The Meaning of LIFF
    Robert Dober, Jul 1, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piet
    Replies:
    0
    Views:
    495
  2. Network/Software Buyer
    Replies:
    0
    Views:
    396
    Network/Software Buyer
    May 23, 2010
  3. est
    Replies:
    1
    Views:
    531
  4. rp
    Replies:
    1
    Views:
    478
    red floyd
    Nov 10, 2011
  5. LC Geldenhuys
    Replies:
    5
    Views:
    243
    Robert Klemme
    Feb 18, 2004
Loading...

Share This Page