Skip to content

Instantly share code, notes, and snippets.

@cpetschnig
Created September 21, 2010 09:25
Show Gist options
  • Save cpetschnig/589447 to your computer and use it in GitHub Desktop.
Save cpetschnig/589447 to your computer and use it in GitHub Desktop.
Show the possible tokens of Ruby Time#strftime
#!/usr/bin/env ruby
# one line version:
# t = Time.new; puts (65.upto(90).map{|i|s = "%%#{i.chr}: %#{i.chr}"; t.strftime(s).ljust(25) + t.strftime(s.downcase)}.join("\n"))
# Output:
#
# %A: Tuesday %a: Tue
# %B: September %b: Sep
# %C: 20 %c: Tue Sep 21 08:53:35 2010
# %D: 09/21/10 %d: 21
# %e: 21
# %F: 2010-09-21
# %G: 2010 %g: 10
# %H: 08 %h: Sep
# %I: 08
# %j: 264
# %k: 8
# %L: 103 %l: 8
# %M: 53 %m: 09
# %N: 103078000 %n: \n (newline)
#
# %P: am %p: AM
#
# %R: 08:53 %r: 08:53:35 AM
# %S: 35 %s: 1285052015
# %T: 08:53:35 %t:
# %U: 38 %u: 2
# %V: 38 %v: 21-SEP-2010
# %W: 38 %w: 2
# %X: 08:53:35 %x: 09/21/10
# %Y: 2010 %y: 10
# %Z: CEST %z: +0200
t = Time.new
result = 'A'.ord.upto('Z'.ord).map do |i|
str = "%#{i.chr}"
strf = t.strftime(str)
str_d = str.downcase
strf_d = t.strftime(str_d).sub("\n", "\\n (newline)")
(str == strf ? '' : " #{str}: #{strf}").ljust(25) +
(str_d == strf_d ? '' : " #{str_d}: #{strf_d}")
end.join("\n")
puts "\n" + result + "\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment