Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # File date/format.rb, line 200
- def strftime(fmt='%F')
- fmt.gsub(/%([-_0^#]+)?(\d+)?[EO]?(:{1,3}z|.)/m) do |m|
- f = {}
- s, w, c = $1, $2, $3
- if s
- s.scan(/./) do |k|
- case k
- when '-'; f[:p] = '-'
- when '_'; f[:p] = "\s"
- when '0'; f[:p] = '0'
- when '^'; f[:u] = true
- when '#'; f[:x] = true
- end
- end
- end
- if w
- f[:w] = w.to_i
- end
- case c
- when 'A'; emit_ad(DAYNAMES[wday], 0, f)
- when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f)
- when 'B'; emit_ad(MONTHNAMES[mon], 0, f)
- when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f)
- when 'C'; emit_sn((year / 100).floor, 2, f)
- when 'c'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
- when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
- when 'd'; emit_n(mday, 2, f)
- when 'e'; emit_a(mday, 2, f)
- when 'F'
- if m == '%F'
- format('%.4d-%02d-%02d', year, mon, mday) # 4p
- else
- emit_a(strftime('%Y-%m-%d'), 0, f)
- end
- when 'G'; emit_sn(cwyear, 4, f)
- when 'g'; emit_n(cwyear % 100, 2, f)
- when 'H'; emit_n(hour, 2, f)
- when 'h'; emit_ad(strftime('%b'), 0, f)
- when 'I'; emit_n((hour % 12).nonzero? || 12, 2, f)
- when 'j'; emit_n(yday, 3, f)
- when 'k'; emit_a(hour, 2, f)
- when 'L'
- emit_n((sec_fraction / (1.to_r/86400/(10**3))).round, 3, f)
- when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
- when 'M'; emit_n(min, 2, f)
- when 'm'; emit_n(mon, 2, f)
- when 'N'
- emit_n((sec_fraction / (1.to_r/86400/(10**9))).round, 9, f)
- when 'n'; "\n"
- when 'P'; emit_ad(strftime('%p').downcase, 0, f)
- when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
- when 'Q'
- d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
- s = (d * 86400*10**3).to_i
- emit_sn(s, 1, f)
- when 'R'; emit_a(strftime('%H:%M'), 0, f)
- when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
- when 'S'; emit_n(sec, 2, f)
- when 's'
- d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
- s = (d * 86400).to_i
- emit_sn(s, 1, f)
- when 'T'
- if m == '%T'
- format('%02d:%02d:%02d', hour, min, sec) # 4p
- else
- emit_a(strftime('%H:%M:%S'), 0, f)
- end
- when 't'; "\t"
- when 'U', 'W'
- emit_n(if c == 'U' then wnum0 else wnum1 end, 2, f)
- when 'u'; emit_n(cwday, 1, f)
- when 'V'; emit_n(cweek, 2, f)
- when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
- when 'w'; emit_n(wday, 1, f)
- when 'X'; emit_a(strftime('%H:%M:%S'), 0, f)
- when 'x'; emit_a(strftime('%m/%d/%y'), 0, f)
- when 'Y'; emit_sn(year, 4, f)
- when 'y'; emit_n(year % 100, 2, f)
- when 'Z'; emit_au(strftime('%:z'), 0, f)
- when /\A(:{0,3})z/
- t = $1.size
- sign = if offset < 0 then -1 else +1 end
- fr = offset.abs
- hh, fr = fr.divmod(1.to_r/24)
- mm, fr = fr.divmod(1.to_r/1440)
- ss, fr = fr.divmod(1.to_r/86400)
- if t == 3
- if ss.nonzero? then t = 2
- elsif mm.nonzero? then t = 1
- else t = -1
- end
- end
- case t
- when -1
- tail = []
- sep = ''
- when 0
- f[:w] -= 2 if f[:w]
- tail = ['%02d' % mm]
- sep = ''
- when 1
- f[:w] -= 3 if f[:w]
- tail = ['%02d' % mm]
- sep = ':'
- when 2
- f[:w] -= 6 if f[:w]
- tail = ['%02d' % mm, '%02d' % ss]
- sep = ':'
- end
- ([emit_z(sign * hh, 2, f)] + tail).join(sep)
- when '%'; emit_a('%', 0, f)
- when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
- when '1'
- if $VERBOSE
- warn("warning: strftime: %1 is deprecated; forget this")
- end
- emit_n(jd, 1, f)
- when '2'
- if $VERBOSE
- warn("warning: strftime: %2 is deprecated; use '%Y-%j'")
- end
- emit_a(strftime('%Y-%j'), 0, f)
- when '3'
- if $VERBOSE
- warn("warning: strftime: %3 is deprecated; use '%F'")
- end
- emit_a(strftime('%F'), 0, f)
- else
- c
- end
- end
- end
Add Comment
Please, Sign In to add comment