Skip to content

Commit

Permalink
Merge pull request #223 from synthead/use-symbols-for-protocol-3-and-…
Browse files Browse the repository at this point in the history
…4-Time-date_format-param

Use Time#strftime values for protocol 3 and 4 Time date_format parameter
  • Loading branch information
synthead authored Dec 23, 2022
2 parents 0844a67 + e3b89ff commit 07e6ef8
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 83 deletions.
27 changes: 15 additions & 12 deletions docs/timex_datalink_protocol_3.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,30 @@ TimexDatalinkClient::Protocol3::Time.new(
name: "PDT",
time: Time.new(2022, 9, 5, 3, 39, 44),
is_24h: false,
date_format: TimexDatalinkClient::Protocol3::Time::DATE_FORMATS[:"%_m-%d-%y"]
date_format: "%_m-%d-%y"
)

TimexDatalinkClient::Protocol3::Time.new(
zone: 2,
name: "GMT",
time: Time.new(2022, 9, 5, 11, 39, 44),
is_24h: true,
date_format: TimexDatalinkClient::Protocol3::Time::DATE_FORMATS[:"%_m-%d-%y"]
date_format: "%_m-%d-%y"
)
```

These are the available keys for `DATE_FORMATS`, represented by
[Ruby's `strftime` format](https://apidock.com/ruby/DateTime/strftime):
Here are the valid values for `action_at_end`, represented by
[Time#strftime format](https://apidock.com/ruby/DateTime/strftime), followed by an example of 2023-09-06 in `%Y-%m-%d`
format:

- `:"%_m-%d-%y"`
- `:"%_d-%m-%y"`
- `:"%y-%m-%d"`
- `:"%_m.%d.%y"`
- `:"%_d.%m.%y"`
- `:"%y.%m.%d"`
|`date_format` value|Formatted example|
|---|---|
|`"%_m-%d-%y"`|` 9-06-23`|
|`"%_d-%m-%y"`|` 6-09-23`|
|`"%y-%m-%d"`|`23-09-06`|
|`"%_m.%d.%y"`|` 9.06.23`|
|`"%_d.%m.%y"`|` 6.09.23`|
|`"%y.%m.%d"`|`23.09.06`|

## Alarms

Expand Down Expand Up @@ -264,13 +267,13 @@ models = [
zone: 1,
time: time1,
is_24h: false,
date_format: TimexDatalinkClient::Protocol3::Time::DATE_FORMATS[:"%_m-%d-%y"]
date_format: "%_m-%d-%y"
),
TimexDatalinkClient::Protocol3::Time.new(
zone: 2,
time: time2,
is_24h: true,
date_format: TimexDatalinkClient::Protocol3::Time::DATE_FORMATS[:"%_m-%d-%y"]
date_format: "%_m-%d-%y"
),

TimexDatalinkClient::Protocol3::Alarm.new(
Expand Down
27 changes: 15 additions & 12 deletions docs/timex_datalink_protocol_4.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,30 @@ TimexDatalinkClient::Protocol4::Time.new(
name: "PDT",
time: Time.new(2022, 9, 5, 3, 39, 44),
is_24h: false,
date_format: TimexDatalinkClient::Protocol4::Time::DATE_FORMATS[:"%_m-%d-%y"]
date_format: "%_m-%d-%y"
)

TimexDatalinkClient::Protocol4::Time.new(
zone: 2,
name: "GMT",
time: Time.new(2022, 9, 5, 11, 39, 44),
is_24h: true,
date_format: TimexDatalinkClient::Protocol4::Time::DATE_FORMATS[:"%_m-%d-%y"]
date_format: "%_m-%d-%y"
)
```

