zones 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +51 -13
  3. data/lib/zones.rb +10 -8
  4. data/zones.gemspec +1 -1
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fcc3e314485f8294d9dfbfa29afc78ccd6cf9619cd67095bc5fe6b1f84616277
4
- data.tar.gz: 36bea6abe134f28921a23d3a1bb9bb6e1cea7b1c9ffb6fbfc80362156e3f6895
3
+ metadata.gz: 68effb6f43a7c0bc15f336624569d9f39c549cacd0b4afb319f4f40af29bd35b
4
+ data.tar.gz: 5eaa777bd12704f63a3d76b09dd6957f6e7896213c710377c8acdf7e7499b7bb
5
5
  SHA512:
6
- metadata.gz: 1c9742d8ce70f27717f435d2174c8c019529d168cf84162d0f89769f3d394eac3ddd5f150e171edd5de0d1eb2b847a997909031368f9f3038770f1ba479c1447
7
- data.tar.gz: 48e62375f30a6bb5a45ef207a419f13731b06cc11c8e2b9df6a7a6c4df74235c1451956e3d69b5beb5c798d0ba64c98aa10b38bdb29c244d3bf1e3c58d0445f3
6
+ metadata.gz: ef064dda2c373fd9faf62866e381432c76d72ed6735b0c1f1e544f21a8a0c4f7c909c8aabfc2756294e54862408012e63025b24da4d9693344ede3ba8ead648c
7
+ data.tar.gz: e7871c5dac1315da804bccf33c8a42f78196d1a13ff5dfbbca537a7d1cdfc8728f9a4f0126eb07c3a912389ce9bd403b4da5ff1f9c6bda360b04815806110580
data/README.md CHANGED
@@ -5,13 +5,13 @@ A friendly Ruby gem for time parsing and time zone conversion.
5
5
  ## API
6
6
 
7
7
  ```ruby
8
- Time.tz(str, toz=nil, asz=nil) # create a new Time object with a time zone
9
- Time.tz!(str, asz="UTC", toz=nil) # overrides original time zone and swaps params
8
+ Time.to_tz(str, toz=nil, asz=nil) # create a new Time object with a time zone
9
+ Time.as_tz(str, asz="UTC", toz=nil) # overrides original time zone and swaps params
10
10
  Time#to(zone) # converts a time to a new time zone
11
11
  Time#as(zone) # keeps the time, but changes the time zone
12
12
 
13
- String#to_tz # calls Time.tz
14
- String#to_tz! # calls Time.tz!
13
+ String#to_tz # calls Time.to_tz
14
+ String#as_tz # calls Time.as_tz
15
15
  String#to_day # calls Date.to_day
16
16
  String#iso_date # parses and shows ISO date (YYYY-MM-DD)
17
17
  ```
@@ -21,17 +21,55 @@ String#iso_date # parses and shows ISO date (YYYY-MM-DD)
21
21
  Parsing strings:
22
22
 
