Class: Time
Class Method Summary collapse
-
.as_tz(str, asz = "UTC", toz = nil) ⇒ Object
swap the order of to_tz and default asz to UTC.
- .parse_str(str) ⇒ Object
-
.to_tz(str, toz = nil, asz = nil) ⇒ Object
read values of time in asz, stated, or local timezone, optionally convert to toz timezone.
Instance Method Summary collapse
-
#as(zone) ⇒ Object
same values of time, different time zone (ie - change time zone only).
-
#to(zone) ⇒ Object
same moment in time, different time zone (ie - change time and zone).
Methods included from MonthValue
Class Method Details
.as_tz(str, asz = "UTC", toz = nil) ⇒ Object
swap the order of to_tz and default asz to UTC
105 106 107 |
# File 'lib/zones.rb', line 105 def self.as_tz(str, asz="UTC", toz=nil) to_tz(str, toz, asz) end |
.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 |
.to_tz(str, toz = nil, asz = nil) ⇒ Object
read values of time in asz, stated, or local timezone, optionally convert to toz timezone
98 99 100 101 102 |
# File 'lib/zones.rb', line 98 def self.to_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 |
Instance Method Details
#as(zone) ⇒ Object
same values of time, different time zone (ie - change time zone only)
118 119 120 121 122 123 |
# File 'lib/zones.rb', line 118 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 moment in time, different time zone (ie - change time and zone)
110 111 112 113 114 115 |
# File 'lib/zones.rb', line 110 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 |