zones 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/zones.rb +49 -43
  3. data/zones.gemspec +1 -1
  4. metadata +3 -4
  5. data/.ruby-version +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e885735f5b158bc128438382c99bab2319a1717e3bee2582240c241c6a97d5d
4
- data.tar.gz: 4ec9636d438698c2ee257904e01e8b6f2a48389fb1bfc0df06a935a031bbbe61
3
+ metadata.gz: 5f86231105060ba0d2a2e5de9b5d5e1a12697442eab2f53e25141ae9e364c595
4
+ data.tar.gz: 435fd13071ba665a0e3c8f4cca2162a804ca160ffee145ca179558a4e22f383d
5
5
  SHA512:
6
- metadata.gz: c7791eeed90e65b247d6a3a2a841e6f2774f5381b4d3ab3e7c6c010e11eb7c271eb2ddeaed587275e05bbbe8c42b94d65fe6b66d7d41ce9caf82aee801fe54fd
7
- data.tar.gz: 6935b6b8c8f7e9e92f7f2bdb6c30ab4f5ea65b2da7e07e7094b0b5a61aec9cb6d4510492f164015649f4fb2d2342fdc039593cd09ecec47d13b064457e622144
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)
@@ -64,7 +63,7 @@ class Time
64
63
  when %r!^((?:19|20)\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)?\.?(\d+)?([-+]\d\d:?\d\d)?!
65
64
  ymd = [$1.to_i, $2.to_i, $3.to_i]
66
65
  hms = [$4.to_i, $5.to_i, "#{$6}.#{$7}".to_f]
67
- off = $8.sub(/(\d)(\d\d)$/,"\1:\2") if $8 && !ignore_offset
66
+ off = $8.sub(/(\d)(\d\d)$/,'\1:\2') if $8 && !ignore_offset
68
67
  when %r!^
69
68
  (?:(0[1-9]|[12]\d|3[01]|[1-9][-/ ])[-/ ]? # $1: day
70
69
  ((?>[a-z]{3,9}))[-/ ]? # $2: month
@@ -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.0"
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zones
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-18 00:00:00.000000000 Z
11
+ date: 2023-06-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem makes it easy to work with time zones.
14
14
  email: steve.shreeve@gmail.com
@@ -16,7 +16,6 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
- - ".ruby-version"
20
19
  - Gemfile
21
20
  - LICENSE
22
21
  - README.md
@@ -41,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
40
  - !ruby/object:Gem::Version
42
41
  version: '0'
43
42
  requirements: []
44
- rubygems_version: 3.2.16
43
+ rubygems_version: 3.4.14
45
44
  signing_key:
46
45
  specification_version: 4
47
46
  summary: Easy time parsing and conversion between time zones
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.6.5