Skip to content

Commit

Permalink
location input; midnight fix/improve
Browse files Browse the repository at this point in the history
  • Loading branch information
bgetsug committed Mar 14, 2018
1 parent a6791e4 commit b8d7908
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 32 deletions.
30 changes: 21 additions & 9 deletions cmd/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ var (
min = tNow.Minute()
sec = tNow.Second()
ns = tNow.Nanosecond()
loc = time.Local
)

// convCmd represents the conv command
var convCmd = &cobra.Command{
Use: "conv [time | year] [month] [day] [hour] [minute] [second] [nanosecond]",
Use: "conv [time | year] [month] [day] [hour] [minute] [second] [nanosecond] [location]",
Short: "Convert a time",
Run: conv,
}
Expand All @@ -54,33 +55,44 @@ func init() {

flags := convCmd.PersistentFlags()

flags.StringVarP(&format, "format", "f", "2006-01-02 15:04:05 -0700 MST", "The format (layout) of the input time")
flags.StringVarP(&timezone, "timezone", "z", time.Local.String(), "")
flags.StringVarP(&format, "format", "f", "2006-01-02 15:04:05 -0700 MST", "Time format (layout)")
flags.StringVarP(&timezone, "timezone", "z", time.Local.String(), "The output timezone")
}

func conv(cmd *cobra.Command, args []string) {
loc := location()
loc := location(timezone)

in := parseTimeFromArgs(args)

fmt.Println(in.Format(format), "occurs at", in.In(loc).Format(format))
}

func location() *time.Location {
func location(name string) *time.Location {
var err error

loc := time.Local

if len(timezone) > 0 {
if strings.HasPrefix(timezone, "UTC") {
if len(name) > 0 {
if strings.HasPrefix(name, "UTC") {
for _, offset := range utcOffsetLocations {
if timezone == offset.String() {
if name == offset.String() {
return offset
}
}
}

if loc, err = time.LoadLocation(timezone); err != nil {
switch name {
case "CDT":
name = "CST6CDT"
case "EDT":
name = "EST5EDT"
case "MDT":
name = "MST7MDT"
case "PDT":
name = "PST8PDT"
}

if loc, err = time.LoadLocation(name); err != nil {
panic(err)
}
}
Expand Down
62 changes: 42 additions & 20 deletions cmd/midnight.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,70 @@ import (
)

var (
days int
midnightCount int
)

// midnightCmd represents the midnight command
var midnightCmd = &cobra.Command{
Use: "midnight",
Short: "List upcoming midnights for each UTC offset",
Run: midnight,
Run: midnight,
}

func init() {
RootCmd.AddCommand(midnightCmd)

flags := midnightCmd.PersistentFlags()

flags.IntVarP(&days, "days", "d", 1, "Number of days to generate")
flags.IntVarP(&midnightCount, "count", "n", 1, "Number of midnights to generate")
}

func midnight(cmd *cobra.Command, args []string) {
var midnights []string
t := time.Now().UTC()

for i := 0; i < days; i++ {
t := time.Now().UTC()
fmt.Print("\nNext midnight(s) will occur at:\n\n")
fmt.Println(" Local Time | Zone Time ")
fmt.Println("-----------------------------------------------------------------------")

midnights := make(chan time.Time)
sentMidnights := 0

go midnightsAfter(t, t, midnights, &sentMidnights)

for midnight := range midnights {
fmt.Println(fmt.Sprint(
midnight.In(time.Local), " | ", midnight.Format("2006-01-02 15:04:05 -0700 (MST)"),
))
}
}

func midnightsAfter(initialTime, t time.Time, midnights chan time.Time, sentMidnights *int) {

for _, loc := range utcOffsetLocations {
midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc).AddDate(0, 0, i)
var mns []time.Time

if midnight.After(t) {
midnights = append(midnights, fmt.Sprint(
midnight.In(time.Local), " | ", midnight.Format("2006-01-02 15:04:05 -0700 (MST)"),
))
}
for _, loc := range utcOffsetLocations {
midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)

if midnight.After(initialTime) {
mns = append(mns, midnight)
}
}

sort.Strings(midnights)
sort.Slice(mns, func(i, j int) bool { return mns[i].Before(mns[j]) })

fmt.Print("\nNext midnight will occur at:\n\n")
fmt.Println(" Local Time | Zone Time ")
fmt.Println("-----------------------------------------------------------------------")
for _, midnight := range mns {
if *sentMidnights == midnightCount {
break
}

for _, midnight := range midnights {
fmt.Println(midnight)
midnights <- midnight
*sentMidnights++
}
}

if *sentMidnights < midnightCount {
go midnightsAfter(initialTime, t.AddDate(0, 0, 1), midnights, sentMidnights)
return
}

close(midnights)
}
10 changes: 7 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ func parseTimeFromArgs(args []string) time.Time {
}

if len(args) >= 7 {
ns = mustInt(args[7])
ns = mustInt(args[6])
}

in = time.Date(yr, mo, d, hr, min, sec, ns, time.Local)
if len(args) >= 8 {
loc = location(args[7])
}

in = time.Date(yr, mo, d, hr, min, sec, ns, loc)
}

return in
Expand All @@ -117,4 +121,4 @@ func mustInt(a string) int {
}

return i
}
}

0 comments on commit b8d7908

Please sign in to comment.