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
# File 'lib/fluid-time.rb', line 16

def self.demo
  %w{
    to_time
    to_date
    A.tday.weekday
    weekday.up
    b.mmm.smonth
    B.month
    c
    full
    d.day.num.dd
    e.mday
    mmm.d.th.year
    year.mmm.d.th
    H.h24.hour24
    I.h12.hour.hour12
    l
    j.yday.day_of_year
    m.mm.mon.month_of_year
    M.min.minute
    p.am.pm
    time.xs.p.down
    S.sec.second
    U.th.txt('week')
    W.th.tc.ts._('min')
    w
    x.autodate
    X.autotime
    date
    time
    y.yy.sy
    Y.year
    Z.zone.timezone
    txt('01').xz
    txt('1:00').xz.xs.pm.lower.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
    db
  }.each {|ex| "FluidTime.new.   #{ex}.to_s".tap {|str| p str + (" %#{80 - str.length}s" % eval(str))} }
  nil
end

Instance Method Details

#<=>(o) ⇒ Object



77
78
79
# File 'lib/fluid-time.rb', line 77

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

#AObject Also known as: weekday, tday

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



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

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

#aObject Also known as: ddd, wday

Formats



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

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

#BObject Also known as: month

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



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

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

#bObject Also known as: mmm, smonth

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



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

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

#cObject Also known as: full

%c - The preferred local date and time representation



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

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

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



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

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

#colonObject Also known as: tn, sn



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

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

#commaObject Also known as: tc, sc



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

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

#dObject Also known as: dd, day, num

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



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

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

#dashObject Also known as: td, sd



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

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

#dateObject



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

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

#dbObject

Presets



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

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

#downObject Also known as: lower



227
228
229
230
# File 'lib/fluid-time.rb', line 227

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

#eObject Also known as: mday

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



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

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

#HObject Also known as: h24, hour24

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



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

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

#IObject Also known as: hour, h12, hour12

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



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

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

#jObject Also known as: yday, day_of_year

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



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

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

#lObject



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

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

#lbraceObject Also known as: lb



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

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

#lcurveObject Also known as: lc



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

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

#lparenObject Also known as: lp



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

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

#mObject Also known as: mm, mon, month_of_year

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



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

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

#MObject Also known as: min, minute

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



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

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

#pObject Also known as: om, am, pm

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



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

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

#periodObject Also known as: tp, sp



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

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

#rbraceObject Also known as: rb



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

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

#rcurveObject Also known as: rc



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

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

#rparenObject Also known as: rp



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

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

#SObject Also known as: sec, second

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



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

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

#slashObject Also known as: tl, sl



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

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

#spaceObject Also known as: ts, ss

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



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

def space;     txt ' ',''  end

#stripObject Also known as: xs



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

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

#strip_zeroObject Also known as: xz



239
240
241
242
243
244
# File 'lib/fluid-time.rb', line 239

def strip_zero
  @build = @build.strip.split.tap do |parts|
    parts << parts.pop.tap { |last| last.replace(last.gsub(/^0/,'').gsub(':00','')) }
  end.join(' ') + ' '
  self
end

#thObject

Modifiers



208
209
210
211
212
213
# File 'lib/fluid-time.rb', line 208

def th
  @build = @build.strip.split.tap do |parts|
    parts << parts.pop.tap { |last| last.replace(ordinalize(last)) }
  end.join(' ') + ' '
  self
end

#timeObject



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

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

#to_dateObject



73
74
75
# File 'lib/fluid-time.rb', line 73

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

#to_sObject



63
64
65
66
67
# File 'lib/fluid-time.rb', line 63

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

#to_timeObject



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

def to_time
  @time
end

#UObject



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

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

#upObject Also known as: upper



233
234
235
236
# File 'lib/fluid-time.rb', line 233

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

#usdateObject



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

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

#WObject



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

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

#wObject

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



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

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

#XObject Also known as: autotime

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



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

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

#xObject Also known as: autodate

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



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

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

#YObject Also known as: year

%Y - Year with century



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

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

#yObject Also known as: yy, sy

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



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

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

#Y_m_dObject



287
288
289
# File 'lib/fluid-time.rb', line 287

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

#YmdObject



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

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

#yttObject



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/fluid-time.rb', line 215

def ytt
  @build = @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.tomorrow.to_s
      parts[index] = 'Yesterday' if date.is_a?(Date) && date.to_s == Date.yesterday.to_s
    end
  end.join(' ') + ' '
  self
end

#ZObject Also known as: zone, timezone

%Z - Time zone name



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

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