...
 
Commits (2)
......@@ -3,10 +3,10 @@
source 'https://rubygems.org'
group :test do
gem 'pry'
gem 'rake'
gem 'rspec'
gem 'rubocop'
gem 'pry'
end
group :release do
......
......@@ -10,6 +10,28 @@ a given time and converts it to the offsets you've specified.
- [Download the latest artifact](https://gitlab.com/alexives/timezone-helper-alfred-workflow/-/jobs/artifacts/master/download?job=build&file=Timezone-Helper.alfredworkflow)
- Open the alfredworkflow file in Alfred
## Usage
Show all of the configured timezones in your current time `timez`:
![result of timez command](/screenshots/default_times.png)
Show all configured timezones in a supplied time `timez 22:00`:
![result of timez 10p](/screenshots/time_calc.png)
Filter out the timezones to show a specific one calculated `timez <filter> <time>:
![result of timez central 10p](/screenshots/time_calc_filter.png)
Add a new timezone with `timez update <zone name>, <utc offset>`:
![result of timez update new, 10](/screenshots/add_time.png)
Update an existing timezone's offset with the same command:
![result of timez update NA Central, -6](/screenshots/update_time.png)
## Development
### Setup
......
......@@ -8,24 +8,33 @@ def get_zones(file)
end
def format_zones_json(zones, argument, time=Time.now)
filter = ''
filters = []
time_a = []
argument.split(' ').each do |arg|
if arg.match(/(\d+):?(\d+)?(am?|pm?)?/i)
time_a = arg.match(/(\d+):?(\d+)?(am?|pm?)?/i).to_a
time_a[1] = time_a[1].to_i + 12 if time_a[3]&.match(/pm?/i)
else
filter = arg
last_time = ''
if argument && !argument.empty?
argument.split(' ').each do |arg|
if arg.match(/(\d+):?(\d+)?(am?|pm?)?/i)
filters.push(last_time) unless time_a.empty?
last_time = arg
time_a = arg.match(/(\d+):?(\d+)?(am?|pm?)?/i).to_a
time_a[1] = time_a[1].to_i + 12 if time_a[3]&.match(/pm?/i)
time_a[2] = 0 if time_a[2].nil?
else
filters.push(arg)
end
end
end if argument && !argument.empty?
end
# throwaway way to make a new date object in local time that we can convert to UTC based optionally with minutes/hours
time = Time.new(2000, 'jan', 1, (time_a[1] || time.hour), (time_a[2] || time.min), 0, time.strftime('%:z'))
time_string = time.strftime('%H:%M')
time = time.utc
items = zones.select do |member, offset|
filter.empty? || member.match(/#{filter}/im)
items = zones.select do |member, _offset|
# All filters must match against the zone for it to pass
filters.reject do |filter|
member.match(/#{filter}/im)
end.empty?
end.map do |member, offset|
member_time = (time + offset * 60 * 60)
{
......
......@@ -12,37 +12,41 @@ describe 'get_zones' do
end
describe 'format_zones_json' do
let(:frozen_time) { Time.new(2016, 'jan', 6, 17, 0, 0, '+00:00') }
let(:frozen_time) { Time.new(2016, 'jan', 6, 17, 17, 0, '+00:00') }
let(:zones) do
{
'First' => -1,
'Second' => 10,
'Third' => -5
'Third' => -5,
'10' => 10
}
end
it 'returns the current time in the various zones if nothing is supplied' do
formatted = JSON.parse(format_zones_json(zones, nil, frozen_time))['items']
expect(formatted.length).to eq(3)
expect(formatted[0]['title']).to match(/^16:00 - .*$/)
expect(formatted[1]['title']).to match(/^03:00 - .*$/)
expect(formatted[2]['title']).to match(/^12:00 - .*$/)
expect(formatted.length).to eq(4)
expect(formatted[0]['title']).to match(/^16:17 - .*$/)
expect(formatted[1]['title']).to match(/^03:17 - .*$/)
expect(formatted[2]['title']).to match(/^12:17 - .*$/)
expect(formatted[3]['title']).to match(/^03:17 - .*$/)
end
it 'returns the current time, but with hours changed if given hours' do
formatted = JSON.parse(format_zones_json(zones, '10', frozen_time))['items']
expect(formatted.length).to eq(3)
expect(formatted.length).to eq(4)
expect(formatted[0]['title']).to match(/^09:00 - .*$/)
expect(formatted[1]['title']).to match(/^20:00 - .*$/)
expect(formatted[2]['title']).to match(/^05:00 - .*$/)
expect(formatted[3]['title']).to match(/^20:00 - .*$/)
end
it 'returns the time provided in the requested time zones' do
formatted = JSON.parse(format_zones_json(zones, '10:30', frozen_time))['items']
expect(formatted.length).to eq(3)
expect(formatted.length).to eq(4)
expect(formatted[0]['title']).to match(/^09:30 - .*$/)
expect(formatted[1]['title']).to match(/^20:30 - .*$/)
expect(formatted[2]['title']).to match(/^05:30 - .*$/)
expect(formatted[3]['title']).to match(/^20:30 - .*$/)
end
it 'filters by title if provided a string instead of a time' do
......@@ -82,4 +86,16 @@ describe 'format_zones_json' do
formatted = JSON.parse(format_zones_json(zones, '1:30PM first', frozen_time))['items']
expect(formatted[0]['title']).to match(/^12:30 - First$/)
end
it 'can have multiple filters' do
formatted = JSON.parse(format_zones_json(zones, 'ir 10 FI', frozen_time))['items']
expect(formatted.length).to eq(1)
expect(formatted[0]['title']).to match(/^09:00 - First$/)
end
it 'selects the last number as the time value' do
formatted = JSON.parse(format_zones_json(zones, '10 10', frozen_time))['items']
expect(formatted.length).to eq(1)
expect(formatted[0]['title']).to match(/^20:00 - 10$/)
end
end