-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtg.go
255 lines (243 loc) · 9.07 KB
/
dtg.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// The dtg package parses and prints time.Time in Allied Communication
// Publication (ACP) 121 Date-Time Group (DTG) format. This package is Copyright
// (C) 2022 SA6MWA Michel Blomgren.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package dtg
// github.com/sa6mwa/dtg Copyright (C) 2022 SA6MWA Michel Blomgren.
// Format derived from Allied Communication Publication, ACP 121: COMMUNICATION
// INSTRUCTIONS GENERAL
import (
"errors"
"fmt"
"regexp"
"strings"
"time"
"unicode/utf8"
)
var (
DtgRegexp *regexp.Regexp = regexp.MustCompile(`^([0-9]{2})([0-9]{2})([0-9]{2})([A-Z]{0,1})(JAN|FEB|MAR|APR|MAY|MAJ|JUN|JUL|AUG|SEP|OCT|OKT|NOV|DEC){0,1}([0-9]{2}){0,1}$`)
ErrInvalidDTG error = errors.New("invalid DTG format (minimally ddHHMM to complete ddHHMMZmmmYY)")
ErrInvalidTimeZoneLetter error = errors.New("invalid time zone letter")
ErrInvalidDtgVariadic error = errors.New("invalid DTG slice passed as variadic")
)
const (
_ = iota
dtgSubMatchDay
dtgSubMatchHour
dtgSubMatchMinute
dtgSubMatchTimeZone
dtgSubMatchMonth
dtgSubMatchYear
)
const (
numericTimeZoneLayout string = `-0700`
expandedDtgLayout string = `021504-0700Jan06`
dayLayout string = `02`
hourLayout string = `15`
minuteLayout string = `04`
monthLayout string = `Jan`
yearLayout string = `06`
)
type DTG struct {
time.Time
}
// String returns a NATO ACP 121 Date Time Group of the DTG Time field.
func (dtg DTG) String() string {
_, offset := dtg.Time.Zone()
hours := offset / (60 * 60)
letter := rune('J')
if hours == 0 {
letter = 'Z'
} else if hours >= 1 && hours <= 9 {
letter = 'A'
letter += rune(hours - 1)
} else if hours >= 10 && hours <= 12 {
letter = 'A'
letter += rune(hours)
} else if hours >= -12 && hours <= -1 {
letter = 'N'
letter -= rune(hours + 1)
}
return dtg.Time.Format(`021504`) + string(letter) + strings.ToUpper(dtg.Time.Format(`Jan06`))
}
// Parse transforms a NATO (ACP 121 Communication Instructions General) Date
// Time Group into a time.Time object via the DTG struct. The String() function
// of the DTG object reproduces a full Date Time Group from the time.Time object.
func Parse(dtgString string) (dtg DTG, err error) {
dtgString = strings.ToUpper(strings.TrimSpace(dtgString))
matches := DtgRegexp.FindAllStringSubmatch(dtgString, 1)
if len(matches) != 1 || len(matches[0]) != 7 {
return dtg, ErrInvalidDTG
}
match := matches[0]
var numericTimeZone *time.Location
numericTimeZone, err = GetNumericTimeZone(match[dtgSubMatchTimeZone], match[dtgSubMatchDay], match[dtgSubMatchHour], match[dtgSubMatchMinute], match[dtgSubMatchMonth], match[dtgSubMatchYear])
if err != nil {
return dtg, err
}
if utf8.RuneCountInString(match[dtgSubMatchMonth]) < 3 {
match[dtgSubMatchMonth] = strings.ToUpper(time.Now().In(numericTimeZone).Format(monthLayout))
}
if utf8.RuneCountInString(match[dtgSubMatchYear]) < 2 {
match[dtgSubMatchYear] = time.Now().In(numericTimeZone).Format(yearLayout)
}
expandedDtg := match[dtgSubMatchDay] + match[dtgSubMatchHour] +
match[dtgSubMatchMinute] + numericTimeZone.String() +
match[dtgSubMatchMonth] + match[dtgSubMatchYear]
dtg.Time, err = time.ParseInLocation(expandedDtgLayout, expandedDtg, numericTimeZone)
if err != nil {
return dtg, err
}
return dtg, nil
}
// Return a time.Location (and error) with the numeric time zone representation
// (e.g +0100 or -1100) of an ACP 121 time zone letter (A-Z). Name field will be
// the numeric time zone (to parse with -0700). The variadic
// dayHourMinuteMonthYear string slice (optional) will parse this into a
// time.Time to use instead of time.Now() for the local time zone letter J (to
// present a daylight saving - DST - compensated offset). There is a String()
// function in time.Location to extract the numeric time zone as name is
// non-exported.
//
// UTC-12: Y (e.g., Fiji)
// UTC-11: X (American Samoa)
// UTC-10: W (Honolulu, HI)
// UTC-9: V (Juneau, AK)
// UTC-8: U (PST, Los Angeles, CA)
// UTC-7: T (MST, Denver, CO)
// UTC-6: S (CST, Dallas, TX)
// UTC-5: R (EST, New York, NY)
// UTC-4: Q (Halifax, Nova Scotia)
// UTC-3: P (Buenos Aires, Argentina)
// UTC-2: O (Godthab, Greenland)
// UTC-1: N (Azores)
// UTC+-0: Z (Zulu time)
// UTC+1: A (France)
// UTC+2: B (Athens, Greece)
// UTC+3: C (Arab Standard Time, Iraq, Bahrain, Kuwait, Saudi Arabia, Yemen, Qatar)
// UTC+4: D (Used for Moscow, Russia, and Afghanistan, however, Afghanistan is technically +4:30 from UTC)
// UTC+5: E (Pakistan, Kazakhstan, Tajikistan, Uzbekistan, and Turkmenistan)
// UTC+6: F (Bangladesh)
// UTC+7: G (Thailand)
// UTC+8: H (Beijing, China)
// UTC+9: I (Tokyo, Japan)
// UTC+10: K (Brisbane, Australia)
// UTC+11: L (Sydney, Australia)
// UTC+12: M (Wellington, New Zealand)
func GetNumericTimeZone(dtgTimeZoneLetter string, dayHourMinuteMonthYear ...string) (*time.Location, error) {
dtgTimeZoneLetter = strings.ToUpper(strings.TrimSpace(dtgTimeZoneLetter))
if utf8.RuneCountInString(dtgTimeZoneLetter) > 1 {
return nil, ErrInvalidTimeZoneLetter
} else if utf8.RuneCountInString(dtgTimeZoneLetter) < 1 {
dtgTimeZoneLetter = "J"
}
letter := int([]rune(dtgTimeZoneLetter)[0])
if letter < 'A' || letter > 'Z' {
return nil, ErrInvalidTimeZoneLetter
}
if letter == 'J' {
var localTime time.Time
var err error
layout := dayLayout + hourLayout + minuteLayout + monthLayout + yearLayout
location := time.Now().Location()
switch len(dayHourMinuteMonthYear) {
case 0:
localTime = time.Now()
case 1:
remaining := time.Now().Format(hourLayout + minuteLayout + monthLayout + yearLayout)
localTime, err = time.ParseInLocation(layout, dayHourMinuteMonthYear[0]+remaining, location)
if err != nil {
// ddHHMM is mandatory
return nil, err
}
case 2:
remaining := time.Now().Format(minuteLayout + monthLayout + yearLayout)
localTime, err = time.ParseInLocation(layout, strings.Join(dayHourMinuteMonthYear, "")+remaining, location)
if err != nil {
// ddHHMM is mandatory
return nil, err
}
case 3:
remaining := time.Now().Format(monthLayout + yearLayout)
localTime, err = time.ParseInLocation(layout, strings.Join(dayHourMinuteMonthYear, "")+remaining, location)
if err != nil {
// ddHHMM is mandatory
return nil, err
}
case 4:
if utf8.RuneCountInString(dayHourMinuteMonthYear[3]) < 3 {
remaining := time.Now().Format(monthLayout + yearLayout)
localTime, err = time.ParseInLocation(layout, strings.Join(dayHourMinuteMonthYear[:3], "")+remaining, location)
if err != nil {
return nil, err
}
} else {
remaining := time.Now().Format(yearLayout)
localTime, err = time.ParseInLocation(layout, strings.Join(dayHourMinuteMonthYear, "")+remaining, location)
if err != nil {
return nil, err
}
}
case 5:
m := dayHourMinuteMonthYear[3]
y := dayHourMinuteMonthYear[4]
if utf8.RuneCountInString(m) < 3 {
m = time.Now().Format(monthLayout)
}
if utf8.RuneCountInString(y) < 2 {
y = time.Now().Format(yearLayout)
}
localTime, err = time.ParseInLocation(layout, strings.Join(dayHourMinuteMonthYear[:3], "")+m+y, location)
if err != nil {
return nil, err
}
default:
return nil, ErrInvalidDtgVariadic
}
_, offset := localTime.Zone()
return time.FixedZone(localTime.Format(numericTimeZoneLayout), offset), nil
}
var hours int = 0
if letter == 'Z' {
// Zulu time.
hours = 0
} else if letter >= 'A' && letter <= 'I' {
// A to I are positive, A starts at +1.
hours = 1 + (letter - 'A')
} else if letter >= 'K' && letter <= 'M' {
// K, L, M are also positive, K starts at +10.
hours = 10 + (letter - 'K')
} else if letter >= 'N' && letter <= 'Y' {
// N to Y are negative, N starts at -1.
hours = -1 - (letter - 'N')
}
/* // Will never reach this...
else {
return nil, ErrInvalidTimeZoneLetter
}
*/
return time.FixedZone(fmt.Sprintf("%+03d00", hours), hours*3600), nil
}
// Validate attempts to parse the DTG string, discards the DTG object and
// returns error if parsing failed (invalid DTG) or nil (valid DTG).
func Validate(dtgString string) error {
_, err := Parse(dtgString)
return err
}