Date interaction for Golang, with strftime formatting for time
Go
Latest commit 57b2841 Jul 31, 2015 @bmuller added Epoch function
Permalink
Failed to load latest commit information.
lib added Epoch function Jul 31, 2015
.travis.yml first commit Jun 3, 2015
LICENSE updated readme Jun 3, 2015
README.md I just learned how to spell iterate Jun 21, 2015

README.md

Arrow: Go Date Parsing

GoDoc Build Status

Arrow provides a C style format based parsing in Golang (among other helpful date/time functions).

Time flies like an arrow; fruit flies like a banana.

Strftime Compatability

The problem with formatting times in Golang is that the format string you give is based on re-formatting a single date that is a pain to remember (I know it's 1/2 3:04:05 2006 -0700, but I'm lazy). Most languages based on C (Python, etc) use a string formatting based on strftime, which is what I think most people are familiar with.

So here's strftime compatability for Golang with CFormat and CParse:

package main

import (
    "fmt"

    "github.com/voxmedia/arrow/lib"
)

func main() {
     // formatting
     fmt.Println("Current date: ", arrow.Now().CFormat("%Y-%m-%d %H:%M"))

     // parsing
     parsed, _ := arrow.CParse("%Y-%m-%d", "2015-06-03")
     fmt.Println("Some other date: ", parsed)
}

Additional Fun

You can also utilize helpful functions to get things like the beginning of the minute, hour, day, week, month, and year.

day := arrow.Now().AtBeginningOfWeek().CFormat("%Y-%m-%d")
fmt.Println("First day of week: ", day)

hour := arrow.Now().AtBeginningOfHour().CFormat("%Y-%m-%d %H:%M")
fmt.Println("First second of hour: ", hour)

You can also more easily sleep until specific times:

// sleep until the next minute starts
arrow.SleepUntil(arrow.NextMinute())
fmt.Println(arrow.Now().CFormat("%H:%M:%S"))

There are also helpers to get today, yesterday, and UTC times:

day := arrow.Yesterday().CFormat("%Y-%m-%d")
fmt.Println("Yesterday: ", day)

dayutc := arrow.UTC().Yesterday().CFormat("%Y-%m-%d %H:%M")
fmt.Println("Yesterday, UTC: ", dayutc)

newyork := arrow.InTimezone("America/New_York").CFormat("%H:%M:%s")
fmt.Println("Time in New York: ", newyork)

And for generating ranges when you need to iterate:

// Print every minute from now until 24 hours from now
for _, a := range arrow.Now().UpTo(arrow.Tomorrow(), arrow.Minute) {
     fmt.Println(a.CFormat("%Y-%m-%d %H:%M:%S"))
}