Class: Time
Class Method Summary collapse
- .parse_str(str) ⇒ Object
- .tz(str, toz = nil, asz = nil) ⇒ Object
- .tz!(str, asz = "UTC", toz = nil) ⇒ Object
Instance Method Summary collapse
-
#as(zone) ⇒ Object
same time values, different time zone (ie - change time zone only).
-
#to(zone) ⇒ Object
same time moment, different time zone (ie - change time and zone).
Methods included from MonthValue
Class Method Details
.parse_str(str) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/zones.rb', line 56 def self.parse_str(str) case str when %r!^ ((?:19|20)\d\d)[-/\s]?(\d\d)[-/\s]?(\d\d)\s? # $1-3: year, month, day (\d\d):?(\d\d):?(\d\d)?\.?(\d+)? # $4-7: hour, min, sec, decimal ([-+]\d\d:?\d\d)? # $8 : offset !x ymd = [$1.to_i, $2.to_i, $3.to_i] hms = [$4.to_i, $5.to_i, "#{$6}.#{$7}".to_f] off = $8.sub(/(\d)(\d\d)$/,'\1:\2') if $8 when %r!^ (?:(0[1-9]|[12]\d|3[01]|[1-9](?=\D))[-/\s]? # $1: day ( (?>[a-z]{3,9}))[-/\s]? # $2: month (no digits allowed here) ((?>19|20)\d\d) # $3: year | # or... ((?>19|20)\d\d)[-/\s]? # $4: year (0[1-9]|1[012]|[1-9](?=\D)|(?>[a-z]{3,9}))[-/\s]? # $5: month (0[1-9]|[12]\d|3[01]|[1-9](?=\D)) # $6: day | # or... (0[1-9]|1[012]|[1-9](?=\D)|(?>[a-z]{3,9}))[-/\s]? # $7: month (0[1-9]|[12]\d|3[01]|[1-9](?=\D)),?[-/\s]? # $8: day ((?>19|20)\d\d) # $9: year )\s?T?\s? (\d\d?)? # $10: hour :?(\d\d)? # $11: min :?(\d\d)? # $12: sec \.?(\d+)? # $13: dec \s?(?:(a|p)?m)? # $14: am/pm \s?(([-+])?(\d\d):?(\d\d)|UTC|GMT|Z)? # $15: offset ($16=sign, $17=hours, $18=mins) !iox ymd = $1 ? [ $3.to_i, month_value($2), $1.to_i] : \ $4 ? [ $4.to_i, month_value($5), $6.to_i] : \ [ $9.to_i, month_value($7), $8.to_i] hms = [$14 ? ($10.to_i % 12) + (($14=="P" || $14=="p") ? 12 : 0) : $10.to_i, $11.to_i, "#{$12}.#{$13}".to_f] off = ($17 ? "#{$16||'+'}#{$17}:#{$18}" : "+00:00") if $15 else raise "can't parse: #{str}" end [ymd, hms, off] end |
.tz(str, toz = nil, asz = nil) ⇒ Object
97 98 99 100 101 |
# File 'lib/zones.rb', line 97 def self.tz(str, toz=nil, asz=nil) ymd, hms, off = parse_str(str) out = Time.new(*ymd, *hms, asz ? TZInfo::Timezone.get(asz) : off) toz ? out.to(toz) : out end |
.tz!(str, asz = "UTC", toz = nil) ⇒ Object
103 104 105 |
# File 'lib/zones.rb', line 103 def self.tz!(str, asz="UTC", toz=nil) tz(str, toz, asz) end |
Instance Method Details
#as(zone) ⇒ Object
same time values, different time zone (ie - change time zone only)
116 117 118 119 120 121 |
# File 'lib/zones.rb', line 116 def as(zone) cfg = TZInfo::Timezone.get(zone) use = self ary = use.to_a[1,5].reverse.push(strftime("%S.%6N").to_f) Time.new(*ary, cfg) end |
#to(zone) ⇒ Object
same time moment, different time zone (ie - change time and zone)
108 109 110 111 112 113 |
# File 'lib/zones.rb', line 108 def to(zone) cfg = TZInfo::Timezone.get(zone) use = cfg.to_local(self) ary = use.to_a[1,5].reverse.push(strftime("%S.%6N").to_f) Time.new(*ary, cfg) end |