Skip to content

Commit 7787221

Browse files
committed
Adding support for explicit TZ conversion requests
1 parent 504169b commit 7787221

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

core/args.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
202202
// Handle direct flags
203203
if requestTime != "" {
204204
// Parse time
205-
rTime, err := parseRequestTime(startConfig, requestTime)
205+
rTime, err := ParseRequestTime(startConfig, requestTime)
206206
if err != nil {
207207
return startConfig, rt, changed, err
208208
}
@@ -216,7 +216,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
216216
// If last argument is a time, parse it
217217
if len(lastArg) > 0 && lastArg[0] >= '0' && lastArg[0] <= '9' {
218218
// Parse time
219-
rTime, err := parseRequestTime(startConfig, lastArg)
219+
rTime, err := ParseRequestTime(startConfig, lastArg)
220220
if err != nil {
221221
return startConfig, rt, changed, err
222222
}
@@ -279,36 +279,46 @@ type inputTimeFormat struct {
279279
TZInfo bool
280280
}
281281

282-
// parseRequestTime parses a requested time in various formats. Furthermore, it
282+
// ParseRequestTime parses a requested time in various formats. Furthermore, it
283283
// reads an optional timezone index and uses its timezone instead of local.
284-
func parseRequestTime(config Config, t string) (time.Time, error) {
284+
func ParseRequestTime(config Config, t string) (time.Time, error) {
285285
tzSeparator := "@"
286286
tz := time.Local
287287
// Check whether a different time zone than the local one was specified.
288288
if strings.Contains(t, tzSeparator) {
289289
// Split time and timezone
290290
parts := strings.Split(t, tzSeparator)
291291
if len(parts) != 2 {
292-
return time.Time{}, fmt.Errorf("invalid time format: %s (should be <timezone-index>/<time>)", t)
292+
return time.Time{}, fmt.Errorf("invalid time format: %s (should be <time>@<timezone-index> or <time>@<timezone-identifier>)", t)
293293
}
294-
// Parse timezone index
295-
tzIndex, err := strconv.Atoi(parts[1])
296-
if err != nil {
297-
return time.Time{}, fmt.Errorf("invalid time format: %s (should be <timezone-index>/<time>)", t)
298-
}
299-
if tzIndex < 0 || tzIndex > len(config.Timezones) {
300-
return time.Time{}, fmt.Errorf("invalid time format: %s (timezone-index out of range)", t)
301-
}
302-
t = parts[0]
303-
// Get timezone at index (offset by one to account for 0 as local timezone)
304-
if tzIndex > 0 {
305-
tz, err = time.LoadLocation(config.Timezones[tzIndex-1].TZ)
294+
if checkTimezoneLocation(parts[1]) {
295+
// >>> Handle the explicitly given timezone
296+
explicitTZ, err := time.LoadLocation(parts[1])
297+
if err == nil {
298+
// Use explicit timezone
299+
tz = explicitTZ
300+
}
301+
} else {
302+
// >>> Handle the timezone index referring to the configured TZs
303+
tzIndex, err := strconv.Atoi(parts[1])
306304
if err != nil {
307-
return time.Time{}, fmt.Errorf("invalid timezone: %s (given index %d)",
308-
config.Timezones[tzIndex-1].TZ,
309-
tzIndex)
305+
return time.Time{}, fmt.Errorf("invalid time format: %s (should be <time>@<timezone-index> or <time>@<timezone-identifier>)", t)
306+
}
307+
if tzIndex < 0 || tzIndex > len(config.Timezones) {
308+
return time.Time{}, fmt.Errorf("invalid time format: %s (timezone-index out of range)", t)
309+
}
310+
// Get timezone at index (offset by one to account for 0 as local timezone)
311+
if tzIndex > 0 {
312+
tz, err = time.LoadLocation(config.Timezones[tzIndex-1].TZ)
313+
if err != nil {
314+
return time.Time{}, fmt.Errorf("invalid timezone: %s (given index %d)",
315+
config.Timezones[tzIndex-1].TZ,
316+
tzIndex)
317+
}
310318
}
311319
}
320+
// Keep time element
321+
t = parts[0]
312322
}
313323
// Parse time
314324
rt, err := parseTime(t, tz)

0 commit comments

Comments
 (0)