tztr 0.1.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/CHANGELOG.md +2 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +98 -0
- data/LICENSE.txt +21 -0
- data/README.md +78 -0
- data/bin/tztr +71 -0
- data/lib/tztr/version.rb +5 -0
- data/lib/tztr.rb +158 -0
- data/tztr.gemspec +20 -0
- metadata +105 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e92587c8ab85fb2d8d30742355d39affa2732dfca85f3e2593f0b24747db174e
|
|
4
|
+
data.tar.gz: 9bcb138c9a6d4344251307e0d302e9ecd33d2b36a2ed00d28e6dfc3f15ed943a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6a4438297ef18d9e41e8b2b9cf2a69c3c0edbadc6f22e5ef55ada8614f13bc4c838c125e5438e446ca7b34c9e0aac8123425f46656302ba019c4206764430397
|
|
7
|
+
data.tar.gz: 70432ef4777d42567ff4f84e34460e503a6d46de6216f1079621d4c6cc15c032c65684fce712b6c2c78350e7695aa4fa35662adcad4144ae92500baf64e544d2
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
tztr (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
date (3.5.1)
|
|
10
|
+
debug (1.11.1)
|
|
11
|
+
irb (~> 1.10)
|
|
12
|
+
reline (>= 0.3.8)
|
|
13
|
+
diff-lcs (1.6.2)
|
|
14
|
+
docile (1.4.1)
|
|
15
|
+
erb (6.0.2)
|
|
16
|
+
io-console (0.8.2)
|
|
17
|
+
irb (1.17.0)
|
|
18
|
+
pp (>= 0.6.0)
|
|
19
|
+
prism (>= 1.3.0)
|
|
20
|
+
rdoc (>= 4.0.0)
|
|
21
|
+
reline (>= 0.4.2)
|
|
22
|
+
pp (0.6.3)
|
|
23
|
+
prettyprint
|
|
24
|
+
prettyprint (0.2.0)
|
|
25
|
+
prism (1.9.0)
|
|
26
|
+
psych (5.3.1)
|
|
27
|
+
date
|
|
28
|
+
stringio
|
|
29
|
+
rdoc (7.2.0)
|
|
30
|
+
erb
|
|
31
|
+
psych (>= 4.0.0)
|
|
32
|
+
tsort
|
|
33
|
+
reline (0.6.3)
|
|
34
|
+
io-console (~> 0.5)
|
|
35
|
+
rspec (3.13.2)
|
|
36
|
+
rspec-core (~> 3.13.0)
|
|
37
|
+
rspec-expectations (~> 3.13.0)
|
|
38
|
+
rspec-mocks (~> 3.13.0)
|
|
39
|
+
rspec-core (3.13.6)
|
|
40
|
+
rspec-support (~> 3.13.0)
|
|
41
|
+
rspec-debugging (0.0.4)
|
|
42
|
+
rspec-expectations (>= 3)
|
|
43
|
+
rspec-expectations (3.13.5)
|
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
45
|
+
rspec-support (~> 3.13.0)
|
|
46
|
+
rspec-mocks (3.13.8)
|
|
47
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
48
|
+
rspec-support (~> 3.13.0)
|
|
49
|
+
rspec-support (3.13.7)
|
|
50
|
+
simplecov (0.22.0)
|
|
51
|
+
docile (~> 1.1)
|
|
52
|
+
simplecov-html (~> 0.11)
|
|
53
|
+
simplecov_json_formatter (~> 0.1)
|
|
54
|
+
simplecov-html (0.13.2)
|
|
55
|
+
simplecov_json_formatter (0.1.4)
|
|
56
|
+
stringio (3.2.0)
|
|
57
|
+
tsort (0.2.0)
|
|
58
|
+
|
|
59
|
+
PLATFORMS
|
|
60
|
+
arm64-darwin-25
|
|
61
|
+
ruby
|
|
62
|
+
|
|
63
|
+
DEPENDENCIES
|
|
64
|
+
debug (>= 1)
|
|
65
|
+
rspec (>= 3.10)
|
|
66
|
+
rspec-debugging
|
|
67
|
+
simplecov (>= 0.22)
|
|
68
|
+
tztr!
|
|
69
|
+
|
|
70
|
+
CHECKSUMS
|
|
71
|
+
date (3.5.1) sha256=750d06384d7b9c15d562c76291407d89e368dda4d4fff957eb94962d325a0dc0
|
|
72
|
+
debug (1.11.1) sha256=2e0b0ac6119f2207a6f8ac7d4a73ca8eb4e440f64da0a3136c30343146e952b6
|
|
73
|
+
diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962
|
|
74
|
+
docile (1.4.1) sha256=96159be799bfa73cdb721b840e9802126e4e03dfc26863db73647204c727f21e
|
|
75
|
+
erb (6.0.2) sha256=9fe6264d44f79422c87490a1558479bd0e7dad4dd0e317656e67ea3077b5242b
|
|
76
|
+
io-console (0.8.2) sha256=d6e3ae7a7cc7574f4b8893b4fca2162e57a825b223a177b7afa236c5ef9814cc
|
|
77
|
+
irb (1.17.0) sha256=168c4ddb93d8a361a045c41d92b2952c7a118fa73f23fe14e55609eb7a863aae
|
|
78
|
+
pp (0.6.3) sha256=2951d514450b93ccfeb1df7d021cae0da16e0a7f95ee1e2273719669d0ab9df6
|
|
79
|
+
prettyprint (0.2.0) sha256=2bc9e15581a94742064a3cc8b0fb9d45aae3d03a1baa6ef80922627a0766f193
|
|
80
|
+
prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85
|
|
81
|
+
psych (5.3.1) sha256=eb7a57cef10c9d70173ff74e739d843ac3b2c019a003de48447b2963d81b1974
|
|
82
|
+
rdoc (7.2.0) sha256=8650f76cd4009c3b54955eb5d7e3a075c60a57276766ebf36f9085e8c9f23192
|
|
83
|
+
reline (0.6.3) sha256=1198b04973565b36ec0f11542ab3f5cfeeec34823f4e54cebde90968092b1835
|
|
84
|
+
rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587
|
|
85
|
+
rspec-core (3.13.6) sha256=a8823c6411667b60a8bca135364351dda34cd55e44ff94c4be4633b37d828b2d
|
|
86
|
+
rspec-debugging (0.0.4) sha256=7a8e2dc240c140f0ed27b452a5661a56474ee8cf7b84c5bcbefd827ad36f0a6f
|
|
87
|
+
rspec-expectations (3.13.5) sha256=33a4d3a1d95060aea4c94e9f237030a8f9eae5615e9bd85718fe3a09e4b58836
|
|
88
|
+
rspec-mocks (3.13.8) sha256=086ad3d3d17533f4237643de0b5c42f04b66348c28bf6b9c2d3f4a3b01af1d47
|
|
89
|
+
rspec-support (3.13.7) sha256=0640e5570872aafefd79867901deeeeb40b0c9875a36b983d85f54fb7381c47c
|
|
90
|
+
simplecov (0.22.0) sha256=fe2622c7834ff23b98066bb0a854284b2729a569ac659f82621fc22ef36213a5
|
|
91
|
+
simplecov-html (0.13.2) sha256=bd0b8e54e7c2d7685927e8d6286466359b6f16b18cb0df47b508e8d73c777246
|
|
92
|
+
simplecov_json_formatter (0.1.4) sha256=529418fbe8de1713ac2b2d612aa3daa56d316975d307244399fa4838c601b428
|
|
93
|
+
stringio (3.2.0) sha256=c37cb2e58b4ffbd33fe5cd948c05934af997b36e0b6ca6fdf43afa234cf222e1
|
|
94
|
+
tsort (0.2.0) sha256=9650a793f6859a43b6641671278f79cfead60ac714148aabe4e3f0060480089f
|
|
95
|
+
tztr (0.1.0)
|
|
96
|
+
|
|
97
|
+
BUNDLED WITH
|
|
98
|
+
4.0.9
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Pepper
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
tztr
|
|
2
|
+
======
|
|
3
|
+

|
|
4
|
+
[](https://codecov.io/gh/dpep/tztr)
|
|
5
|
+
|
|
6
|
+
Timezone Translator - convert timestamps to local time.
|
|
7
|
+
|
|
8
|
+
Reads from stdin or file, auto-detects timestamp formats, and preserves the original format by default.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
gem install tztr
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
echo '2026-04-03T12:00:00Z' | tztr -t America/Los_Angeles
|
|
22
|
+
# 2026-04-03T05:00:00-07:00
|
|
23
|
+
|
|
24
|
+
echo '15:30 UTC' | tztr -t America/New_York
|
|
25
|
+
# 11:30 EDT
|
|
26
|
+
|
|
27
|
+
tail -f app.log | tztr
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Options
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
-f, --from TZ Input timezone (default: auto-detect)
|
|
34
|
+
-t, --to TZ Output timezone (default: UTC)
|
|
35
|
+
-F, --format FMT Output format: iso, short, time (default: preserve input)
|
|
36
|
+
-v, --version Show version
|
|
37
|
+
-h, --help Show this help
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Environment
|
|
41
|
+
|
|
42
|
+
Set `TZ` to change the default output timezone (overridden by `-t`):
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
export TZ=America/Los_Angeles
|
|
46
|
+
echo '2026-04-03T12:00:00Z' | tztr
|
|
47
|
+
# 2026-04-03T05:00:00-07:00
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Supported Formats
|
|
51
|
+
|
|
52
|
+
- ISO 8601: `2026-04-03T12:00:00Z`, `2026-04-03T12:00:00+05:30`
|
|
53
|
+
- Date + time: `2026-04-03 12:00:00 UTC`
|
|
54
|
+
- Time only: `15:30 UTC`, `08:30:45 PDT`
|
|
55
|
+
- Fractional seconds: `2026-04-03T12:00:00.123Z`
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
## Library
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
require "tztr"
|
|
62
|
+
|
|
63
|
+
Tztr.translate("log 2026-04-03T12:00:00Z event", to: "America/Los_Angeles")
|
|
64
|
+
# => "log 2026-04-03T05:00:00-07:00 event"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
----
|
|
69
|
+
## Contributing
|
|
70
|
+
|
|
71
|
+
Yes please :)
|
|
72
|
+
|
|
73
|
+
1. Fork it
|
|
74
|
+
1. Create your feature branch (`git checkout -b my-feature`)
|
|
75
|
+
1. Ensure the tests pass (`bundle exec rspec`)
|
|
76
|
+
1. Commit your changes (`git commit -am 'awesome new feature'`)
|
|
77
|
+
1. Push your branch (`git push origin my-feature`)
|
|
78
|
+
1. Create a Pull Request
|
data/bin/tztr
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'optparse'
|
|
5
|
+
|
|
6
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
|
|
7
|
+
require 'tztr'
|
|
8
|
+
|
|
9
|
+
local_tz = ENV['TZ']
|
|
10
|
+
|
|
11
|
+
options = {
|
|
12
|
+
from: nil,
|
|
13
|
+
to: local_tz || 'UTC',
|
|
14
|
+
format: nil
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
parser = OptionParser.new do |opts|
|
|
18
|
+
opts.banner = "Usage: tztr [options] [file]"
|
|
19
|
+
opts.separator ""
|
|
20
|
+
opts.separator "Timezone Translator - convert timestamps timezone. Reads from stdin or file."
|
|
21
|
+
opts.separator ""
|
|
22
|
+
|
|
23
|
+
opts.on("-f", "--from TZ", "Input timezone (default: auto-detect)") { |v| options[:from] = v }
|
|
24
|
+
opts.on("-t", "--to TZ", "Output timezone (default: UTC)") { |v| options[:to] = v }
|
|
25
|
+
opts.on("-l", "--list", "List timezone aliases") { Tztr::TIMEZONE_ALIASES.sort.each { |k, v| puts "%-12s %s" % [k, v] }; exit }
|
|
26
|
+
opts.on("-i", "--in-place", "Edit file in place") { options[:inplace] = true }
|
|
27
|
+
opts.on("-F", "--format FMT", %i[iso short time], "Output format: iso, short, time (default: preserve input)") { |v| options[:format] = v }
|
|
28
|
+
opts.on("-v", "--version", "Show version") { puts Tztr::VERSION; exit }
|
|
29
|
+
opts.on("-h", "--help", "Show this help") { puts opts; exit }
|
|
30
|
+
|
|
31
|
+
opts.separator ""
|
|
32
|
+
opts.separator "Environment:"
|
|
33
|
+
opts.separator " TZ Sets default output timezone (overridden by -t)"
|
|
34
|
+
opts.separator ""
|
|
35
|
+
opts.separator "Examples:"
|
|
36
|
+
opts.separator " echo '2026-04-03T12:00:00Z' | tztr -t sf"
|
|
37
|
+
opts.separator " echo '15:30 UTC' | tztr -t pst"
|
|
38
|
+
opts.separator " echo '12:00 EST' | tztr -t -8"
|
|
39
|
+
opts.separator " tail -f app.log | tztr -t nyc"
|
|
40
|
+
end
|
|
41
|
+
parser.parse!
|
|
42
|
+
|
|
43
|
+
options[:to] = Tztr.resolve_tz(options[:to])
|
|
44
|
+
options[:from] = Tztr.resolve_tz(options[:from]) || Tztr.resolve_tz(local_tz)
|
|
45
|
+
ENV['TZ'] = options[:to]
|
|
46
|
+
|
|
47
|
+
$stdout.sync = true
|
|
48
|
+
|
|
49
|
+
if ARGV.empty? && $stdin.tty?
|
|
50
|
+
puts parser
|
|
51
|
+
exit
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
if options[:inplace]
|
|
55
|
+
abort "#{parser.program_name}: -i requires a file argument" if ARGV.empty?
|
|
56
|
+
|
|
57
|
+
ARGV.each do |file|
|
|
58
|
+
content = File.read(file)
|
|
59
|
+
translated = content.each_line.map do |line|
|
|
60
|
+
Tztr.translate(line, to: options[:to], from: options[:from], format: options[:format])
|
|
61
|
+
end.join
|
|
62
|
+
File.write(file, translated) if translated != content
|
|
63
|
+
end
|
|
64
|
+
else
|
|
65
|
+
inputs = ARGV.empty? ? [$stdin] : ARGV.map { |f| File.open(f) }
|
|
66
|
+
inputs.each do |input|
|
|
67
|
+
input.each_line do |line|
|
|
68
|
+
print Tztr.translate(line, to: options[:to], from: options[:from], format: options[:format])
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
data/lib/tztr/version.rb
ADDED
data/lib/tztr.rb
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'time'
|
|
4
|
+
require_relative 'tztr/version'
|
|
5
|
+
|
|
6
|
+
module Tztr
|
|
7
|
+
PATTERNS = [
|
|
8
|
+
# ISO 8601 with Z or offset: 2026-04-03T12:34:56Z, 2026-04-03T12:34:56.123+00:00
|
|
9
|
+
/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})/,
|
|
10
|
+
# ISO 8601 without timezone: 2026-04-03T12:34:56
|
|
11
|
+
/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?/,
|
|
12
|
+
# Date space time with tz: 2026-04-03 12:34:56 UTC
|
|
13
|
+
/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)? ?(?:UTC|GMT|[A-Z]{2,4}|[+-]\d{4})/,
|
|
14
|
+
# Date space time: 2026-04-03 12:34:56
|
|
15
|
+
/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)?/,
|
|
16
|
+
# Time with tz: 12:34:56 UTC, 12:34 PST
|
|
17
|
+
/\b\d{1,2}:\d{2}(?::\d{2}(?:\.\d+)?)? ?(?:UTC|GMT|[A-Z]{2,4}|[+-]\d{4})\b/,
|
|
18
|
+
# Time with offset: 12:34:56+00:00
|
|
19
|
+
/\b\d{1,2}:\d{2}(?::\d{2}(?:\.\d+)?)?[+-]\d{2}:?\d{2}\b/,
|
|
20
|
+
# Bare time: 12:34:56, 12:34
|
|
21
|
+
/\b\d{1,2}:\d{2}(?::\d{2}(?:\.\d+)?)?\b/,
|
|
22
|
+
].freeze
|
|
23
|
+
|
|
24
|
+
TIMEZONE_ALIASES = {
|
|
25
|
+
# UTC
|
|
26
|
+
'utc' => 'UTC', 'gmt' => 'UTC', 'z' => 'UTC',
|
|
27
|
+
# US Eastern
|
|
28
|
+
'est' => 'America/New_York', 'edt' => 'America/New_York', 'et' => 'America/New_York',
|
|
29
|
+
'eastern' => 'America/New_York',
|
|
30
|
+
# US Central
|
|
31
|
+
'cst' => 'America/Chicago', 'cdt' => 'America/Chicago', 'ct' => 'America/Chicago',
|
|
32
|
+
'central' => 'America/Chicago',
|
|
33
|
+
# US Mountain
|
|
34
|
+
'mst' => 'America/Denver', 'mdt' => 'America/Denver', 'mt' => 'America/Denver',
|
|
35
|
+
'mountain' => 'America/Denver',
|
|
36
|
+
# US Pacific
|
|
37
|
+
'pst' => 'America/Los_Angeles', 'pdt' => 'America/Los_Angeles', 'pt' => 'America/Los_Angeles',
|
|
38
|
+
'pacific' => 'America/Los_Angeles',
|
|
39
|
+
# US Other
|
|
40
|
+
'hst' => 'Pacific/Honolulu', 'akst' => 'America/Anchorage', 'akdt' => 'America/Anchorage',
|
|
41
|
+
# Europe
|
|
42
|
+
'cet' => 'Europe/Berlin', 'cest' => 'Europe/Berlin',
|
|
43
|
+
'gmt' => 'Europe/London', 'bst' => 'Europe/London',
|
|
44
|
+
'ist' => 'Asia/Kolkata',
|
|
45
|
+
# Asia/Pacific
|
|
46
|
+
'jst' => 'Asia/Tokyo', 'kst' => 'Asia/Seoul',
|
|
47
|
+
'cst_china' => 'Asia/Shanghai', 'hkt' => 'Asia/Hong_Kong',
|
|
48
|
+
'aest' => 'Australia/Sydney', 'aedt' => 'Australia/Sydney',
|
|
49
|
+
'nzst' => 'Pacific/Auckland', 'nzdt' => 'Pacific/Auckland',
|
|
50
|
+
# Cities
|
|
51
|
+
'sf' => 'America/Los_Angeles', 'la' => 'America/Los_Angeles', 'seattle' => 'America/Los_Angeles',
|
|
52
|
+
'denver' => 'America/Denver',
|
|
53
|
+
'chicago' => 'America/Chicago',
|
|
54
|
+
'nyc' => 'America/New_York', 'boston' => 'America/New_York', 'miami' => 'America/New_York',
|
|
55
|
+
'london' => 'Europe/London',
|
|
56
|
+
'paris' => 'Europe/Paris', 'berlin' => 'Europe/Berlin', 'amsterdam' => 'Europe/Amsterdam',
|
|
57
|
+
'tokyo' => 'Asia/Tokyo',
|
|
58
|
+
'sydney' => 'Australia/Sydney',
|
|
59
|
+
'mumbai' => 'Asia/Kolkata', 'delhi' => 'Asia/Kolkata',
|
|
60
|
+
'shanghai' => 'Asia/Shanghai', 'beijing' => 'Asia/Shanghai',
|
|
61
|
+
'hong_kong' => 'Asia/Hong_Kong', 'hongkong' => 'Asia/Hong_Kong',
|
|
62
|
+
'singapore' => 'Asia/Singapore',
|
|
63
|
+
'seoul' => 'Asia/Seoul',
|
|
64
|
+
'honolulu' => 'Pacific/Honolulu', 'hawaii' => 'Pacific/Honolulu',
|
|
65
|
+
'anchorage' => 'America/Anchorage', 'alaska' => 'America/Anchorage',
|
|
66
|
+
'toronto' => 'America/Toronto',
|
|
67
|
+
'vancouver' => 'America/Vancouver',
|
|
68
|
+
'auckland' => 'Pacific/Auckland',
|
|
69
|
+
'dubai' => 'Asia/Dubai',
|
|
70
|
+
'sao_paulo' => 'America/Sao_Paulo',
|
|
71
|
+
}.freeze
|
|
72
|
+
|
|
73
|
+
module_function
|
|
74
|
+
|
|
75
|
+
def resolve_tz(input)
|
|
76
|
+
return input if input.nil?
|
|
77
|
+
|
|
78
|
+
# Numeric offset: -7 -> Etc/GMT+7 (POSIX sign is inverted)
|
|
79
|
+
if input.match?(/\A[+-]?\d{1,2}\z/)
|
|
80
|
+
n = input.to_i
|
|
81
|
+
return 'UTC' if n == 0
|
|
82
|
+
|
|
83
|
+
return "Etc/GMT#{n > 0 ? '-' : '+'}#{n.abs}"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
TIMEZONE_ALIASES[input.downcase.tr(' ', '_')] || input
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def translate(line, to: 'UTC', from: nil, format: nil)
|
|
90
|
+
to = resolve_tz(to)
|
|
91
|
+
from = resolve_tz(from)
|
|
92
|
+
ENV['TZ'] = to
|
|
93
|
+
result = line.dup
|
|
94
|
+
|
|
95
|
+
PATTERNS.each do |pattern|
|
|
96
|
+
next unless result.match?(pattern)
|
|
97
|
+
|
|
98
|
+
result.gsub!(pattern) do |match|
|
|
99
|
+
begin
|
|
100
|
+
time = parse(match, from:, to:)
|
|
101
|
+
format_time(time.localtime, format, match)
|
|
102
|
+
rescue ArgumentError
|
|
103
|
+
match
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
break result
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
result
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def parse(str, from: nil, to: 'UTC')
|
|
114
|
+
if has_timezone?(str)
|
|
115
|
+
Time.parse(str)
|
|
116
|
+
elsif from
|
|
117
|
+
ENV['TZ'] = from
|
|
118
|
+
t = Time.parse(str).utc
|
|
119
|
+
ENV['TZ'] = to
|
|
120
|
+
t.localtime
|
|
121
|
+
else
|
|
122
|
+
ENV['TZ'] = to
|
|
123
|
+
Time.parse(str)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def has_timezone?(str)
|
|
128
|
+
str.match?(/Z$|[+-]\d{2}:?\d{2}$| ?(?:UTC|GMT|[A-Z]{2,4}|[+-]\d{4})$/)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def format_time(time, fmt, original)
|
|
132
|
+
case fmt
|
|
133
|
+
when :short then return time.strftime('%Y-%m-%d %H:%M')
|
|
134
|
+
when :time then return time.strftime('%H:%M:%S')
|
|
135
|
+
when :iso then return time.strftime('%Y-%m-%d %H:%M:%S')
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Preserve input format
|
|
139
|
+
tz = time.utc_offset == 0 ? 'Z' : time.strftime('%:z')
|
|
140
|
+
|
|
141
|
+
case original
|
|
142
|
+
when /^\d{4}-\d{2}-\d{2}T/
|
|
143
|
+
has_frac = original.match?(/T\d{2}:\d{2}:\d{2}\.\d+/)
|
|
144
|
+
base = has_frac ? time.strftime('%Y-%m-%dT%H:%M:%S.%L') : time.strftime('%Y-%m-%dT%H:%M:%S')
|
|
145
|
+
base + tz
|
|
146
|
+
when /^\d{4}-\d{2}-\d{2} /
|
|
147
|
+
has_frac = original.match?(/ \d{2}:\d{2}:\d{2}\.\d+/)
|
|
148
|
+
base = has_frac ? time.strftime('%Y-%m-%d %H:%M:%S.%L') : time.strftime('%Y-%m-%d %H:%M:%S')
|
|
149
|
+
base + " " + (time.utc? ? 'UTC' : time.strftime('%Z'))
|
|
150
|
+
when /^\d{1,2}:\d{2}(?::\d{2})/
|
|
151
|
+
time.strftime('%H:%M:%S') + " " + (time.utc? ? 'UTC' : time.strftime('%Z'))
|
|
152
|
+
when /^\d{1,2}:\d{2}/
|
|
153
|
+
time.strftime('%H:%M') + " " + (time.utc? ? 'UTC' : time.strftime('%Z'))
|
|
154
|
+
else
|
|
155
|
+
time.strftime('%Y-%m-%d %H:%M:%S') + " " + (time.utc? ? 'UTC' : time.strftime('%Z'))
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
data/tztr.gemspec
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require_relative "lib/tztr"
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.authors = ["Daniel Pepper"]
|
|
5
|
+
s.description = "Convert timestamps between timezones. Reads from stdin or file, auto-detects timestamp formats, and preserves the original format by default."
|
|
6
|
+
s.executables = ["tztr"]
|
|
7
|
+
s.files = `git ls-files * ':!:spec'`.split("\n")
|
|
8
|
+
s.homepage = "https://github.com/dpep/tztr"
|
|
9
|
+
s.license = "MIT"
|
|
10
|
+
s.name = File.basename(__FILE__, ".gemspec")
|
|
11
|
+
s.summary = "Timezone Translator"
|
|
12
|
+
s.version = Tztr::VERSION
|
|
13
|
+
|
|
14
|
+
s.required_ruby_version = ">= 3.2"
|
|
15
|
+
|
|
16
|
+
s.add_development_dependency 'debug', '>= 1'
|
|
17
|
+
s.add_development_dependency 'rspec', '>= 3.10'
|
|
18
|
+
s.add_development_dependency 'rspec-debugging'
|
|
19
|
+
s.add_development_dependency 'simplecov', '>= 0.22'
|
|
20
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: tztr
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Daniel Pepper
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: debug
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '1'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '1'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rspec
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '3.10'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '3.10'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rspec-debugging
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: simplecov
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0.22'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0.22'
|
|
68
|
+
description: Convert timestamps between timezones. Reads from stdin or file, auto-detects
|
|
69
|
+
timestamp formats, and preserves the original format by default.
|
|
70
|
+
executables:
|
|
71
|
+
- tztr
|
|
72
|
+
extensions: []
|
|
73
|
+
extra_rdoc_files: []
|
|
74
|
+
files:
|
|
75
|
+
- CHANGELOG.md
|
|
76
|
+
- Gemfile
|
|
77
|
+
- Gemfile.lock
|
|
78
|
+
- LICENSE.txt
|
|
79
|
+
- README.md
|
|
80
|
+
- bin/tztr
|
|
81
|
+
- lib/tztr.rb
|
|
82
|
+
- lib/tztr/version.rb
|
|
83
|
+
- tztr.gemspec
|
|
84
|
+
homepage: https://github.com/dpep/tztr
|
|
85
|
+
licenses:
|
|
86
|
+
- MIT
|
|
87
|
+
metadata: {}
|
|
88
|
+
rdoc_options: []
|
|
89
|
+
require_paths:
|
|
90
|
+
- lib
|
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '3.2'
|
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - ">="
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
requirements: []
|
|
102
|
+
rubygems_version: 3.6.9
|
|
103
|
+
specification_version: 4
|
|
104
|
+
summary: Timezone Translator
|
|
105
|
+
test_files: []
|