23
23
  ```ruby
24
- # no argument means to parse the value as is; "!" ignores the time zone and uses UTC
25
- x = "3 August 2013 11:43 +0415".to_tz # 2013-08-03 11:43:00 +0415
26
- y = "3 August 2013 11:43 +0415".to_tz! # 2013-08-03 11:43:00 +0000
24
+ # no argument means to parse the value as is; 'as' ignores the time zone and defaults to UTC
25
+ "3 August 2013 11:43 +0415".to_tz # 2013-08-03 11:43:00 +0415
26
+ "3 August 2013 11:43 +0415".as_tz # 2013-08-03 11:43:00 +0000
27
27
 
28
- # one argument means to convert to that time zone, use "!" to ignore the original offset
29
- x = "3 August 2013 11:43 +0415".to_tz("US/Pacific") # 2013-08-03 00:28:00 -0700
30
- y = "3 August 2013 11:43 +0415".to_tz!("US/Pacific") # 2013-08-03 11:43:00 -0700
28
+ # one argument for `to_tz` means to parse naturally and then convert to that time zone
29
+ "3 August 2013 11:43 +0415".to_tz("US/Pacific") # 2013-08-03 00:28:00 -0700
30
+ "3 August 2013 11:43 +0415".to_tz("America/Caracas") # 2013-08-03 02:58:00 -0430
31
31
 
32
- # use two arguments to indicate source and destination time zones, "!" swaps the order
33
- x = "3 August 2013 11:43 +0415".to_tz("US/Pacific", "America/Caracas") # 2013-08-03 09:13:00 -0700
34
- y = "3 August 2013 11:43 +0415".to_tz!("US/Pacific", "America/Caracas") # 2013-08-03 14:13:00 -0430
32
+ # one argument for `as_tz` means to ignore offset and use the parameter passed in
33
+ "3 August 2013 11:43 +0415".as_tz("US/Pacific") # 2013-08-03 11:43:00 -0700
34
+ "3 August 2013 11:43 +0415".as_tz("America/Caracas") # 2013-08-03 11:43:00 -0430
35
+
36
+ # use two arguments to indicate destination and source time zones, 'as' swaps the order
37
+ "3 August 2013 11:43 +0415".to_tz("US/Pacific", "America/Caracas") # 2013-08-03 09:13:00 -0700
38
+ "3 August 2013 11:43 +0415".as_tz("US/Pacific", "America/Caracas") # 2013-08-03 14:13:00 -0430
39
+
40
+ # the same as above, swapping functions and parameter order gives same results
41
+ "3 August 2013 11:43 +0415".to_tz("America/Caracas", "US/Pacific") # 2013-08-03 14:13:00 -0430
42
+ "3 August 2013 11:43 +0415".as_tz("America/Caracas", "US/Pacific") # 2013-08-03 09:13:00 -0700
43
+ ```
44
+
45
+ Parsing strings using `to_tz`:
46
+
47
+ ```ruby
48
+ # no args means to parse the value as is
49
+ "3 August 2013 11:43 +0415".to_tz # 2013-08-03 11:43:00 +0415
50
+
51
+ # one arg will then convert to that time zone
52
+ "3 August 2013 11:43 +0415".to_tz("US/Pacific") # 2013-08-03 00:28:00 -0700
53
+ "3 August 2013 11:43 +0415".to_tz("America/Caracas") # 2013-08-03 02:58:00 -0430
54
+
55
+ # two args forces the second arg as the initial time zone
56
+ "3 August 2013 11:43 +0415".to_tz("US/Pacific", "America/Caracas") # 2013-08-03 09:13:00 -0700
57
+ "3 August 2013 11:43 +0415".to_tz("America/Caracas", "US/Pacific") # 2013-08-03 14:13:00 -0430
58
+ ```
59
+
60
+ Parsing strings using `as_tz`:
61
+
62
+ ```ruby
63
+ # no args forces the timezone to be UTC
64
+ "3 August 2013 11:43 +0415".as_tz # 2013-08-03 11:43:00 +0000
65
+
66
+ # one arg ignore offset and forces the time zone as the arg
67
+ "3 August 2013 11:43 +0415".as_tz("US/Pacific") # 2013-08-03 11:43:00 -0700
68
+ "3 August 2013 11:43 +0415".as_tz("America/Caracas") # 2013-08-03 11:43:00 -0430
69
+
70
+ # two args forces the second arg as the initial time zone
71
+ "3 August 2013 11:43 +0415".as_tz("US/Pacific", "America/Caracas") # 2013-08-03 14:13:00 -0430
72
+ "3 August 2013 11:43 +0415".as_tz("America/Caracas", "US/Pacific") # 2013-08-03 09:13:00 -0700
35
73
  ```
36
74
 
37
75
  Converting values:
data/lib/zones.rb CHANGED
@@ -94,17 +94,19 @@ class Time
94
94
  [ymd, hms, off]
95
95
  end
96
96
 
97
- def self.tz(str, toz=nil, asz=nil)
97
+ # read values of time in asz, stated, or local timezone, optionally convert to toz timezone
98
+ def self.to_tz(str, toz=nil, asz=nil)
98
99
  ymd, hms, off = parse_str(str)
99
100
  out = Time.new(*ymd, *hms, asz ? TZInfo::Timezone.get(asz) : off)
100
101
  toz ? out.to(toz) : out
101
102
  end
102
103
 
103
- def self.tz!(str, asz="UTC", toz=nil)
104
- tz(str, toz, asz)
104
+ # swap the order of to_tz and default asz to UTC
105
+ def self.as_tz(str, asz="UTC", toz=nil)
106
+ to_tz(str, toz, asz)
105
107
  end
106
108
 
107
- # same time moment, different time zone (ie - change time and zone)
109
+ # same moment in time, different time zone (ie - change time and zone)
108
110
  def to(zone)
109
111
  cfg = TZInfo::Timezone.get(zone)
110
112
  use = cfg.to_local(self)
@@ -112,7 +114,7 @@ class Time
112
114
  Time.new(*ary, cfg)
113
115
  end
114
116
 
115
- # same time values, different time zone (ie - change time zone only)
117
+ # same values of time, different time zone (ie - change time zone only)
116
118
  def as(zone)
117
119
  cfg = TZInfo::Timezone.get(zone)
118
120
  use = self
@@ -131,10 +133,10 @@ class String
131
133
  end
132
134
 
133
135
  def to_tz(*args)
134
- Time.tz(self, *args)
136
+ Time.to_tz(self, *args)
135
137
  end
136
138
 
137
- def to_tz!(*args)
138
- Time.tz!(self, *args)
139
+ def as_tz(*args)
140
+ Time.as_tz(self, *args)
139
141
  end
140
142
  end
data/zones.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "zones"
3
- s.version = "1.0.1"
3
+ s.version = "1.1.0"
4
4
  s.author = "Steve Shreeve"
5
5
  s.email = "steve.shreeve@gmail.com"
6
6
  s.summary =
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: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-29 00:00:00.000000000 Z
11
+ date: 2023-07-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A friendly Ruby gem for time parsing and time zone conversion
14
14
  email: steve.shreeve@gmail.com
@@ -40,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  requirements: []
43
- rubygems_version: 3.4.14
43
+ rubygems_version: 3.4.16
44
44
  signing_key:
45
45
  specification_version: 4
46
46
  summary: A friendly Ruby gem for time parsing and time zone conversion