extending strftime

Discussion in 'Ruby' started by Lin Wj, Jan 13, 2009.

  1. Lin Wj

    Lin Wj Guest

    can i change the options which is recognized by strftime
    eg:
    '%Y %m %d' will apply the format of YYYY mm dd

    can i do something like extending strftime to recognize 'YYYY mm dd'
    instead ?

    because right now , i am running the format string which i get(YYYY mm
    dd) through a regex and converting them to the parameters which strftime
    recognizes

    or please do suggest alternate methods
    --
    Posted via http://www.ruby-forum.com/.
    Lin Wj, Jan 13, 2009
    #1
    1. Advertising

  2. Lin Wj wrote:
    > can i change the options which is recognized by strftime
    > eg:
    > '%Y %m %d' will apply the format of YYYY mm dd
    >
    > can i do something like extending strftime to recognize 'YYYY mm dd'
    > instead ?
    >
    > because right now , i am running the format string which i get(YYYY mm
    > dd) through a regex and converting them to the parameters which strftime
    > recognizes


    That sounds like a perfectly reasonable approach.

    If you really want to, you can monkey-patch the built-in class (I'd
    advise against this though, except in small self-contained scripts)

    class Time
    alias :eek:ld_strftime :strftime
    STRFMAP = {
    'YYYY' => '%Y',
    'mm' => '%m',
    'dd' => '%d',
    }
    STRFRE = /#{STRFMAP.keys.join("|")}/

    def strftime(arg)
    old_strftime(arg.gsub(STRFRE) { STRFMAP[$&] })
    end
    end

    puts Time.now.strftime("YYYY-mm-dd")

    Note that by not using the % escape syntax, you may end up with some
    subtle bugs:

    puts Time.now.strftime("Today's YYYY-mm-dd, buddy!")
    # => Today's 2009-01-13, bu13y!

    You can restrict the regular expression to require a word boundary:

    STRFRE = /\b(#{STRFMAP.keys.join("|")})\b/

    But in this case, a string like "YYYYmmdd" doesn't work.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 13, 2009
    #2
    1. Advertising

  3. Lin Wj

    Trans Guest

    On Jan 13, 6:41=A0am, Lin Wj <> wrote:
    > can i change the options which is recognized by strftime
    > eg:
    > '%Y %m %d' =A0will apply the format of YYYY mm dd
    >
    > can i do something like extending strftime to recognize 'YYYY mm dd'
    > instead ?


    why not write your own Time method? you don't need to override
    strftime.

    > because right now , i am running the format string which i get(YYYY mm
    > dd) through a regex and converting them to the parameters which strftime
    > recognizes
    >
    > or please do suggest alternate methods


    T.
    Trans, Jan 13, 2009
    #3
    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. Bryan O'Malley

    strftime not working as expected

    Bryan O'Malley, Feb 3, 2004, in forum: C++
    Replies:
    8
    Views:
    2,613
    Jerry Coffin
    Feb 17, 2004
  2. Bimal
    Replies:
    1
    Views:
    450
    Tim Prince
    Aug 20, 2003
  3. Richard Shea

    strftime - %a is always Monday ?

    Richard Shea, Dec 29, 2003, in forum: Python
    Replies:
    5
    Views:
    1,056
    Richard Shea
    Dec 30, 2003
  4. Allen Unueco

    time.strftime Timezone issue

    Allen Unueco, Jun 13, 2004, in forum: Python
    Replies:
    1
    Views:
    348
    Allen Unueco
    Jun 13, 2004
  5. Tim Peters

    RE: time.strftime Timezone issue

    Tim Peters, Jun 13, 2004, in forum: Python
    Replies:
    1
    Views:
    743
    Allen Unueco
    Jun 13, 2004
Loading...

Share This Page