Guest User

Untitled

a guest
Feb 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. # File date/format.rb, line 200
  2. def strftime(fmt='%F')
  3. fmt.gsub(/%([-_0^#]+)?(\d+)?[EO]?(:{1,3}z|.)/m) do |m|
  4. f = {}
  5. s, w, c = $1, $2, $3
  6. if s
  7. s.scan(/./) do |k|
  8. case k
  9. when '-'; f[:p] = '-'
  10. when '_'; f[:p] = "\s"
  11. when '0'; f[:p] = '0'
  12. when '^'; f[:u] = true
  13. when '#'; f[:x] = true
  14. end
  15. end
  16. end
  17. if w
  18. f[:w] = w.to_i
  19. end
  20. case c
  21. when 'A'; emit_ad(DAYNAMES[wday], 0, f)
  22. when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f)
  23. when 'B'; emit_ad(MONTHNAMES[mon], 0, f)
  24. when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f)
  25. when 'C'; emit_sn((year / 100).floor, 2, f)
  26. when 'c'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
  27. when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
  28. when 'd'; emit_n(mday, 2, f)
  29. when 'e'; emit_a(mday, 2, f)
  30. when 'F'
  31. if m == '%F'
  32. format('%.4d-%02d-%02d', year, mon, mday) # 4p
  33. else
  34. emit_a(strftime('%Y-%m-%d'), 0, f)
  35. end
  36. when 'G'; emit_sn(cwyear, 4, f)
  37. when 'g'; emit_n(cwyear % 100, 2, f)
  38. when 'H'; emit_n(hour, 2, f)
  39. when 'h'; emit_ad(strftime('%b'), 0, f)
  40. when 'I'; emit_n((hour % 12).nonzero? || 12, 2, f)
  41. when 'j'; emit_n(yday, 3, f)
  42. when 'k'; emit_a(hour, 2, f)
  43. when 'L'
  44. emit_n((sec_fraction / (1.to_r/86400/(10**3))).round, 3, f)
  45. when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
  46. when 'M'; emit_n(min, 2, f)
  47. when 'm'; emit_n(mon, 2, f)
  48. when 'N'
  49. emit_n((sec_fraction / (1.to_r/86400/(10**9))).round, 9, f)
  50. when 'n'; "\n"
  51. when 'P'; emit_ad(strftime('%p').downcase, 0, f)
  52. when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
  53. when 'Q'
  54. d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
  55. s = (d * 86400*10**3).to_i
  56. emit_sn(s, 1, f)
  57. when 'R'; emit_a(strftime('%H:%M'), 0, f)
  58. when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
  59. when 'S'; emit_n(sec, 2, f)
  60. when 's'
  61. d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
  62. s = (d * 86400).to_i
  63. emit_sn(s, 1, f)
  64. when 'T'
  65. if m == '%T'
  66. format('%02d:%02d:%02d', hour, min, sec) # 4p
  67. else
  68. emit_a(strftime('%H:%M:%S'), 0, f)
  69. end
  70. when 't'; "\t"
  71. when 'U', 'W'
  72. emit_n(if c == 'U' then wnum0 else wnum1 end, 2, f)
  73. when 'u'; emit_n(cwday, 1, f)
  74. when 'V'; emit_n(cweek, 2, f)
  75. when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
  76. when 'w'; emit_n(wday, 1, f)
  77. when 'X'; emit_a(strftime('%H:%M:%S'), 0, f)
  78. when 'x'; emit_a(strftime('%m/%d/%y'), 0, f)
  79. when 'Y'; emit_sn(year, 4, f)
  80. when 'y'; emit_n(year % 100, 2, f)
  81. when 'Z'; emit_au(strftime('%:z'), 0, f)
  82. when /\A(:{0,3})z/
  83. t = $1.size
  84. sign = if offset < 0 then -1 else +1 end
  85. fr = offset.abs
  86. hh, fr = fr.divmod(1.to_r/24)
  87. mm, fr = fr.divmod(1.to_r/1440)
  88. ss, fr = fr.divmod(1.to_r/86400)
  89. if t == 3
  90. if ss.nonzero? then t = 2
  91. elsif mm.nonzero? then t = 1
  92. else t = -1
  93. end
  94. end
  95. case t
  96. when -1
  97. tail = []
  98. sep = ''
  99. when 0
  100. f[:w] -= 2 if f[:w]
  101. tail = ['%02d' % mm]
  102. sep = ''
  103. when 1
  104. f[:w] -= 3 if f[:w]
  105. tail = ['%02d' % mm]
  106. sep = ':'
  107. when 2
  108. f[:w] -= 6 if f[:w]
  109. tail = ['%02d' % mm, '%02d' % ss]
  110. sep = ':'
  111. end
  112. ([emit_z(sign * hh, 2, f)] + tail).join(sep)
  113. when '%'; emit_a('%', 0, f)
  114. when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
  115. when '1'
  116. if $VERBOSE
  117. warn("warning: strftime: %1 is deprecated; forget this")
  118. end
  119. emit_n(jd, 1, f)
  120. when '2'
  121. if $VERBOSE
  122. warn("warning: strftime: %2 is deprecated; use '%Y-%j'")
  123. end
  124. emit_a(strftime('%Y-%j'), 0, f)
  125. when '3'
  126. if $VERBOSE
  127. warn("warning: strftime: %3 is deprecated; use '%F'")
  128. end
  129. emit_a(strftime('%F'), 0, f)
  130. else
  131. c
  132. end
  133. end
  134. end
Add Comment
Please, Sign In to add comment