/
time.rb
77 lines (65 loc) · 1.8 KB
/
time.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true
require "timex_datalink_client/helpers/char_encoders"
require "timex_datalink_client/helpers/crc_packets_wrapper"
class TimexDatalinkClient
class Protocol3
class Time
include Helpers::CharEncoders
prepend Helpers::CrcPacketsWrapper
CPACKET_TIME = [0x32]
attr_accessor :zone, :is_24h, :date_format, :time
# Create a Time instance.
#
# @param zone [Integer] Time zone number (1 or 2).
# @param is_24h [Boolean] Toggle 24 hour time.
# @param date_format [Integer] Date 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.
def initialize(zone:, is_24h:, date_format:, time:, name: nil)
@zone = zone
@is_24h = is_24h
@date_format = date_format
@time = time
@name = name
end
# Compile packets for a time.
#
# @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
def packets
[
[
CPACKET_TIME,
zone,
time.sec,
time.hour,
time.min,
time.month,
time.day,
year_mod_1900,
name_characters,
wday_from_monday,
is_24h_value,
date_format
].flatten
]
end
private
def name
@name || time.zone.downcase
end
def name_characters
chars_for(name, length: 3, pad: true)
end
def year_mod_1900
time.year % 100
end
def wday_from_monday
(time.wday + 6) % 7
end
def is_24h_value
is_24h ? 2 : 1
end
end
end
end