method

strftime

v1_9_2_180 - Show latest stable - Class: Date
strftime(fmt='%F')
public

No documentation available.

# File lib/date/format.rb, line 215
  def strftime(fmt='%F')
    fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/) do
      f = {}
      m = $&
      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', 'EC'; emit_sn((year / 100).floor, 2, f)
      when 'c', 'Ec'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
      when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
      when 'd', 'Od'; emit_n(mday, 2, f)
      when 'e', 'Oe'; 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', 'OH'; emit_n(hour, 2, f)
      when 'h'; emit_ad(strftime('%b'), 0, f)
      when 'I', 'OI'; emit_n((hour % 12).nonzero? || 12, 2, f)
      when 'j'; emit_n(yday, 3, f)
      when 'k'; emit_a(hour, 2, f)
      when 'L'
        f[:p] = nil
        w = f[:w] || 3
        u = 10**w
        emit_n((sec_fraction * u).floor, w, f)
      when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
      when 'M', 'OM'; emit_n(min, 2, f)
      when 'm', 'Om'; emit_n(mon, 2, f)
      when 'N'
        f[:p] = nil
        w = f[:w] || 9
        u = 10**w
        emit_n((sec_fraction * u).floor, w, f)
      when 'n'; emit_a("\n", 0, f)
      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'
        s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round
        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', 'OS'; emit_n(sec, 2, f)
      when 's'
        s = ((ajd - UNIX_EPOCH_IN_AJD) / SECONDS_IN_DAY).round
        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'; emit_a("\t", 0, f)
      when 'U', 'W', 'OU', 'OW'
        emit_n(if c[-1,1] == 'U' then wnum0 else wnum1 end, 2, f)
      when 'u', 'Ou'; emit_n(cwday, 1, f)
      when 'V', 'OV'; emit_n(cweek, 2, f)
      when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
      when 'w', 'Ow'; emit_n(wday, 1, f)
      when 'X', 'EX'; emit_a(strftime('%H:%M:%S'), 0, f)
      when 'x', 'Ex'; emit_a(strftime('%m/%d/%y'), 0, f)
      when 'Y', 'EY'; emit_sn(year, 4, f)
      when 'y', 'Ey', 'Oy'; 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
        ss = fr.div(SECONDS_IN_DAY) # 4p
        hh, ss = ss.divmod(3600)
        mm, ss = ss.divmod(60)
        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)
      else
        m
      end
    end
  end

2Notes

see also – similar methods

roryokane · Jun 28, 2012

See also Time#strftime and DateTime#strftime . (They work similarly, but have different APIdock notes.)

Bug in Ruby or this documentation

panzi · May 16, 2013

%Q doesn't return microseconds but milliseconds! Use %s%6N for microseconds.