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
Also aliased as: tday, weekday
AM()
Alias for: p
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
Also aliased as: h24, mhour, hour24
I() click to toggle source
# File lib/fluid-time.rb, line 142
def I; cat @time.strftime('%I') end
Also aliased as: h12, hour, hour12
M() click to toggle source
# File lib/fluid-time.rb, line 163
def M; cat @time.strftime('%M') end
Also aliased as: min, minute
PM()
Alias for: p
S() click to toggle source
# File lib/fluid-time.rb, line 179
def S; cat @time.strftime('%S') end
Also aliased as: sec, second
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
Also aliased as: zone, timezone
_(str, sep = ' ')
Alias for: cat
a() click to toggle source

Formats

# File lib/fluid-time.rb, line 103
def a; cat @time.strftime('%a') end
Also aliased as: ddd, wday
am()
Alias for: pm
autodate()
Alias for: x
autotime()
Alias for: X
b() click to toggle source
# File lib/fluid-time.rb, line 113
def b; cat @time.strftime('%b') end
Also aliased as: mmm, smonth
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
Also aliased as: txt, _
colon() click to toggle source
# File lib/fluid-time.rb, line 275
def colon;  xs.txt ':', '' end
Also aliased as: tn, sn
comma() click to toggle source
# File lib/fluid-time.rb, line 274
def comma;  xs.txt ',', '' end
Also aliased as: tc, sc
d() click to toggle source
# File lib/fluid-time.rb, line 126
def d; cat @time.strftime('%d') end
Also aliased as: dd, day, num
dash() click to toggle source
# File lib/fluid-time.rb, line 276
def dash;   xs.txt '-', '' end
Also aliased as: td, sd
date() click to toggle source
# File lib/fluid-time.rb, line 311
def date
  self.mon.slash.day.slash.year
end
day()
Alias for: d
day_of_year()
Alias for: j
db() click to toggle source

Presets

# File lib/fluid-time.rb, line 291
def db
  self.Y.m.d.H.I.S
end
dd()
Alias for: d
ddd()
Alias for: a
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
full()
Alias for: c
h12()
Alias for: I
h24()
Alias for: H
hour()
Alias for: I
hour12()
Alias for: I
hour24()
Alias for: H
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
lb()
Alias for: lbrace
lbrace() click to toggle source
# File lib/fluid-time.rb, line 279
def lbrace;  xs.txt '[', '' end
Also aliased as: lb
lc()
Alias for: lcurve
lcurve() click to toggle source
# File lib/fluid-time.rb, line 282
def lcurve;  xs.txt '{', '' end
Also aliased as: lc
lower()
Alias for: down
lp()
Alias for: lparen
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
Also aliased as: mm, mon, month_of_year
mday()
Alias for: e
mhour()
Alias for: H
min()
Alias for: M
minute()
Alias for: M
mm()
Alias for: m
mmm()
Alias for: b
mon()
Alias for: m
month()
Alias for: B
month_of_year()
Alias for: m
nozero()
Alias for: strip_zero
num()
Alias for: d
p() click to toggle source
# File lib/fluid-time.rb, line 168
def p; cat @time.strftime('%p') end
Also aliased as: AM, PM
period() click to toggle source
# File lib/fluid-time.rb, line 273
def period; xs.txt '.', '' end
Also aliased as: tp, sp
pm() click to toggle source
# File lib/fluid-time.rb, line 172
def pm; self.p.down end
Also aliased as: am
rb()
Alias for: rbrace
rbrace() click to toggle source
# File lib/fluid-time.rb, line 280
def rbrace;  xs.txt ']', '' end
Also aliased as: rb
rc()
Alias for: rcurve
rcurve() click to toggle source
# File lib/fluid-time.rb, line 283
def rcurve;  xs.txt '}', '' end
Also aliased as: rc
rp()
Alias for: rparen
rparen() click to toggle source
# File lib/fluid-time.rb, line 286
def rparen;  xs.txt ')', '' end
Also aliased as: rp
sc()
Alias for: comma
sd()
Alias for: dash
sec()
Alias for: S
second()
Alias for: S
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
sl()
Alias for: slash
slash() click to toggle source
# File lib/fluid-time.rb, line 277
def slash;  xs.txt '/', '' end
Also aliased as: tl, sl
smonth()
Alias for: b
sn()
Alias for: colon
sp()
Alias for: period
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
Also aliased as: ts, ss
ss()
Alias for: space
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
Also aliased as: xz, x0, nozero
sy()
Alias for: y
tc()
Alias for: comma
td()
Alias for: dash
tday()
Alias for: A
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
timezone()
Alias for: Z
tl()
Alias for: slash
tn()
Alias for: colon
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
today()
Alias for: ytt
tomorrow()
Alias for: ytt
tp()
Alias for: period
ts()
Alias for: space
txt(str, sep = ' ')
Alias for: cat
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
upper()
Alias for: up
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
wday()
Alias for: a
weekday()
Alias for: A
x() click to toggle source
# File lib/fluid-time.rb, line 200
def x; cat @time.strftime('%x') end
Also aliased as: autodate
x0()
Alias for: strip_zero
xam()
Alias for: xpm
xpm() click to toggle source
# File lib/fluid-time.rb, line 175
def xpm; self.xz.xs.pm end
Also aliased as: xam
xs()
Alias for: strip
xz()
Alias for: strip_zero
y() click to toggle source
# File lib/fluid-time.rb, line 208
def y; cat @time.strftime('%y') end
Also aliased as: yy, sy
yday()
Alias for: j
year()
Alias for: Y
yesterday()
Alias for: ytt
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
Also aliased as: today, tomorrow, yesterday
yy()
Alias for: y
zone()
Alias for: Z

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