zones 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/zones.rb +48 -42
  3. data/zones.gemspec +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 849ad931c3600169ffafe613ee4dbdea31e43d6164a2fa70c7e389561be64e36
4
- data.tar.gz: 7721087a68b9d227da0187c0e95b3f2b12ff9f1a80aab7a4f91beeb67478681e
3
+ metadata.gz: 5f86231105060ba0d2a2e5de9b5d5e1a12697442eab2f53e25141ae9e364c595
4
+ data.tar.gz: 435fd13071ba665a0e3c8f4cca2162a804ca160ffee145ca179558a4e22f383d
5
5
  SHA512:
6
- metadata.gz: bc91b4e9b83b532a7f971ad3b17fa11b3c826918c1c535147e67816c5f15afd0e27af606e3131d4a6956d0ae4198262baebdd316a67c5be383c65811352214bc
7
- data.tar.gz: 82ae0d0bbf8820bccac25d2e55c6f38dde144ceb81f5d6b522237c03edda1416a00b1007bb1102450d6bae62cca8592f87f4b9b92cce2d87a1410ed9dde1fcf5
6
+ metadata.gz: 10f84f521cefa6e9ef0d3933338d8c6666dde86908fb18cf8d530ffe71535e95cb8c028a1537356edba17ce5d36a6fdf71a86dfc040208a178b0ea8282adfa48
7
+ data.tar.gz: 5929966234fda57b8b1fac23ace116f43369e3a68a994ad4413d56ed001c415efc22e850e27f2a2de9175bfb8bd6a363575d14d408a7618a69b246c18b6e3fc6
data/lib/zones.rb CHANGED
@@ -1,29 +1,28 @@
1
1
  require "tzinfo"
2
2
 
3
- # ==[ Dart uses the folllowing ]==
4
-
5
- # /*
6
- # * date ::= yeardate time_opt timezone_opt
7
- # * yeardate ::= year colon_opt month colon_opt day
8
- # * year ::= sign_opt digit{4,6}
9
- # * colon_opt :: <empty> | ':'
10
- # * sign ::= '+' | '-'
11
- # * sign_opt ::= <empty> | sign
12
- # * month ::= digit{2}
13
- # * day ::= digit{2}
14
- # * time_opt ::= <empty> | (' ' | 'T') hour minutes_opt
15
- # * minutes_opt ::= <empty> | colon_opt digit{2} seconds_opt
16
- # * seconds_opt ::= <empty> | colon_opt digit{2} millis_opt
17
- # * micros_opt ::= <empty> | ('.' | ',') digit+
18
- # * timezone_opt ::= <empty> | space_opt timezone
19
- # * space_opt :: ' ' | <empty>
20
- # * timezone ::= 'z' | 'Z' | sign digit{2} timezonemins_opt
21
- # * timezonemins_opt ::= <empty> | colon_opt digit{2}
22
- # */
23
- # static final RegExp _parseFormat =
24
- # RegExp(r'^([+-]?\d{4,6})-?(\d\d)-?(\d\d)' // Day part.
25
- # r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:[.,](\d+))?)?)?' // Time part.
26
- # r'( ?[zZ]| ?([-+])(\d\d)(?::?(\d\d))?)?)?$'); // Timezone part.
3
+ # ==[ Dart uses the folllowing ]==
4
+ #
5
+ # date ::= yeardate time_opt timezone_opt
6
+ # yeardate ::= year colon_opt month colon_opt day
7
+ # year ::= sign_opt digit{4,6}
8
+ # colon_opt :: <empty> | ':'
9
+ # sign ::= '+' | '-'
10
+ # sign_opt ::= <empty> | sign
11
+ # month ::= digit{2}
12
+ # day ::= digit{2}
13
+ # time_opt ::= <empty> | (' ' | 'T') hour minutes_opt
14
+ # minutes_opt ::= <empty> | colon_opt digit{2} seconds_opt
15
+ # seconds_opt ::= <empty> | colon_opt digit{2} millis_opt
16
+ # micros_opt ::= <empty> | ('.' | ',') digit+
17
+ # timezone_opt ::= <empty> | space_opt timezone
18
+ # space_opt :: ' ' | <empty>
19
+ # timezone ::= 'z' | 'Z' | sign digit{2} timezonemins_opt
20
+ # timezonemins_opt ::= <empty> | colon_opt digit{2}
21
+ #
22
+ # static final RegExp _parseFormat =
23
+ # RegExp(r'^([+-]?\d{4,6})-?(\d\d)-?(\d\d)' // Day part.
24
+ # r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:[.,](\d+))?)?)?' // Time part.
25
+ # r'( ?[zZ]| ?([-+])(\d\d)(?::?(\d\d))?)?)?$'); // Timezone part.
27
26
 
