arrow

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 3 Imported by: 3

README

Arrow: Go Date Parsing

Build Status GoDoc

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.

Installation

Go get it:

go get github.com/bmuller/arrow

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/bmuller/arrow"
)

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"))
}

Running Tests

To run tests:

go test

Reporting Issues

Please report all issues on github.

Documentation

Overview

Package arrow provides C-style date formating and parsing, along with other date goodies.

See the github project page at http://github.com/bmuller/arrow for more info.

Index

Constants

View Source
const (
	Nanosecond  time.Duration = 1
	Microsecond               = 1000 * Nanosecond
	Millisecond               = 1000 * Microsecond
	Second                    = 1000 * Millisecond
	Minute                    = 60 * Second
	Hour                      = 60 * Minute
	Day                       = 24 * Hour
	Week                      = 7 * Day
)

Like time's constants, but with Day and Week

Variables

This section is empty.

Functions

func SleepUntil

func SleepUntil(t Arrow)

Types

type Arrow

type Arrow struct {
	time.Time
}

func CParse

func CParse(layout, value string) (Arrow, error)

Parse the time using the same format string types as strftime See http://man7.org/linux/man-pages/man3/strftime.3.html for more info.

func CParseInLocation

func CParseInLocation(layout, value string, loc *time.Location) (Arrow, error)

Parse the time using the same format string types as strftime, within the given location. See http://man7.org/linux/man-pages/man3/strftime.3.html for more info.

func CParseInStringLocation

func CParseInStringLocation(layout, value, timezone string) (Arrow, error)

Parse the time using the same format string types as strftime, within the given location (string value for timezone). See http://man7.org/linux/man-pages/man3/strftime.3.html for more info.

func Epoch

func Epoch() Arrow

func InTimezone

func InTimezone(timezone string) Arrow

Get the current time in the given timezone. The timezone parameter should correspond to a file in the IANA Time Zone database, such as "America/New_York". "UTC" and "Local" are also acceptable. If the timezone given isn't valid, then no change to the timezone is made.

func New

func New(t time.Time) Arrow

func NextDay

func NextDay() Arrow

func NextHour

func NextHour() Arrow

func NextMinute

func NextMinute() Arrow

func NextSecond

func NextSecond() Arrow

func Now

func Now() Arrow

func Tomorrow

func Tomorrow() Arrow

func UTC

func UTC() Arrow

func Unix

func Unix(sec int64, nsec int64) Arrow

func Yesterday

func Yesterday() Arrow

func (Arrow) Add

func (a Arrow) Add(d time.Duration) Arrow

func (Arrow) AddDays

func (a Arrow) AddDays(days int) Arrow

func (Arrow) AddDuration

func (a Arrow) AddDuration(duration string) Arrow

Add any duration parseable by time.ParseDuration

func (Arrow) AddDurations

func (a Arrow) AddDurations(durations ...string) Arrow

Add any durations parseable by time.ParseDuration

func (Arrow) AddHours

func (a Arrow) AddHours(hours int) Arrow

func (Arrow) AddMinutes

func (a Arrow) AddMinutes(minutes int) Arrow

func (Arrow) AddMonths added in v1.0.3

func (a Arrow) AddMonths(months int) Arrow

func (Arrow) AddSeconds

func (a Arrow) AddSeconds(seconds int) Arrow

func (Arrow) AddYears added in v1.0.3

func (a Arrow) AddYears(years int) Arrow

func (Arrow) After

func (a Arrow) After(b Arrow) bool

func (Arrow) AtBeginningOfDay

func (a Arrow) AtBeginningOfDay() Arrow

func (Arrow) AtBeginningOfHour

func (a Arrow) AtBeginningOfHour() Arrow

func (Arrow) AtBeginningOfMinute

func (a Arrow) AtBeginningOfMinute() Arrow

func (Arrow) AtBeginningOfMonth

func (a Arrow) AtBeginningOfMonth() Arrow

func (Arrow) AtBeginningOfSecond

func (a Arrow) AtBeginningOfSecond() Arrow

func (Arrow) AtBeginningOfWeek

func (a Arrow) AtBeginningOfWeek() Arrow

func (Arrow) AtBeginningOfYear

func (a Arrow) AtBeginningOfYear() Arrow

func (Arrow) Before

func (a Arrow) Before(b Arrow) bool

func (Arrow) CFormat

func (a Arrow) CFormat(format string) string

Format the time using the same format string types as strftime. See http://man7.org/linux/man-pages/man3/strftime.3.html for more info.

func (Arrow) Equal

func (a Arrow) Equal(b Arrow) bool

func (Arrow) InTimezone

func (a Arrow) InTimezone(timezone string) Arrow

The timezone parameter should correspond to a file in the IANA Time Zone database, such as "America/New_York". "UTC" and "Local" are also acceptable. If the timezone given isn't valid, then no change to the timezone is made.

func (Arrow) Sub

func (a Arrow) Sub(b Arrow) time.Duration

func (Arrow) Tomorrow

func (a Arrow) Tomorrow() Arrow

func (Arrow) UTC

func (a Arrow) UTC() Arrow

func (Arrow) UpTo

func (a Arrow) UpTo(b Arrow, by time.Duration) []Arrow

Return an array of Arrow's from this one up to the given one, by duration. For instance, Now().UpTo(Tomorrow(), Hour) will return an array of Arrow's from now until tomorrow by hour (inclusive of a and b).

func (Arrow) Yesterday

func (a Arrow) Yesterday() Arrow

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL