Skip to content

Commit

Permalink
Merge pull request #12 from merschformann/merschformann/request-time-…
Browse files Browse the repository at this point in the history
…in-other-tz

Enables requesting specific time in different timezone
  • Loading branch information
merschformann committed Jun 26, 2022
2 parents 95addb2 + 33aabfe commit c0924f9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 24 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ gotz
Show arbitrary time:

```bash
gotz 23
gotz 15
```

![preview](material/screenshot/gotz-23.png)
![preview](material/screenshot/gotz-15.png)

Show arbitrary time using different timezone (index based):

```bash
gotz 15@1
```

![preview](material/screenshot/gotz-15-1.png)

Time can be one of the following formats:

Expand Down
78 changes: 59 additions & 19 deletions core/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -65,7 +66,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,

// Define direct flags
var requestTime string
var t time.Time = time.Time{}
var rt time.Time = time.Time{}
flag.StringVar(
&requestTime,
"time",
Expand All @@ -87,7 +88,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
changed = true
tzs, err := parseTimezones(timezones)
if err != nil {
return startConfig, time.Time{}, changed, err
return startConfig, rt, changed, err
}
startConfig.Timezones = tzs
}
Expand All @@ -96,7 +97,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
startConfig.Style.Symbols = symbols
symbolError := checkSymbolMode(startConfig.Style.Symbols)
if symbolError != nil {
return startConfig, time.Time{}, changed, symbolError
return startConfig, rt, changed, symbolError
}
}
if tics != "" {
Expand All @@ -106,7 +107,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
} else if strings.ToLower(tics) == "false" {
startConfig.Tics = false
} else {
return startConfig, time.Time{}, changed, fmt.Errorf("invalid value for tics: %s", tics)
return startConfig, rt, changed, fmt.Errorf("invalid value for tics: %s", tics)
}
}
if stretch != "" {
Expand All @@ -116,7 +117,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
} else if strings.ToLower(stretch) == "false" {
startConfig.Stretch = false
} else {
return startConfig, time.Time{}, changed, fmt.Errorf("invalid value for stretch: %s", stretch)
return startConfig, rt, changed, fmt.Errorf("invalid value for stretch: %s", stretch)
}
}
if colorize != "" {
Expand All @@ -126,7 +127,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
} else if strings.ToLower(colorize) == "false" {
startConfig.Style.Colorize = false
} else {
return startConfig, time.Time{}, changed, fmt.Errorf("invalid value for colorize: %s", colorize)
return startConfig, rt, changed, fmt.Errorf("invalid value for colorize: %s", colorize)
}
}
if hours12 != "" {
Expand All @@ -136,7 +137,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
} else if strings.ToLower(hours12) == "false" {
startConfig.Hours12 = false
} else {
return startConfig, time.Time{}, changed, fmt.Errorf("invalid value for hours12: %s", hours12)
return startConfig, rt, changed, fmt.Errorf("invalid value for hours12: %s", hours12)
}
}
if live != "" {
Expand All @@ -146,18 +147,18 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
} else if strings.ToLower(live) == "false" {
startConfig.Live = false
} else {
return startConfig, time.Time{}, changed, fmt.Errorf("invalid value for live: %s", live)
return startConfig, rt, changed, fmt.Errorf("invalid value for live: %s", live)
}
}

// Handle direct flags
if requestTime != "" {
// Parse time
rTime, err := parseTime(requestTime)
rTime, err := parseRequestTime(startConfig, requestTime)
if err != nil {
return startConfig, time.Time{}, changed, err
return startConfig, rt, changed, err
}
t = rTime
rt = rTime
}

// Handle last argument as time, if it starts with a digit
Expand All @@ -167,15 +168,15 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
// If last argument is a time, parse it
if len(lastArg) > 0 && lastArg[0] >= '0' && lastArg[0] <= '9' {
// Parse time
rTime, err := parseTime(lastArg)
rTime, err := parseRequestTime(startConfig, lastArg)
if err != nil {
return startConfig, time.Time{}, changed, err
return startConfig, rt, changed, err
}
t = rTime
rt = rTime
}
}

return startConfig, t, changed, nil
return startConfig, rt, changed, nil
}

// parseTimezones parses a comma-separated list of timezones.
Expand Down Expand Up @@ -230,8 +231,47 @@ type inputTimeFormat struct {
TZInfo bool
}

// parseTime parses a time string.
func parseTime(t string) (time.Time, error) {
// parseRequestTime parses a requested time in various formats. Furthermore, it
// reads an optional timezone index and uses its timezone instead of local.
func parseRequestTime(config Config, t string) (time.Time, error) {
tzSeparator := "@"
tz := time.Local
// Check whether a different time zone than the local one was specified.
if strings.Contains(t, tzSeparator) {
// Split time and timezone
parts := strings.Split(t, tzSeparator)
if len(parts) != 2 {
return time.Time{}, fmt.Errorf("invalid time format: %s (should be <timezone-index>/<time>)", t)
}
// Parse timezone index
tzIndex, err := strconv.Atoi(parts[1])
if err != nil {
return time.Time{}, fmt.Errorf("invalid time format: %s (should be <timezone-index>/<time>)", t)
}
if tzIndex < 0 || tzIndex > len(config.Timezones) {
return time.Time{}, fmt.Errorf("invalid time format: %s (timezone-index out of range)", t)
}
t = parts[0]
// Get timezone at index (offset by one to account for 0 as local timezone)
if tzIndex > 0 {
tz, err = time.LoadLocation(config.Timezones[tzIndex-1].TZ)
if err != nil {
return time.Time{}, fmt.Errorf("invalid timezone: %s (given index %d)",
config.Timezones[tzIndex-1].TZ,
tzIndex)
}
}
}
// Parse time
rt, err := parseTime(t, tz)
if err != nil {
return time.Time{}, err
}
return rt, nil
}

// parseTime parses a time string in various formats.
func parseTime(t string, tz *time.Location) (time.Time, error) {
// Try all supported formats
for _, format := range []inputTimeFormat{
{"15", false, false},
Expand All @@ -249,9 +289,9 @@ func parseTime(t string) (time.Time, error) {
n := time.Now()
if !format.TZInfo {
if format.Date {
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, tz)
} else {
t = time.Date(n.Year(), n.Month(), n.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
t = time.Date(n.Year(), n.Month(), n.Day(), t.Hour(), t.Minute(), t.Second(), 0, tz)
}
}
return t, nil
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
}
}
// Parse flags
config, t, changed, err := core.ParseFlags(config, Version)
config, rt, changed, err := core.ParseFlags(config, Version)
if err != nil {
fmt.Println("error parsing flags:", err)
os.Exit(1)
Expand All @@ -45,7 +45,7 @@ func main() {
}
}
// Plot time
err = core.Plot(config, t)
err = core.Plot(config, rt)
if err != nil {
fmt.Println("error plotting time:", err)
os.Exit(1)
Expand Down
Binary file added material/screenshot/gotz-15-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added material/screenshot/gotz-15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed material/screenshot/gotz-23.png
Binary file not shown.
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const Version = "v0.1.4"
const Version = "v0.1.5"

0 comments on commit c0924f9

Please sign in to comment.