Class: FluidTime

Inherits:
Object
  • Object
show all
Defined in:
lib/fluid-time.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = Time.now) ⇒ FluidTime

Returns a new instance of FluidTime.



6
7
8
9
10
11
12
13
14
# 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

Class Method Details

.demoObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

Instance Method Details

#<=>(o) ⇒ Object



83
84
85
# File 'lib/fluid-time.rb', line 83

def <=>(o)
  self.to_s <=> o.to_s
end

#aObject Also known as: ddd, wday

Formats



103
# File 'lib/fluid-time.rb', line 103

def a; cat @time.strftime('%a') end

#AObject Also known as: tday, weekday

%A - The full weekday name (“Sunday”)



108
# File 'lib/fluid-time.rb', line 108

def A; cat @time.strftime('%A') end

#BObject Also known as: month

%B - The full month name (“January”)



118
# File 'lib/fluid-time.rb', line 118

def B; cat @time.strftime('%B') end

#bObject Also known as: mmm, smonth

%b - The abbreviated month name (“Jan”)



113
# File 'lib/fluid-time.rb', line 113

def b; cat @time.strftime('%b') end

#cObject Also known as: full

%c - The preferred local date and time representation



122
# File 'lib/fluid-time.rb', line 122

def c; cat @time.strftime('%c') end

#cat(str, sep = ' ') ⇒ Object Also known as: txt, _



87
88
89
90
91
# File 'lib/fluid-time.rb', line 87

def cat(str, sep = ' ')
  @last_was_space = ' ' == str.to_s
  @build += str.to_s + sep
  self
end

#colonObject Also known as: tn, sn



275
# File 'lib/fluid-time.rb', line 275

def colon;  xs.txt ':', '' end

#commaObject Also known as: tc, sc



274
# File 'lib/fluid-time.rb', line 274

def comma;  xs.txt ',', '' end

#dObject Also known as: dd, day, num

%d - Day of the month (01..31)



126
# File 'lib/fluid-time.rb', line 126

def d; cat @time.strftime('%d') end

#dashObject Also known as: td, sd



276
# File 'lib/fluid-time.rb', line 276

def dash;   xs.txt '-', '' end

#dateObject



311
312
313
# File 'lib/fluid-time.rb', line 311

def date
  self.mon.slash.day.slash.year
end

#dbObject

Presets



291
292
293
# File 'lib/fluid-time.rb', line 291

def db
  self.Y.m.d.H.I.S
end

#downObject Also known as: lower



244
245
246
# File 'lib/fluid-time.rb', line 244

def down
  set(@build.split.tap {|parts| parts << parts.pop.downcase}.join(' '))
end

#eObject Also known as: mday

%e - Day of the month (1..31)



132
# File 'lib/fluid-time.rb', line 132

def e; cat @time.strftime('%e') end

#HObject Also known as: h24, mhour, hour24

%H - Hour of the day, 24-hour clock (00..23)



136
# File 'lib/fluid-time.rb', line 136

def H; cat @time.strftime('%H') end

#IObject Also known as: h12, hour, hour12

%I - Hour of the day, 12-hour clock (01..12)



142
# File 'lib/fluid-time.rb', line 142

def I; cat @time.strftime('%I') end

#jObject Also known as: yday, day_of_year

%j - Day of the year (001..366)



152
# File 'lib/fluid-time.rb', line 152

def j; cat @time.strftime('%j') end

#lObject



148
# File 'lib/fluid-time.rb', line 148

def l; cat @time.strftime('%l') end

#lbraceObject Also known as: lb



279
# File 'lib/fluid-time.rb', line 279

def lbrace;  xs.txt '[', '' end

#lcurveObject Also known as: lc



282
# File 'lib/fluid-time.rb', line 282

def lcurve;  xs.txt '{', '' end

#lparenObject Also known as: lp



285
# File 'lib/fluid-time.rb', line 285

def lparen;  xs.txt '(', '' end

#MObject Also known as: min, minute

%M - Minute of the hour (00..59)



163
# File 'lib/fluid-time.rb', line 163

def M; cat @time.strftime('%M') end

#mObject Also known as: mm, mon, month_of_year

%m - Month of the year (01..12)



157
# File 'lib/fluid-time.rb', line 157

def m; cat @time.strftime('%m') end

#pObject Also known as: AM, PM

%p - Meridian indicator (“AM” or “PM”)



168
# File 'lib/fluid-time.rb', line 168

def p; cat @time.strftime('%p') end

