zone 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ec4bf54c71190831471974566598f5f99d1398588eb781d6bf05d5b09e8d208b
4
+ data.tar.gz: b2984b02b1e3b384bfc2593e3f24e9fb79507eec7bd7b4fa33981023b2daeda7
5
+ SHA512:
6
+ metadata.gz: 1ab5600b1439b2ff8692eaa08baf9c8687978f5c8c9e3639d1031c509fb45db59379f77359d76274c8030ca02bb7589c57ebf3692867a55188567a3c39b8c6a6
7
+ data.tar.gz: 2223d049e9c21f2805c0ab411c9623a11fd5da50a05d742e48d9dfc0edff90a2fe8481db551d3f46a19f09103a35156a0b91d89419fc553ac18b1d18ddb824e9
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-11-04
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 David Gillis
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,39 @@
1
+ # Zone
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zone`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gillisd/zone.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
data/exe/zone ADDED
@@ -0,0 +1,258 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'rubygems'
6
+ require 'logger'
7
+ require 'optparse'
8
+ require 'tzinfo'
9
+ require 'time'
10
+ require 'date'
11
+
12
+ options = { delimiter: nil, strftime: nil, iso8601: false, pretty: false, headers: false, unix: false, index: 1, zone: nil, utc: false, local: false }
13
+ parser = OptionParser.new do |parser|
14
+ parser.on '--index N', '-i N', Integer, 'Index of the field to convert (default: 1)'
15
+ parser.on '--delimiter PATTERN', '-d', 'Field delimiter (default: space)'
16
+ parser.on '--iso8601', 'Output in ISO 8601 (default: true)'
17
+ parser.on '--strftime FORMAT', '-f', 'Output format using strftime (default: none)'
18
+ parser.on '--pretty', 'Output in pretty format (e.g., "Jan 02 - 03:04 PM")'
19
+ parser.on '--unix', 'Output as Unix timestamp (default: false)'
20
+ parser.on '--zone TZ', 'Convert to time zone (default: local time zone)'
21
+ parser.on '--local', 'Convert to local time zone (alias for --zone local)'
22
+ parser.on '--utc', 'Convert to UTC time zone (alias for --zone UTC)'
23
+ parser.on '--headers', 'Skip the first line as headers'
24
+ parser.on '--verbose', '-v', 'Enable verbose/debug output'
25
+ parser.on '--help', '-h', 'Show this help message' do
26
+ puts parser
27
+ exit
28
+ end
29
+ end
30
+
31
+ parser.parse!(into: options)
32
+
33
+ COLORS = {
34
+ reset: "\e[0m",
35
+ bold: "\e[1m",
36
+ cyan: "\e[36m",
37
+ yellow: "\e[33m",
38
+ red: "\e[31m",
39
+ gray: "\e[90m"
40
+ }
41
+
42
+ $logger = Logger.new($stderr).tap do |l|
43
+ l.formatter = ->(severity, _datetime, _progname, message) {
44
+ color = case severity
45
+ when "INFO" then COLORS[:cyan]
46
+ when "WARN" then COLORS[:yellow]
47
+ when "ERROR" then COLORS[:red]
48
+ else COLORS[:gray]
49
+ end
50
+
51
+ prefix = case severity
52
+ when "INFO" then "→"
53
+ when "WARN" then "⚠"
54
+ when "ERROR" then "✗"
55
+ else "·"
56
+ end
57
+
58
+ "#{color}#{prefix} #{message}#{COLORS[:reset]}\n"
59
+ }
60
+ l.level = options.delete(:verbose) ? Logger::INFO : Logger::WARN
61
+ end
62
+
63
+ format = (
64
+ case options
65
+ in { strftime: nil, iso8601: true, unix: false, pretty: false } then :iso8601
66
+ in { strftime: nil, iso8601: false, unix: true, pretty: false } then :unix
67
+ in { strftime: String, iso8601: false, unix: false, pretty: false } then :strftime
68
+ in { strftime: nil, iso8601: false, unix: false, pretty: true } then :pretty
69
+ in { strftime: nil, iso8601: false, unix: false, pretty: false } then :iso8601
70
+ else
71
+ $logger.error 'Error: Only one of --strftime, --iso8601, or --unix can be specified.'
72
+ exit 1
73
+ end
74
+ )
75
+
76
+ zone = (
77
+ case options
78
+ in { utc: true, local: false, zone: nil } then 'utc'
79
+ in { utc: false, local: true, zone: nil } then 'local'
80
+ in { zone: nil, utc: false, local: false } then 'utc'
81
+ in { zone: String => z, utc: false, local: false } then z
82
+ else
83
+ $logger.error 'Error: Only one of --zone, --local, or --utc can be specified.'
84
+ exit 1
85
+ end
86
+ )
87
+
88
+ class TimezoneSearch
89
+ attr_reader :keyword, :debug
90
+
91
+ def self.all_zones
92
+ TZInfo::Timezone.all_identifiers
93
+ end
94
+
95
+ def initialize(
96
+ keyword,
97
+ debug: false,
98
+ logger: $logger || Logger.new($stderr)
99
+ )
100
+ @keyword = keyword
101
+ @debug = debug
102
+ @logger = logger
103
+ end
104
+
105
+ def execute
106
+ begin
107
+ TZInfo::Timezone.get(keyword)
108
+ rescue TZInfo::InvalidTimezoneIdentifier
109
+ search_wildcard
110
+ end
111
+ end
112
+
113
+ def us_wildcard
114
+ keyword
115
+ .gsub(/^(?:US)?\/?/, 'US/')
116
+ .gsub(/$/,'.*')
117
+ .then { Regexp.new(it, Regexp::IGNORECASE) }
118
+ end
119
+
120
+ def all_wildcard
121
+ Regexp.new(".*#{keyword}.*", Regexp::IGNORECASE)
122
+ end
123
+
124
+ def search_wildcard
125
+ case self.class.all_zones
126
+ in [*, ^(us_wildcard) => found_zone, *]
127
+ in [*, ^(all_wildcard) => found_zone, *]
128
+ else nil
129
+ end
130
+
131
+ return unless found_zone
132
+
133
+ TZInfo::Timezone.get(found_zone).tap do
134
+ @logger.info "Using time zone '#{found_zone}' matching pattern '#{@keyword}'."
135
+ end
136
+ end
137
+
138
+ private
139
+
140
+ def log(message)
141
+ warn message if @debug
142
+ end
143
+ end
144
+
145
+ zone_callables = Hash.new do |hash, key|
146
+ hash[key] = ->(time) {
147
+ search = TimezoneSearch.new(key)
148
+ if zone = search.execute
149
+ zone.to_local(time)
150
+ else
151
+ $logger.warn "Error: Invalid time zone identifier '#{key}'."
152
+ time
153
+ end
154
+ }
155
+ end
156
+
157
+ zone_callables.merge!(
158
+ 'utc' => ->(t) { t.utc },
159
+ 'local' => ->(t) { t.localtime },
160
+ 'UTC' => ->(t) { t.utc },
161
+ )
162
+
163
+ actual_index = options[:index] - 1
164
+ zone_callable = zone_callables[zone]
165
+
166
+ # Detect if args are timestamps (not filenames)
167
+ timestamps = []
168
+ if ARGV.any? && ARGV.all? { |arg| arg.match?(/^\d/) || arg.match?(/[A-Z][a-z]{2}/) || arg.match?(/:/) }
169
+ $logger.info "Treating arguments as timestamp strings."
170
+ timestamps = ARGV.dup
171
+ ARGV.clear # Clear so ARGF will read from STDIN if piped
172
+ end
173
+
174
+ # Build input enumerable
175
+ input = (
176
+ if timestamps.any?
177
+ timestamps.each
178
+ elsif ARGV.any? || !STDIN.tty?
179
+ ARGF.each_line(chomp: true)
180
+ else
181
+ [Time.now.to_s].each
182
+ end
183
+ )
184
+
185
+ input.each do |line|
186
+ case [options, $.]
187
+ in { headers: true }, 1 then next
188
+ in { delimiter: String => delimiter }
189
+ in { delimiter: nil }
190
+ $logger.info "Auto-detecting delimiter for line: #{line.inspect}"
191
+ options[:delimiter] = (
192
+ case line
193
+ in /,\s*/
194
+ $logger.info "Using comma with whitespace as delimiter."
195
+ /,\s*/
196
+ in /\t/
197
+ $logger.info "Using tab as delimiter."
198
+ "\t"
199
+ in /,/
200
+ $logger.info "Using comma as delimiter."
201
+ ','
202
+ else
203
+ $logger.info "Could not detect delimiter. Using whitespace."
204
+ /\s+/
205
+ end
206
+ )
207
+ else
208
+ ""
209
+ end
210
+
211
+ delimiter = options[:delimiter] || ""
212
+
213
+ fields = (
214
+ case [line, delimiter]
215
+ in String, "" then [line]
216
+ in String, /^.+$/ then line.split(delimiter)
217
+ else [line]
218
+ end
219
+ )
220
+
221
+ target = fields[actual_index]
222
+
223
+ time = (
224
+ begin
225
+ case target
226
+ in Time then target
227
+ in DateTime then target.to_time
228
+ in Date then target.to_time
229
+ in /^[0-9\.]+$/
230
+ Time.at(target.to_f)
231
+ else
232
+ DateTime.parse(target).to_time
233
+ end
234
+ rescue StandardError => e
235
+ $logger.warn "Warning: Could not parse time '#{target}'. Skipping line."
236
+ $logger.warn " #{e.class}: #{e.message}"
237
+ next
238
+ end
239
+ )
240
+
241
+ converted = zone_callable.call(time)
242
+ formatted = (
243
+ case format
244
+ in :pretty
245
+ if (Time.now - converted).abs > 30 * 24 * 60 * 60
246
+ converted.strftime("%b %d, %Y - %I:%M %p %Z")
247
+ else
248
+ converted.strftime("%b %d - %I:%M %p %Z")
249
+ end
250
+ in :strftime then converted.strftime(options[:strftime])
251
+ in :iso8601 then converted.iso8601
252
+ in :unix then converted.to_i
253
+ end
254
+ )
255
+
256
+ fields[actual_index] = formatted
257
+ puts fields.join(delimiter)
258
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zone
4
+ VERSION = "0.1.0"
5
+ end
data/lib/zone.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "zone/version"
4
+
5
+ module Zone
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Gillis
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: tzinfo
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: minitest
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.16'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '5.16'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rake
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '13.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '13.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rubocop
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.21'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.21'
68
+ email:
69
+ - david@flipmine.com
70
+ executables:
71
+ - zone
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - CHANGELOG.md
76
+ - LICENSE.txt
77
+ - README.md
78
+ - Rakefile
79
+ - exe/zone
80
+ - lib/zone.rb
81
+ - lib/zone/version.rb
82
+ homepage: https://github.com/gillisd/zone
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ homepage_uri: https://github.com/gillisd/zone
87
+ source_code_uri: https://github.com/gillisd/zone
88
+ changelog_uri: https://github.com/gillisd/zone/blob/main/CHANGELOG.md
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.2.0
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 3.7.2
104
+ specification_version: 4
105
+ summary: An ergonomic CLI for quick time-zone conversion
106
+ test_files: []