zones 0.5.0
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.
- checksums.yaml +7 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +19 -0
- data/lib/zones.rb +98 -0
- data/zones.gemspec +13 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 93f46e66599119d3fc322a6f13b99004465fb68341036264e1d38e3c4c939a63
|
4
|
+
data.tar.gz: 1dc9d208033d43fc909be86b210122149277f9c01b4c6561ecb271b14417b350
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: df746b79666d56534e923d5e63b6690cf7b92a9f93cf646167c8e610e9d362967abffb07cf431bce3aa1fbfbbb468585a5901821f46c12fe0bdae990cc0a6da3
|
7
|
+
data.tar.gz: b83975032a26e13b851f0efbd3cf3f136714c63ce62c9082e01351fd1661b761ff6c86a1e51290e316194797de736f30f5dc0c41b5bb059eeeb825c4eada6519
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.0
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Steve Shreeve
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# zones
|
2
|
+
|
3
|
+
`zones` is a Ruby gem that makes it easy to parse time and convert it between time zones.
|
4
|
+
|
5
|
+
## Examples
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# when parsing, the "!" means to ignore the supplied time zone / offset
|
9
|
+
x = "3 August 2017 11:43 +0415".to_tz("US/Pacific") # 2017-08-03 00:28:00 -0700
|
10
|
+
y = "3 August 2017 11:43 +0415".to_tz!("US/Pacific") # 2017-08-03 11:43:00 -0700
|
11
|
+
|
12
|
+
# when converting, the "!" means to only change the offset
|
13
|
+
x.to_tz("US/Eastern") # 2017-08-03 03:28:00 -0400
|
14
|
+
y.to_tz!("US/Eastern") # 2017-08-03 11:43:00 -0400
|
15
|
+
```
|
16
|
+
|
17
|
+
## License
|
18
|
+
|
19
|
+
This software is licensed under terms of the MIT License.
|
data/lib/zones.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require "tzinfo"
|
2
|
+
|
3
|
+
class Time
|
4
|
+
def self.parse_str(str, ignore_offset=false)
|
5
|
+
case str
|
6
|
+
when %r!^((?:19|20)\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)?\.?(\d+)?([-+]\d\d:?\d\d)?!
|
7
|
+
ymd = [$1.to_i, $2.to_i, $3.to_i]
|
8
|
+
hms = [$4.to_i, $5.to_i, "#{$6}.#{$7}".to_f]
|
9
|
+
off = $8.sub(/(\d)(\d\d)$/,"\1:\2") if $8 && !ignore_offset
|
10
|
+
when %r!^
|
11
|
+
(?:(0[1-9]|[12]\d|3[01]|[1-9][-/ ])[-/ ]? # $1: day
|
12
|
+
((?>[a-z]{3,9}))[-/ ]? # $2: month
|
13
|
+
((?>19|20)\d\d) # $3: year
|
14
|
+
| # or...
|
15
|
+
((?>19|20)\d\d)[-/]? # $4: year
|
16
|
+
(0[1-9]|1[012]|[1-9][-/])[-/]? # $5: month
|
17
|
+
(0[1-9]|[12]\d|3[01]|[1-9][\sT]) # $6: day
|
18
|
+
| # or...
|
19
|
+
(0[1-9]|1[012]|[1-9][-/])[-/]? # $7: month
|
20
|
+
(0[1-9]|[12]\d|3[01]|[1-9][-/])[-/]? # $8: day
|
21
|
+
((?>19|20)\d\d) # $9: year
|
22
|
+
)\s?T?\s?
|
23
|
+
(\d\d?)? # $10: hour
|
24
|
+
:?(\d\d)? # $11: min
|
25
|
+
:?(\d\d)? # $12: sec
|
26
|
+
\.?(\d+)? # $13: dec
|
27
|
+
\s?(?:(a|p)?m)? # $14: am/pm
|
28
|
+
\s?(([-+])?(\d\d):?(\d\d)|UTC|GMT)? # $15: offset ($16=sign, $17=hours, $18=mins)
|
29
|
+
!iox
|
30
|
+
ymd = $1 ? [$3.to_i, month_num($2), $1.to_i] : $4 ? [$4.to_i, $5.to_i, $6.to_i] : [$9.to_i, $7.to_i, $8.to_i]
|
31
|
+
hms = [$14 ? ($10.to_i % 12) + (($14=="P" || $14=="p") ? 12 : 0) : $10.to_i, $11.to_i, "#{$12}.#{$13}".to_f]
|
32
|
+
off = ($17 ? "#{$16||'+'}#{$17}:#{$18}" : "+00:00") if $15 && !ignore_offset
|
33
|
+
else
|
34
|
+
raise "can't parse: #{str}"
|
35
|
+
end
|
36
|
+
off ? [ymd, hms, off] : [ymd, hms]
|
37
|
+
end
|
38
|
+
|
39
|
+
# get month number
|
40
|
+
def self.month_num(str)
|
41
|
+
(@month_num ||= {
|
42
|
+
"jan" => 1, "january" => 1, "jul" => 7, "july" => 7,
|
43
|
+
"feb" => 2, "february" => 2, "aug" => 8, "august" => 8,
|
44
|
+
"mar" => 3, "march" => 3, "sep" => 9, "septmeber" => 9,
|
45
|
+
"apr" => 4, "april" => 4, "oct" => 10, "october" => 10,
|
46
|
+
"may" => 5, "nov" => 11, "november" => 11,
|
47
|
+
"jun" => 6, "june" => 6, "dec" => 12, "december" => 12,
|
48
|
+
})[str.downcase] or raise "bad month: #{str}"
|
49
|
+
end
|
50
|
+
|
51
|
+
# parse time and honor desired timezone
|
52
|
+
def self.to_tz(str, to_tz=nil, ignore_offset=false)
|
53
|
+
ymd, hms, off = parse_str(str, ignore_offset)
|
54
|
+
out = Time.new(*ymd, *hms, off)
|
55
|
+
if to_tz
|
56
|
+
if off
|
57
|
+
out = out.to_tz(to_tz)
|
58
|
+
else
|
59
|
+
utc = out.utc
|
60
|
+
off = TZInfo::Timezone.get(to_tz).utc_to_local(utc) - utc
|
61
|
+
out = Time.new(*ymd, *hms, off)
|
62
|
+
end
|
63
|
+
else
|
64
|
+
out
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# ignore supplied timezone, use local
|
69
|
+
def self.to_tz!(str, to_tz=nil)
|
70
|
+
to_tz(str, to_tz, true)
|
71
|
+
end
|
72
|
+
|
73
|
+
# transform time to new timezone
|
74
|
+
def to_tz(to_tz)
|
75
|
+
utc = utc? ? self : getutc
|
76
|
+
raw = TZInfo::Timezone.get(to_tz).utc_to_local(utc)
|
77
|
+
all = raw.to_a[1,5].reverse.push(strftime("%S.%6N").to_f) # retain fractional seconds
|
78
|
+
out = Time.new(*all, raw - utc)
|
79
|
+
end
|
80
|
+
|
81
|
+
# preserve time but change offset
|
82
|
+
def to_tz!(to_tz)
|
83
|
+
all = to_a[1,5].reverse.push(strftime("%S.%6N").to_f) # retain fractional seconds
|
84
|
+
raw = Time.utc(*all)
|
85
|
+
utc = TZInfo::Timezone.get(to_tz).local_to_utc(raw)
|
86
|
+
out = Time.new(*all, raw - utc)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class String
|
91
|
+
def to_tz(*args)
|
92
|
+
Time.to_tz(self, *args)
|
93
|
+
end
|
94
|
+
|
95
|
+
def to_tz!(*args)
|
96
|
+
Time.to_tz!(self, *args)
|
97
|
+
end
|
98
|
+
end
|
data/zones.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "zones"
|
5
|
+
s.version = "0.5.0"
|
6
|
+
s.author = "Steve Shreeve"
|
7
|
+
s.email = "steve.shreeve@gmail.com"
|
8
|
+
s.summary = "Easy time parsing and conversion between time zones"
|
9
|
+
s.description = "This gem makes it easy to work with time zones."
|
10
|
+
s.homepage = "https://github.com/shreeve/zones"
|
11
|
+
s.license = "MIT"
|
12
|
+
s.files = `git ls-files`.split("\n") - %w[.gitignore]
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zones
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Shreeve
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This gem makes it easy to work with time zones.
|
14
|
+
email: steve.shreeve@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".ruby-version"
|
20
|
+
- Gemfile
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- lib/zones.rb
|
24
|
+
- zones.gemspec
|
25
|
+
homepage: https://github.com/shreeve/zones
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.7.6
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Easy time parsing and conversion between time zones
|
49
|
+
test_files: []
|