Class: FluidTime
- Inherits:
-
Object
- Object
- FluidTime
- Defined in:
- lib/fluid-time.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(o) ⇒ Object
-
#a ⇒ Object
(also: #ddd, #wday)
Formats.
-
#A ⇒ Object
(also: #tday, #weekday)
%A - The full weekday name (“Sunday”).
-
#B ⇒ Object
(also: #month)
%B - The full month name (“January”).
-
#b ⇒ Object
(also: #mmm, #smonth)
%b - The abbreviated month name (“Jan”).
-
#c ⇒ Object
(also: #full)
%c - The preferred local date and time representation.
- #cat(str, sep = ' ') ⇒ Object (also: #txt, #_)
- #colon ⇒ Object (also: #tn, #sn)
- #comma ⇒ Object (also: #tc, #sc)
-
#d ⇒ Object
(also: #dd, #day, #num)
%d - Day of the month (01..31).
- #dash ⇒ Object (also: #td, #sd)
- #date ⇒ Object
-
#db ⇒ Object
Presets.
- #down ⇒ Object (also: #lower)
-
#e ⇒ Object
(also: #mday)
%e - Day of the month (1..31).
-
#H ⇒ Object
(also: #h24, #mhour, #hour24)
%H - Hour of the day, 24-hour clock (00..23).
-
#I ⇒ Object
(also: #h12, #hour, #hour12)
%I - Hour of the day, 12-hour clock (01..12).
-
#initialize(input = Time.now) ⇒ FluidTime
constructor
A new instance of FluidTime.
-
#j ⇒ Object
(also: #yday, #day_of_year)
%j - Day of the year (001..366).
- #l ⇒ Object
- #lbrace ⇒ Object (also: #lb)
- #lcurve ⇒ Object (also: #lc)
- #lparen ⇒ Object (also: #lp)
-
#M ⇒ Object
(also: #min, #minute)
%M - Minute of the hour (00..59).
-
#m ⇒ Object
(also: #mm, #mon, #month_of_year)
%m - Month of the year (01..12).
-
#p ⇒ Object
(also: #AM, #PM)
%p - Meridian indicator (“AM” or “PM”).
- #period ⇒ Object (also: #tp, #sp)
-
#pm ⇒ Object
(also: #am)
lowercase am/pm.
- #rbrace ⇒ Object (also: #rb)
- #rcurve ⇒ Object (also: #rc)
- #rparen ⇒ Object (also: #rp)
-
#S ⇒ Object
(also: #sec, #second)
%S - Second of the minute (00..60).
- #set(str, sep = ' ') ⇒ Object
- #slash ⇒ Object (also: #tl, #sl)
-
#space ⇒ Object
(also: #ts, #ss)
Symbols (couldn’t decide between: t for text and s for symbol).
- #strip ⇒ Object (also: #xs)
- #strip_zero ⇒ Object (also: #xz, #x0, #nozero)
-
#th ⇒ Object
Modifiers.
- #time ⇒ Object
- #to_date ⇒ Object
- #to_s ⇒ Object
- #to_time ⇒ Object
- #U ⇒ Object
- #up ⇒ Object (also: #upper)
- #usdate ⇒ Object
- #W ⇒ Object
-
#w ⇒ Object
%w - Day of the week (Sunday is 0, 0..6).
-
#x ⇒ Object
(also: #autodate)
%x - Preferred representation for the date alone, no time.
-
#X ⇒ Object
(also: #autotime)
%X - Preferred representation for the time alone, no date.
-
#xpm ⇒ Object
(also: #xam)
lowercase am/pm without space or leading zero on preceding number.
-
#Y ⇒ Object
(also: #year)
%Y - Year with century.
-
#y ⇒ Object
(also: #yy, #sy)
%y - Year without a century (00..99).
- #Y_m_d ⇒ Object
- #Ymd ⇒ Object
- #ytt ⇒ Object (also: #today, #tomorrow, #yesterday)
-
#Z ⇒ Object
(also: #zone, #timezone)
%Z - Time zone name.
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
.demo ⇒ Object
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 |
#a ⇒ Object Also known as: ddd, wday
Formats
103 |
# File 'lib/fluid-time.rb', line 103 def a; cat @time.strftime('%a') end |
#A ⇒ Object 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 |
#B ⇒ Object 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 |
#b ⇒ Object 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 |
#c ⇒ Object 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 |
#colon ⇒ Object Also known as: tn, sn
275 |
# File 'lib/fluid-time.rb', line 275 def colon; xs.txt ':', '' end |
#comma ⇒ Object Also known as: tc, sc
274 |
# File 'lib/fluid-time.rb', line 274 def comma; xs.txt ',', '' end |
#d ⇒ Object 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 |
#dash ⇒ Object Also known as: td, sd
276 |
# File 'lib/fluid-time.rb', line 276 def dash; xs.txt '-', '' end |
#date ⇒ Object
311 312 313 |
# File 'lib/fluid-time.rb', line 311 def date self.mon.slash.day.slash.year end |
#db ⇒ Object
Presets
291 292 293 |
# File 'lib/fluid-time.rb', line 291 def db self.Y.m.d.H.I.S end |
#down ⇒ Object 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 |
#e ⇒ Object 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 |
#H ⇒ Object 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 |
#I ⇒ Object 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 |
#j ⇒ Object 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 |
#l ⇒ Object
148 |
# File 'lib/fluid-time.rb', line 148 def l; cat @time.strftime('%l') end |
#lbrace ⇒ Object Also known as: lb
279 |
# File 'lib/fluid-time.rb', line 279 def lbrace; xs.txt '[', '' end |
#lcurve ⇒ Object Also known as: lc
282 |
# File 'lib/fluid-time.rb', line 282 def lcurve; xs.txt '{', '' end |
#lparen ⇒ Object Also known as: lp
285 |
# File 'lib/fluid-time.rb', line 285 def lparen; xs.txt '(', '' end |
#M ⇒ Object 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 |
#m ⇒ Object 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 |
#p ⇒ Object 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 |
#period ⇒ Object Also known as: tp, sp
273 |
# File 'lib/fluid-time.rb', line 273 def period; xs.txt '.', '' end |
#pm ⇒ Object Also known as: am
lowercase am/pm
172 |
# File 'lib/fluid-time.rb', line 172 def pm; self.p.down end |
#rbrace ⇒ Object Also known as: rb
280 |
# File 'lib/fluid-time.rb', line 280 def rbrace; xs.txt ']', '' end |
#rcurve ⇒ Object Also known as: rc
283 |
# File 'lib/fluid-time.rb', line 283 def rcurve; xs.txt '}', '' end |
#rparen ⇒ Object Also known as: rp
286 |
# File 'lib/fluid-time.rb', line 286 def rparen; xs.txt ')', '' end |
#S ⇒ Object 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 |
#slash ⇒ Object Also known as: tl, sl
277 |
# File 'lib/fluid-time.rb', line 277 def slash; xs.txt '/', '' end |
#space ⇒ Object 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 |
#strip ⇒ Object 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_zero ⇒ Object 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 |
#th ⇒ Object
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 |
#time ⇒ Object
307 308 309 |
# File 'lib/fluid-time.rb', line 307 def time self.hour.colon.minute.colon.second end |
#to_date ⇒ Object
79 80 81 |
# File 'lib/fluid-time.rb', line 79 def to_date Date.new(@time.year, @time.month, @time.day) end |
#to_s ⇒ Object
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_time ⇒ Object
75 76 77 |
# File 'lib/fluid-time.rb', line 75 def to_time @time end |
#U ⇒ Object
184 |
# File 'lib/fluid-time.rb', line 184 def U; cat @time.strftime('%U') end |
#up ⇒ Object 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 |
#usdate ⇒ Object
295 296 297 |
# File 'lib/fluid-time.rb', line 295 def usdate self.mm.slash.d.slash.Y end |
#W ⇒ Object
190 |
# File 'lib/fluid-time.rb', line 190 def W; cat @time.strftime('%W') end |
#w ⇒ Object
%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 |
#x ⇒ Object 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 |
#X ⇒ Object 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 |
#xpm ⇒ Object 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 |
#Y ⇒ Object Also known as: year
%Y - Year with century
213 |
# File 'lib/fluid-time.rb', line 213 def Y; cat @time.strftime('%Y') end |
#y ⇒ Object 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_d ⇒ Object
303 304 305 |
# File 'lib/fluid-time.rb', line 303 def Y_m_d self.Y.dash.mm.dash.d end |
#Ymd ⇒ Object
299 300 301 |
# File 'lib/fluid-time.rb', line 299 def Ymd self.Y.xs.mm.xs.d end |
#ytt ⇒ Object 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 |
#Z ⇒ Object Also known as: zone, timezone
%Z - Time zone name
217 |
# File 'lib/fluid-time.rb', line 217 def Z; cat @time.strftime('%Z') end |