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: #weekday, #tday)
%A - The full weekday name (“Sunday”).
-
#a ⇒ Object
(also: #ddd, #wday)
Formats.
-
#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, #hour24)
%H - Hour of the day, 24-hour clock (00..23).
-
#I ⇒ Object
(also: #hour, #h12, #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: #mm, #mon, #month_of_year)
%m - Month of the year (01..12).
-
#M ⇒ Object
(also: #min, #minute)
%M - Minute of the hour (00..59).
-
#p ⇒ Object
(also: #om, #am, #pm)
%p - Meridian indicator (“AM” or “PM”).
- #period ⇒ Object (also: #tp, #sp)
- #rbrace ⇒ Object (also: #rb)
- #rcurve ⇒ Object (also: #rc)
- #rparen ⇒ Object (also: #rp)
-
#S ⇒ Object
(also: #sec, #second)
%S - Second of the minute (00..60).
- #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)
-
#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: #autotime)
%X - Preferred representation for the time alone, no date.
-
#x ⇒ Object
(also: #autodate)
%x - Preferred representation for the date alone, no time.
-
#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
-
#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 |
# 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 |
#A ⇒ Object 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 |
#a ⇒ Object Also known as: ddd, wday
Formats
92 |
# File 'lib/fluid-time.rb', line 92 def a; cat @time.strftime('%a') end |
#B ⇒ Object 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 |
#b ⇒ Object 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 |
#c ⇒ Object 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 |
#colon ⇒ Object Also known as: tn, sn
259 |
# File 'lib/fluid-time.rb', line 259 def colon; xs.txt ':', '' end |
#comma ⇒ Object Also known as: tc, sc
258 |
# File 'lib/fluid-time.rb', line 258 def comma; xs.txt ',', '' end |
#d ⇒ Object 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 |
#dash ⇒ Object Also known as: td, sd
260 |
# File 'lib/fluid-time.rb', line 260 def dash; xs.txt '-', '' end |
#date ⇒ Object
295 296 297 |
# File 'lib/fluid-time.rb', line 295 def date self.mon.sl.day.sl.year end |
#db ⇒ Object
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 |
#down ⇒ Object 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 |
#e ⇒ Object 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 |
#H ⇒ Object 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 |
#I ⇒ Object 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 |
#j ⇒ Object 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 |
#l ⇒ Object
137 |
# File 'lib/fluid-time.rb', line 137 def l; cat @time.strftime('%l') end |
#lbrace ⇒ Object Also known as: lb
263 |
# File 'lib/fluid-time.rb', line 263 def lbrace; xs.txt '[', '' end |
#lcurve ⇒ Object Also known as: lc
266 |
# File 'lib/fluid-time.rb', line 266 def lcurve; xs.txt '{', '' end |
#lparen ⇒ Object Also known as: lp
269 |
# File 'lib/fluid-time.rb', line 269 def lparen; xs.txt '(', '' end |
#m ⇒ Object 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 |
#M ⇒ Object 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 |
#p ⇒ Object 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 |
#period ⇒ Object Also known as: tp, sp
257 |
# File 'lib/fluid-time.rb', line 257 def period; xs.txt '.', '' end |
#rbrace ⇒ Object Also known as: rb
264 |
# File 'lib/fluid-time.rb', line 264 def rbrace; xs.txt ']', '' end |
#rcurve ⇒ Object Also known as: rc
267 |
# File 'lib/fluid-time.rb', line 267 def rcurve; xs.txt '}', '' end |
#rparen ⇒ Object Also known as: rp
270 |
# File 'lib/fluid-time.rb', line 270 def rparen; xs.txt ')', '' end |
#S ⇒ Object 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 |
#slash ⇒ Object Also known as: tl, sl
261 |
# File 'lib/fluid-time.rb', line 261 def slash; xs.txt '/', '' end |
#space ⇒ Object 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 |
#strip ⇒ Object 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_zero ⇒ Object 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 |
#th ⇒ Object
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 |
#time ⇒ Object
291 292 293 |
# File 'lib/fluid-time.rb', line 291 def time self.hour.tn.minute.tn.second end |
#to_date ⇒ Object
73 74 75 |
# File 'lib/fluid-time.rb', line 73 def to_date Date.new(@time.year, @time.month, @time.day) end |
#to_s ⇒ Object
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_time ⇒ Object
69 70 71 |
# File 'lib/fluid-time.rb', line 69 def to_time @time end |
#U ⇒ Object
168 |
# File 'lib/fluid-time.rb', line 168 def U; cat @time.strftime('%U') end |
#up ⇒ Object 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 |
#usdate ⇒ Object
279 280 281 |
# File 'lib/fluid-time.rb', line 279 def usdate self.mm.tl.d.tl.Y end |
#W ⇒ Object
174 |
# File 'lib/fluid-time.rb', line 174 def W; cat @time.strftime('%W') end |
#w ⇒ Object
%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 |
#X ⇒ Object 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 |
#x ⇒ Object 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 |
#Y ⇒ Object Also known as: year
%Y - Year with century
197 |
# File 'lib/fluid-time.rb', line 197 def Y; cat @time.strftime('%Y') end |
#y ⇒ Object 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_d ⇒ Object
287 288 289 |
# File 'lib/fluid-time.rb', line 287 def Y_m_d self.Y.td.mm.td.d end |
#Ymd ⇒ Object
283 284 285 |
# File 'lib/fluid-time.rb', line 283 def Ymd self.Y.xs.mm.xs.d end |
#ytt ⇒ Object
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 |
#Z ⇒ Object Also known as: zone, timezone
%Z - Time zone name
201 |
# File 'lib/fluid-time.rb', line 201 def Z; cat @time.strftime('%Z') end |