#periodObject Also known as: tp, sp



273
# File 'lib/fluid-time.rb', line 273

def period; xs.txt '.', '' end

#pmObject Also known as: am

lowercase am/pm



172
# File 'lib/fluid-time.rb', line 172

def pm; self.p.down end

#rbraceObject Also known as: rb



280
# File 'lib/fluid-time.rb', line 280

def rbrace;  xs.txt ']', '' end

#rcurveObject Also known as: rc



283
# File 'lib/fluid-time.rb', line 283

def rcurve;  xs.txt '}', '' end

#rparenObject Also known as: rp



286
# File 'lib/fluid-time.rb', line 286

def rparen;  xs.txt ')', '' end

#SObject Also known as: sec, second

%S - Second of the minute (00..60)



179
# File 'lib/fluid-time.rb', line 179

def S; cat @time.strftime('%S') end

#set(str, sep = ' ') ⇒ Object



95
96
97
98
# File 'lib/fluid-time.rb', line 95

def set(str, sep = ' ')
  @build = str.to_s + sep
  self
end

#slashObject Also known as: tl, sl



277
# File 'lib/fluid-time.rb', line 277

def slash;  xs.txt '/', '' end

#spaceObject Also known as: ts, ss

Symbols (couldn’t decide between: t for text and s for symbol)



272
# File 'lib/fluid-time.rb', line 272

def space;     txt ' ',''  end

#stripObject Also known as: xs



263
264
265
266
# File 'lib/fluid-time.rb', line 263

def strip
  @build.strip! unless @last_was_space
  self
end

#strip_zeroObject Also known as: xz, x0, nozero



254
255
256
257
258
# 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

#thObject

Modifiers



224
225
226
227
228
# 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

#timeObject



307
308
309
# File 'lib/fluid-time.rb', line 307

def time
  self.hour.colon.minute.colon.second
end

#to_dateObject



79
80
81
# File 'lib/fluid-time.rb', line 79

def to_date
  Date.new(@time.year, @time.month, @time.day)
end

#to_sObject



69
70
71
72
73
# File 'lib/fluid-time.rb', line 69

def to_s
  hold = @build.strip
  @build = ""
  '' == hold ? @time.to_s : hold
end

#to_timeObject



75
76
77
# File 'lib/fluid-time.rb', line 75

def to_time
  @time
end

#UObject



184
# File 'lib/fluid-time.rb', line 184

def U; cat @time.strftime('%U') end

#upObject Also known as: upper



249
250
251
# File 'lib/fluid-time.rb', line 249

def up
  set(@build.split.tap {|parts| parts << parts.pop.upcase}.join(' '))
end

#usdateObject



295
296
297
# File 'lib/fluid-time.rb', line 295

def usdate
  self.mm.slash.d.slash.Y
end

#WObject



190
# File 'lib/fluid-time.rb', line 190

def W; cat @time.strftime('%W') end

#wObject

%w - Day of the week (Sunday is 0, 0..6)



197
# File 'lib/fluid-time.rb', line 197

def w; cat @time.strftime('%w') end

#xObject Also known as: autodate

%x - Preferred representation for the date alone, no time



200
# File 'lib/fluid-time.rb', line 200

def x; cat @time.strftime('%x') end

#XObject Also known as: autotime

%X - Preferred representation for the time alone, no date



204
# File 'lib/fluid-time.rb', line 204

def X; cat @time.strftime('%X') end

#xpmObject Also known as: xam

lowercase am/pm without space or leading zero on preceding number



175
# File 'lib/fluid-time.rb', line 175

def xpm; self.xz.xs.pm end

#YObject Also known as: year

%Y - Year with century



213
# File 'lib/fluid-time.rb', line 213

def Y; cat @time.strftime('%Y') end

#yObject Also known as: yy, sy

%y - Year without a century (00..99)



208
# File 'lib/fluid-time.rb', line 208

def y; cat @time.strftime('%y') end

#Y_m_dObject



303
304
305
# File 'lib/fluid-time.rb', line 303

def Y_m_d
  self.Y.dash.mm.dash.d
end

#YmdObject



299
300
301
# File 'lib/fluid-time.rb', line 299

def Ymd
  self.Y.xs.mm.xs.d
end

#yttObject Also known as: today, tomorrow, yesterday



230
231
232
233
234
235
236
237
238
239
# 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

#ZObject Also known as: zone, timezone

%Z - Time zone name



217
# File 'lib/fluid-time.rb', line 217

def Z; cat @time.strftime('%Z') end