go-wise / dayrange.go

package main

import (
	"fmt"
	"os"
	"time"
)

const (
	DAY         = 24 * 60 * 60
	TIME_FORMAT = "2006-01-02"
)

/* dayrange returns a channel with days (formatted as YYYY-MM-DD) from startDate to endDate. */
func dayrange(startDate, endDate string) (chan string, error) {
	startTime, err := time.Parse(TIME_FORMAT, startDate)
	if err != nil {
		return nil, err
	}

	endTime, err := time.Parse(TIME_FORMAT, endDate)
	if err != nil {
		return nil, err
	}

	start := startTime.Unix()
	end := endTime.Unix()

	ch := make(chan string)
	go func() {
		for ; start <= end; start += DAY {
			t := time.Unix(start, 0).UTC()
			ch <- t.Format(TIME_FORMAT)
		}
		close(ch)
	}()

	return ch, nil
}

func main() {
	days, err := dayrange("2011-01-01", "2011-02-15")
	if err != nil {
		fmt.Fprintln(os.Stderr, "error: %s", err)
		os.Exit(1)
	}

	for day := range days {
		fmt.Println(day)
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.