Documentation
¶
Index ¶
- Constants
- Variables
- func AlmostMidnight(year int, month time.Month, day int, loc *time.Location) time.Time
- func Midnight(year int, month time.Month, day int, loc *time.Location) time.Time
- func ParseDate(input string) (year int, month time.Month, day int, err error)
- func ParseDateEnd(input string) (time.Time, error)
- func ParseDateEndIn(input string, loc *time.Location) (time.Time, error)
- func ParseDateStart(input string) (time.Time, error)
- func ParseDateStartIn(input string, loc *time.Location) (time.Time, error)
- func ParseDateTime(input string) (time.Time, error)
- func ParseMonthDay(input string) (time.Month, int, error)
- func ParseMonthDayEnd(input string, year int) (time.Time, error)
- func ParseMonthDayEndIn(input string, year int, loc *time.Location) (time.Time, error)
- func ParseMonthDayStart(input string, year int) (time.Time, error)
- func ParseMonthDayStartIn(input string, year int, loc *time.Location) (time.Time, error)
- func ParseWeek(input string) (year int, week int, err error)
- func ParseWeekDay(input string) (year int, weekNum int, day int, err error)
- func ParseWeekDayEnd(input string) (time.Time, error)
- func ParseWeekDayEndIn(input string, loc *time.Location) (time.Time, error)
- func ParseWeekDayStart(input string) (time.Time, error)
- func ParseWeekDayStartIn(input string, loc *time.Location) (time.Time, error)
- func ParseWeekEnd(input string) (time.Time, error)
- func ParseWeekEndIn(input string, loc *time.Location) (time.Time, error)
- func ParseWeekStart(input string) (time.Time, error)
- func ParseWeekStartIn(input string, loc *time.Location) (time.Time, error)
- func ParseYearMonth(input string) (int, time.Month, error)
- func ParseYearMonthEnd(input string) (time.Time, error)
- func ParseYearMonthEndIn(input string, loc *time.Location) (time.Time, error)
- func ParseYearMonthStart(input string) (time.Time, error)
- func ParseYearMonthStartIn(input string, loc *time.Location) (time.Time, error)
Examples ¶
Constants ¶
const ZeroMonth = time.Month(0)
ZeroMonth is our 'no value' month that we return when the operation fails.
Variables ¶
var ZeroTime = time.Time{}
ZeroTime is our 'no value' time that we return when the operation fails.
Functions ¶
func AlmostMidnight ¶
AlmostMidnight creates a date/time instance in the given time zone that is exactly 11:59:59pm on the specified date.
func Midnight ¶
Midnight creates a date/time instance in the given time zone that is exactly midnight on the specified date.
func ParseDate ¶
ParseDate accepts an ISO-formatted year-month-day string (e.g. "2019-05-22") and returns the year/month/day it represents.
Example ¶
package main
import (
"fmt"
"github.com/robsignorelli/isodates"
)
func main() {
year, month, day, err := isodates.ParseDate("2019-02-24")
if err != nil {
fmt.Printf("oops: %v\n", err)
}
fmt.Printf("Year=%d Month=%d Day=%d", year, month, day)
}
Output: Year=2019 Month=2 Day=24
func ParseDateEnd ¶
ParseDateEnd accepts an ISO-formatted year-month-day string (e.g. "2019-05-22") and returns the given date set to the last nanosecond of 11:59pm in UTC.
func ParseDateEndIn ¶
ParseDateEndIn accepts an ISO-formatted year-month-day string (e.g. "2019-05-22") and returns the given date set to the last nanosecond of 11:59pm in the specified location.
func ParseDateStart ¶
ParseDateStart accepts an ISO-formatted year-month-day string (e.g. "2019-05-22") and returns the given date set to exactly midnight in UTC.
func ParseDateStartIn ¶
ParseDateStartIn accepts an ISO-formatted year-month-day string (e.g. "2019-05-22") and returns the given date set to exactly midnight in the specified location.
func ParseDateTime ¶
ParseDateTime accepts an ISO-formatted date/time string (e.g. "2019-05-22T12:33:53.045Z") and returns the exact date and time that it represents.
Example ¶
package main
import (
"fmt"
"github.com/robsignorelli/isodates"
)
func main() {
date, err := isodates.ParseDateTime("2019-02-24T06:44:33Z")
if err != nil {
fmt.Printf("oops: %v\n", err)
}
fmt.Println(date.Format("Jan 2, 2006 3:04PM"))
}
Output: Feb 24, 2019 6:44AM
func ParseMonthDay ¶
ParseMonthDay accepts an ISO-formatted month/day string (e.g. "--04-01" is April, 1) and returns the month and day that it represents.
Example ¶
package main
import (
"fmt"
"github.com/robsignorelli/isodates"
)
func main() {
// Standard usage
month, day, err := isodates.ParseMonthDay("--04-01")
fmt.Println(fmt.Sprintf("%d %d %v", month, day, err == nil))
// Automatic rollover to subsequent months
month, day, err = isodates.ParseMonthDay("--01-34")
fmt.Println(fmt.Sprintf("%d %d %v", month, day, err == nil))
}
Output: 4 1 true 2 3 true
func ParseMonthDayEnd ¶
ParseMonthDayEnd parses the month/day string (e.g. "--12-24") and returns a date/time at 11:59:59pm in the specified year. The resulting timestamp will be in UTC.
func ParseMonthDayEndIn ¶
ParseMonthDayEndIn parses the month/day string (e.g. "--12-24") and returns a date/time at 11:59:59pm in the specified year. The resulting timestamp will be in the specified time zone.
func ParseMonthDayStart ¶
ParseMonthDayStart parses the month/day string (e.g. "--12-24") and returns a date/time at midnight in the specified year. The resulting timestamp will be in UTC.
func ParseMonthDayStartIn ¶
ParseMonthDayStartIn parses the month/day string (e.g. "--12-24") and returns a date/time at midnight in the specified year. The resulting timestamp will be in specified time zone.
func ParseWeek ¶
ParseWeek accepts an ISO-formatted year/week string (e.g. "2019-W04") and returns the year and week number that it represents.
Example ¶
package main
import (
"fmt"
"github.com/robsignorelli/isodates"
)
func main() {
year, weekNumber, err := isodates.ParseWeek("2019-W02")
fmt.Println(fmt.Sprintf("%d %d %v", year, weekNumber, err == nil))
// We don't support months outside of 1-53
year, weekNumber, err = isodates.ParseWeek("2019-W72")
fmt.Println(fmt.Sprintf("%d %d %v", year, weekNumber, err == nil))
}
Output: 2019 2 true 0 0 false
func ParseWeekDay ¶
ParseWeekDay extracts all 3 numeric components from an ISO Week-Day string (e.g. "2019-W02-3").
Example ¶
package main
import (
"fmt"
"github.com/robsignorelli/isodates"
)
func main() {
date, err := isodates.ParseWeekDayStart("2019-W02-2")
if err != nil {
fmt.Printf("oops: %v\n", err)
}
fmt.Println(date.Format("Jan 2, 2006"))
}
Output: Jan 8, 2019
func ParseWeekDayEnd ¶
ParseWeekDayEnd accepts an ISO-formatted year/week/day string (e.g. "2019-W04-3") and returns the exact date that it represents. The resulting date/time will be at 11:59:59pm in UTC.
func ParseWeekDayEndIn ¶
ParseWeekDayEndIn accepts an ISO-formatted year/week/day string (e.g. "2019-W04-3") and returns the exact date that it represents. The resulting date/time will be at 11:59:59pm in the given time zone.
func ParseWeekDayStart ¶
ParseWeekDayStart accepts an ISO-formatted year/week/day string (e.g. "2019-W04-3") and returns the exact date that it represents. The resulting date/time will be at midnight in UTC.
func ParseWeekDayStartIn ¶
ParseWeekDayStartIn accepts an ISO-formatted year/week/day string (e.g. "2019-W04-3") and returns the exact date that it represents. The resulting date/time will be at midnight in the given time zone.
func ParseWeekEnd ¶
ParseWeekEnd returns 11:59:59pm (one nanosecond before midnight) on Sunday of the specified ISO week string. The resulting date/time will be in UTC. If you would like this to be almost-midnight of some local time, use ParseWeekEndIn.
func ParseWeekEndIn ¶
ParseWeekEndIn returns 11:59:59pm (one nanosecond before midnight) on Sunday of the specified ISO week string. This will be in the local time of the specified location.
func ParseWeekStart ¶
ParseWeekStart returns midnight on Monday of the specified ISO week string. The resulting date/time will be in UTC. If you would like this to be midnight of some local time, use ParseWeekStartIn.
func ParseWeekStartIn ¶
ParseWeekStartIn returns midnight on Monday of the specified ISO week string. This will be in the local time of the specified location.
func ParseYearMonth ¶
ParseYearMonth accepts an ISO string such as "2019-04" and returns the individual date components for the year and month (e.g. 2019 and time.April). We also support the variant where you can prefix the year with either "+" or "-".
Example ¶
package main
import (
"fmt"
"github.com/robsignorelli/isodates"
)
func main() {
year, month, err := isodates.ParseYearMonth("2019-01")
fmt.Println(fmt.Sprintf("%d %d %v", year, month, err == nil))
}
Output: 2019 1 true
func ParseYearMonthEnd ¶
ParseYearMonthEnd returns the last day of the year/month for the parsed input. The resulting date will be at 11:59:59pm in UTC.
func ParseYearMonthEndIn ¶
ParseYearMonthEndIn returns the last day of the year/month for the parsed input. The resulting date will be at 11:59:59pm in the specified time zone.
func ParseYearMonthStart ¶
ParseYearMonthStart returns the first day of the year/month for the parsed input. The resulting date will be at midnight in UTC.
Types ¶
This section is empty.