28
27
  class Date
29
28
  def self.parse_str(str)
@@ -107,15 +106,15 @@ class Time
107
106
  end
108
107
 
109
108
  # parse time and honor desired timezone
110
- def self.to_tz(str, to_tz=nil, ignore_offset=false)
109
+ def self.to_tz(str, zone=nil, ignore_offset=false)
111
110
  ymd, hms, off = parse_str(str, ignore_offset)
112
111
  out = Time.new(*ymd, *hms, off)
113
- if to_tz
112
+ if zone
114
113
  if off
115
- out = out.to_tz(to_tz)
114
+ out = out.to_tz(zone)
116
115
  else
117
116
  utc = out.utc
118
- off = TZInfo::Timezone.get(to_tz).utc_to_local(utc) - utc
117
+ off = TZInfo::Timezone.get(zone).utc_to_local(utc) - utc
119
118
  out = Time.new(*ymd, *hms, off)
120
119
  end
121
120
  else
@@ -124,25 +123,32 @@ class Time
124
123
  end
125
124
 
126
125
  # ignore supplied timezone, use local
127
- def self.to_tz!(str, to_tz=nil)
128
- to_tz(str, to_tz, true)
126
+ def self.to_tz!(str, zone=nil)
127
+ to_tz(str, zone, true)
129
128
  end
130
129
 
131
- # transform time to new timezone
132
- def to_tz(to_tz)
133
- utc = utc? ? self : getutc
134
- raw = TZInfo::Timezone.get(to_tz).utc_to_local(utc)
135
- all = raw.to_a[1,5].reverse.push(strftime("%S.%6N").to_f) # retain fractional seconds
136
- out = Time.new(*all, raw - utc)
130
+ # NOTE: We can clean this up too...
131
+ # this 1-liner works, but returns a TZInfo::TimeWithOffset???
132
+ # def in_tz(zone); TZInfo::Timezone.get(zone).to_local(self); end
133
+
134
+ # same time moment, different time zone offset
135
+ def in_tz(zone)
136
+ cfg = TZInfo::Timezone.get(zone)
137
+ use = cfg.to_local(self)
138
+ ary = use.to_a[1,5].reverse.push(strftime("%S.%6N").to_f)
139
+ Time.new(*ary, cfg)
137
140
  end
138
141
 
139
- # preserve time but change offset
140
- def to_tz!(to_tz)
141
- all = to_a[1,5].reverse.push(strftime("%S.%6N").to_f) # retain fractional seconds
142
- raw = Time.utc(*all)
143
- utc = TZInfo::Timezone.get(to_tz).local_to_utc(raw)
144
- out = Time.new(*all, raw - utc)
142
+ # same time values, different time zone offset
143
+ def as_tz(zone)
144
+ cfg = TZInfo::Timezone.get(zone)
145
+ use = self
146
+ ary = use.to_a[1,5].reverse.push(strftime("%S.%6N").to_f)
147
+ Time.new(*ary, cfg)
145
148
  end
149
+
150
+ alias_method :to_tz , :in_tz
151
+ alias_method :to_tz!, :as_tz
146
152
  end
147
153
 
148
154
  class String
data/zones.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "zones"
5
- s.version = "0.6.1"
5
+ s.version = "0.6.2"
6
6
  s.author = "Steve Shreeve"
7
7
  s.email = "steve.shreeve@gmail.com"
8
8
  s.summary = "Easy time parsing and conversion between time zones"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zones
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve