# Extending strftime

**URL:** <https://www.ruby-forum.com/t/extending-strftime/157592>\
**Category:** Ruby\
**Created:** [January 13, 2009, 12:42pm UTC](https://www.ruby-forum.com/t/extending-strftime/157592 "2009-01-13T12:42:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [January 13, 2009, 12:42pm UTC](https://www.ruby-forum.com/t/extending-strftime/157592/1 "2009-01-13T12:42:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [January 13, 2009, 1:14pm UTC](https://www.ruby-forum.com/t/extending-strftime/157592/2 "2009-01-13T13:14:31Z")

</div>

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 :old\_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.

---

<div class="post-metadata">

**Author:** ![wanyixue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/958977/32.png) [@wanyixue](https://www.ruby-forum.com/u/wanyixue)\
**Post date:** [January 13, 2009, 1:37pm UTC](https://www.ruby-forum.com/t/extending-strftime/157592/3 "2009-01-13T13:37:45Z")

</div>

On Jan 13, 6:41 am, Lin Wj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 ?

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.