These are the available keys for `DATE_FORMATS`, represented by
[Ruby's `strftime` format](https://apidock.com/ruby/DateTime/strftime):
Here are the valid values for `action_at_end`, represented by
[Time#strftime format](https://apidock.com/ruby/DateTime/strftime), followed by an example of 2023-09-06 in `%Y-%m-%d`
format:

- `:"%_m-%d-%y"`
- `:"%_d-%m-%y"`
- `:"%y-%m-%d"`
- `:"%_m.%d.%y"`
- `:"%_d.%m.%y"`
- `:"%y.%m.%d"`
|`date_format` value|Formatted example|
|---|---|
|`"%_m-%d-%y"`|` 9-06-23`|
|`"%_d-%m-%y"`|` 6-09-23`|
|`"%y-%m-%d"`|`23-09-06`|
|`"%_m.%d.%y"`|` 9.06.23`|
|`"%_d.%m.%y"`|` 6.09.23`|
|`"%y.%m.%d"`|`23.09.06`|

## Alarms

Expand Down Expand Up @@ -264,13 +267,13 @@ models = [
zone: 1,
time: time1,
is_24h: false,
date_format: TimexDatalinkClient::Protocol4::Time::DATE_FORMATS[:"%_m-%d-%y"]
date_format: "%_m-%d-%y"
),
TimexDatalinkClient::Protocol4::Time.new(
zone: 2,
time: time2,
is_24h: true,
date_format: TimexDatalinkClient::Protocol4::Time::DATE_FORMATS[:"%_m-%d-%y"]
date_format: "%_m-%d-%y"
),

TimexDatalinkClient::Protocol4::Alarm.new(
Expand Down
23 changes: 14 additions & 9 deletions lib/timex_datalink_client/protocol_3/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class Time

CPACKET_TIME = [0x32]

DATE_FORMATS = {
"%_m-%d-%y": 0,
"%_d-%m-%y": 1,
"%y-%m-%d": 2,
"%_m.%d.%y": 4,
"%_d.%m.%y": 5,
"%y.%m.%d": 6
DATE_FORMAT_MAP = {
"%_m-%d-%y" => 0,
"%_d-%m-%y" => 1,
"%y-%m-%d" => 2,
"%_m.%d.%y" => 4,
"%_d.%m.%y" => 5,
"%y.%m.%d" => 6
}.freeze

attr_accessor :zone, :is_24h, :date_format, :time
Expand All @@ -26,7 +26,8 @@ class Time
#
# @param zone [Integer] Time zone number (1 or 2).
# @param is_24h [Boolean] Toggle 24 hour time.
# @param date_format [Integer] Date format.
# @param date_format ["%_m-%d-%y", "%_d-%m-%y", "%y-%m-%d", "%_m.%d.%y", "%_d.%m.%y", "%y.%m.%d"] Date format
# (represented by Time#strftime format).
# @param time [::Time] Time to set (including time zone).
# @param name [String, nil] Name of time zone (defaults to zone from time; 3 chars max)
# @return [Time] Time instance.
Expand Down Expand Up @@ -55,7 +56,7 @@ def packets
name_characters,
wday_from_monday,
is_24h_value,
date_format
date_format_value
].flatten
]
end
Expand All @@ -81,6 +82,10 @@ def wday_from_monday
def is_24h_value
is_24h ? 2 : 1
end

def date_format_value
DATE_FORMAT_MAP.fetch(date_format)
end
end
end
end
23 changes: 14 additions & 9 deletions lib/timex_datalink_client/protocol_4/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class Time

CPACKET_TIME = [0x32]

DATE_FORMATS = {
"%_m-%d-%y": 0,
"%_d-%m-%y": 1,
"%y-%m-%d": 2,
"%_m.%d.%y": 4,
"%_d.%m.%y": 5,
"%y.%m.%d": 6
DATE_FORMAT_MAP = {
"%_m-%d-%y" => 0,
"%_d-%m-%y" => 1,
"%y-%m-%d" => 2,
"%_m.%d.%y" => 4,
"%_d.%m.%y" => 5,
"%y.%m.%d" => 6
}.freeze

attr_accessor :zone, :is_24h, :date_format, :time
Expand All @@ -26,7 +26,8 @@ class Time
#
# @param zone [Integer] Time zone number (1 or 2).
# @param is_24h [Boolean] Toggle 24 hour time.
# @param date_format [Integer] Date format.
# @param date_format ["%_m-%d-%y", "%_d-%m-%y", "%y-%m-%d", "%_m.%d.%y", "%_d.%m.%y", "%y.%m.%d"] Date format
# (represented by Time#strftime format).
# @param time [::Time] Time to set (including time zone).
# @param name [String, nil] Name of time zone (defaults to zone from time; 3 chars max)
# @return [Time] Time instance.
Expand Down Expand Up @@ -55,7 +56,7 @@ def packets
name_characters,
wday_from_monday,
is_24h_value,
date_format
date_format_value
].flatten
]
end
Expand All @@ -81,6 +82,10 @@ def wday_from_monday
def is_24h_value
is_24h ? 2 : 1
end

def date_format_value
DATE_FORMAT_MAP.fetch(date_format)
end
end
end
end
55 changes: 35 additions & 20 deletions spec/lib/timex_datalink_client/protocol_3/time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
describe TimexDatalinkClient::Protocol3::Time do
let(:zone) { 1 }
let(:is_24h) { false }
let(:date_format) { 0 }
let(:date_format) { "%_m-%d-%y" }
let(:tzinfo) { TZInfo::Timezone.get("US/Pacific") }
let(:time) { tzinfo.local_time(2015, 10, 21, 19, 28, 32) }
let(:name) { nil }
Expand All @@ -22,23 +22,6 @@
)
end

