class FluidTime
Public Class Methods
demo()
click to toggle source
# File lib/fluid-time.rb, line 16 def self.demo %w{ to_time to_date a.ddd.wday A.tday.weekday b.mmm.smonth B.month c full d.dd.day.num e.mday H.h24.mhour.hour24 I.h12.hour.hour12 l j.yday.day_of_year m.mm.mon.month_of_year M.min.minute hour.p hour.pm.txt('or').hour.am hour.PM.txt('or').hour.AM S.sec.second U W w x.autodate X.autotime y.yy.sy Y.year Z.zone.timezone date time db usdate W.th.comma.space._('week') date.hour.colon.min.today.pm year.mmm.d.th time.xs.p.down txt('01').xz.PM.zone txt('1:00').xz.pm.zone _('braces').lb.lp.lc.rc.rp.rb _('symbols').lb.tp.ts.tc.ts.tn.ts.td.rb _('symbols').lb.sp.ss.sc.ss.sn.ss.sd.rb period.space.comma colon.space.dash.space.slash }.each {|ex| "FluidTime.new. #{ex}.to_s".tap {|str| p str + (" %#{80 - str.length}s" % eval(str))} } nil end
new(input = Time.now)
click to toggle source
# File lib/fluid-time.rb, line 6 def initialize(input = Time.now) @build = "" @last_was_space = false @time = nil @time = input if input.is_a?(Time) @time = input.to_time if input.is_a?(Date) && input.methods.include?(:to_time) @time = Time.parse(input.to_s) if @time.nil? end
Public Instance Methods
<=>(o)
click to toggle source
# File lib/fluid-time.rb, line 83 def <=>(o) self.to_s <=> o.to_s end
A()
click to toggle source
# File lib/fluid-time.rb, line 108 def A; cat @time.strftime('%A') end
B()
click to toggle source
# File lib/fluid-time.rb, line 118 def B; cat @time.strftime('%B') end
Also aliased as: month
H()
click to toggle source
# File lib/fluid-time.rb, line 136 def H; cat @time.strftime('%H') end
I()
click to toggle source
# File lib/fluid-time.rb, line 142 def I; cat @time.strftime('%I') end
M()
click to toggle source
# File lib/fluid-time.rb, line 163 def M; cat @time.strftime('%M') end
S()
click to toggle source
# File lib/fluid-time.rb, line 179 def S; cat @time.strftime('%S') end
U()
click to toggle source
# File lib/fluid-time.rb, line 184 def U; cat @time.strftime('%U') end
W()
click to toggle source
starting with the first Sunday
as the first day of the first week (00..53)
# File lib/fluid-time.rb, line 190 def W; cat @time.strftime('%W') end
X()
click to toggle source
# File lib/fluid-time.rb, line 204 def X; cat @time.strftime('%X') end
Also aliased as: autotime
Y()
click to toggle source
# File lib/fluid-time.rb, line 213 def Y; cat @time.strftime('%Y') end
Also aliased as: year
Y_m_d()
click to toggle source
# File lib/fluid-time.rb, line 303 def Y_m_d self.Y.dash.mm.dash.d end
Ymd()
click to toggle source
# File lib/fluid-time.rb, line 299 def Ymd self.Y.xs.mm.xs.d end
Z()
click to toggle source
# File lib/fluid-time.rb, line 217 def Z; cat @time.strftime('%Z') end
a()
click to toggle source
Formats
# File lib/fluid-time.rb, line 103 def a; cat @time.strftime('%a') end
b()
click to toggle source
# File lib/fluid-time.rb, line 113 def b; cat @time.strftime('%b') end
c()
click to toggle source
# File lib/fluid-time.rb, line 122 def c; cat @time.strftime('%c') end
Also aliased as: full
cat(str, sep = ' ')
click to toggle source
# File lib/fluid-time.rb, line 87 def cat(str, sep = ' ') @last_was_space = ' ' == str.to_s @build += str.to_s + sep self end
colon()
click to toggle source
# File lib/fluid-time.rb, line 275 def colon; xs.txt ':', '' end
comma()
click to toggle source
# File lib/fluid-time.rb, line 274 def comma; xs.txt ',', '' end
d()
click to toggle source
# File lib/fluid-time.rb, line 126 def d; cat @time.strftime('%d') end
dash()
click to toggle source
# File lib/fluid-time.rb, line 276 def dash; xs.txt '-', '' end
date()
click to toggle source
# File lib/fluid-time.rb, line 311 def date self.mon.slash.day.slash.year end
db()
click to toggle source
Presets
# File lib/fluid-time.rb, line 291 def db self.Y.m.d.H.I.S end
down()
click to toggle source
# File lib/fluid-time.rb, line 244 def down set(@build.split.tap {|parts| parts << parts.pop.downcase}.join(' ')) end
Also aliased as: lower
e()
click to toggle source
# File lib/fluid-time.rb, line 132 def e; cat @time.strftime('%e') end
Also aliased as: mday
j()
click to toggle source
included for completeness, should just use hour.xz (strip zero)
# File lib/fluid-time.rb, line 152 def j; cat @time.strftime('%j') end
Also aliased as: yday, day_of_year
l()
click to toggle source
# File lib/fluid-time.rb, line 148 def l; cat @time.strftime('%l') end
lbrace()
click to toggle source
# File lib/fluid-time.rb, line 279 def lbrace; xs.txt '[', '' end
Also aliased as: lb
lcurve()
click to toggle source
# File lib/fluid-time.rb, line 282 def lcurve; xs.txt '{', '' end
Also aliased as: lc
lparen()
click to toggle source
# File lib/fluid-time.rb, line 285 def lparen; xs.txt '(', '' end
Also aliased as: lp
m()
click to toggle source
# File lib/fluid-time.rb, line 157 def m; cat @time.strftime('%m') end
p()
click to toggle source
# File lib/fluid-time.rb, line 168 def p; cat @time.strftime('%p') end
period()
click to toggle source
# File lib/fluid-time.rb, line 273 def period; xs.txt '.', '' end
pm()
click to toggle source
# File lib/fluid-time.rb, line 172 def pm; self.p.down end
Also aliased as: am
rbrace()
click to toggle source
# File lib/fluid-time.rb, line 280 def rbrace; xs.txt ']', '' end
Also aliased as: rb
rcurve()
click to toggle source
# File lib/fluid-time.rb, line 283 def rcurve; xs.txt '}', '' end
Also aliased as: rc
rparen()
click to toggle source
# File lib/fluid-time.rb, line 286 def rparen; xs.txt ')', '' end
Also aliased as: rp
set(str, sep = ' ')
click to toggle source
# File lib/fluid-time.rb, line 95 def set(str, sep = ' ') @build = str.to_s + sep self end
slash()
click to toggle source
# File lib/fluid-time.rb, line 277 def slash; xs.txt '/', '' end
space()
click to toggle source
Symbols (couldn’t decide between: t for text and s for symbol)
# File lib/fluid-time.rb, line 272 def space; txt ' ','' end
strip()
click to toggle source
# File lib/fluid-time.rb, line 263 def strip @build.strip! unless @last_was_space self end
Also aliased as: xs
strip_zero()
click to toggle source
# File lib/fluid-time.rb, line 254 def strip_zero set(@build.strip.split.tap do |parts| parts << parts.pop.tap { |last| last.replace(last.gsub(/^0/,'').gsub(':00','')) } end.join(' ')) end
th()
click to toggle source
Modifiers
# File lib/fluid-time.rb, line 224 def th set(@build.strip.split.tap do |parts| parts << parts.pop.tap { |last| last.replace(numeric?(last) ? ordinalize(last) : last) } end.join(' ')) end
time()
click to toggle source
# File lib/fluid-time.rb, line 307 def time self.hour.colon.minute.colon.second end
to_date()
click to toggle source
# File lib/fluid-time.rb, line 79 def to_date Date.new(@time.year, @time.month, @time.day) end
to_s()
click to toggle source
Helpers
# File lib/fluid-time.rb, line 69 def to_s hold = @build.strip @build = "" '' == hold ? @time.to_s : hold end
to_time()
click to toggle source
# File lib/fluid-time.rb, line 75 def to_time @time end
up()
click to toggle source
# File lib/fluid-time.rb, line 249 def up set(@build.split.tap {|parts| parts << parts.pop.upcase}.join(' ')) end
Also aliased as: upper
usdate()
click to toggle source
# File lib/fluid-time.rb, line 295 def usdate self.mm.slash.d.slash.Y end
w()
click to toggle source
starting with the first Monday
as the first day of the first week (00..53)
# File lib/fluid-time.rb, line 197 def w; cat @time.strftime('%w') end
x()
click to toggle source
# File lib/fluid-time.rb, line 200 def x; cat @time.strftime('%x') end
Also aliased as: autodate
xpm()
click to toggle source
# File lib/fluid-time.rb, line 175 def xpm; self.xz.xs.pm end
Also aliased as: xam
y()
click to toggle source
# File lib/fluid-time.rb, line 208 def y; cat @time.strftime('%y') end
ytt()
click to toggle source
# File lib/fluid-time.rb, line 230 def ytt set(@build.gsub('-', '/').split.tap do |parts| parts.each_with_index do |part, index| date = Date.parse(part) rescue nil parts[index] = 'Today' if date.is_a?(Date) && date.to_s == Date.today.to_s parts[index] = 'Tomorrow' if date.is_a?(Date) && date.to_s == (Date.today + 1).to_s parts[index] = 'Yesterday' if date.is_a?(Date) && date.to_s == (Date.today - 1).to_s end end.join(' ')) end
Private Instance Methods
numeric?(string)
click to toggle source
# File lib/fluid-time.rb, line 320 def numeric?(string) true if Float(string) rescue false end
ordinalize(number)
click to toggle source
# File lib/fluid-time.rb, line 324 def ordinalize(number) return "#{number}th" if (11..13).include?(number.to_i % 100) case number.to_i % 10 when 1; "#{number}st" when 2; "#{number}nd" when 3; "#{number}rd" else "#{number}th" end end