describe "DATE_FORMATS" do
subject(:date_formats) { described_class::DATE_FORMATS }

let(:expected_date_formats) do
{
"%_m-%d-%y": 0,
"%_d-%m-%y": 1,
"%y-%m-%d": 2,
"%_m.%d.%y": 4,
"%_d.%m.%y": 5,
"%y.%m.%d": 6
}
end

it { should eq(expected_date_formats) }
end

describe "#packets", :crc do
subject(:packets) { time_instance.packets }

Expand All @@ -62,14 +45,46 @@
]
end

context "when date_format is 4" do
let(:date_format) { 4 }
context "when date_format is \"%_d-%m-%y\"" do
let(:date_format) { "%_d-%m-%y" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x01]
]
end

context "when date_format is \"%y-%m-%d\"" do
let(:date_format) { "%y-%m-%d" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x02]
]
end

context "when date_format is \"%_m.%d.%y\"" do
let(:date_format) { "%_m.%d.%y" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x04]
]
end

context "when date_format is \"%_d.%m.%y\"" do
let(:date_format) { "%_d.%m.%y" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x05]
]
end

context "when date_format is \"%y.%m.%d\"" do
let(:date_format) { "%y.%m.%d" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x06]
]
end

context "when time is 1997-09-19 19:36:55 NZDT" do
let(:tzinfo) { TZInfo::Timezone.get("Pacific/Auckland") }
let(:time) { tzinfo.local_time(1997, 9, 19, 19, 36, 55) }
Expand Down
55 changes: 35 additions & 20 deletions spec/lib/timex_datalink_client/protocol_4/time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
describe TimexDatalinkClient::Protocol4::Time do
let(:zone) { 1 }
let(:is_24h) { false }
let(:date_format) { 0 }
let(:date_format) { "%_m-%d-%y" }
let(:tzinfo) { TZInfo::Timezone.get("US/Pacific") }
let(:time) { tzinfo.local_time(2015, 10, 21, 19, 28, 32) }
let(:name) { nil }
Expand All @@ -22,23 +22,6 @@
)
end

describe "DATE_FORMATS" do
subject(:date_formats) { described_class::DATE_FORMATS }

let(:expected_date_formats) do
{
"%_m-%d-%y": 0,
"%_d-%m-%y": 1,
"%y-%m-%d": 2,
"%_m.%d.%y": 4,
"%_d.%m.%y": 5,
"%y.%m.%d": 6
}
end

it { should eq(expected_date_formats) }
end

describe "#packets", :crc do
subject(:packets) { time_instance.packets }

Expand All @@ -62,14 +45,46 @@
]
end

context "when date_format is 4" do
let(:date_format) { 4 }
context "when date_format is \"%_d-%m-%y\"" do
let(:date_format) { "%_d-%m-%y" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x01]
]
end

context "when date_format is \"%y-%m-%d\"" do
let(:date_format) { "%y-%m-%d" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x02]
]
end

context "when date_format is \"%_m.%d.%y\"" do
let(:date_format) { "%_m.%d.%y" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x04]
]
end

context "when date_format is \"%_d.%m.%y\"" do
let(:date_format) { "%_d.%m.%y" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x05]
]
end

context "when date_format is \"%y.%m.%d\"" do
let(:date_format) { "%y.%m.%d" }

it_behaves_like "CRC-wrapped packets", [
[0x32, 0x01, 0x20, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x19, 0x0d, 0x1d, 0x02, 0x01, 0x06]
]
end

context "when time is 1997-09-19 19:36:55 NZDT" do
let(:tzinfo) { TZInfo::Timezone.get("Pacific/Auckland") }
let(:time) { tzinfo.local_time(1997, 9, 19, 19, 36, 55) }
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/timex_datalink_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
TimexDatalinkClient::Protocol3::Time.new(
zone: 1,
is_24h: false,
date_format: 0,
date_format: "%_m-%d-%y",
time: TZInfo::Timezone.get("US/Pacific").local_time(2015, 10, 21, 19, 28, 32)
),
TimexDatalinkClient::Protocol3::Eeprom.new(
Expand Down

0 comments on commit 07e6ef8

Please sign in to comment.