Documentation ¶
Overview ¶
Package yy converts incomplete dates to time.Time
Incomplete dates are: - with missing year digits - with missing year and month - with missing year,month and day
Conversion is done by finding nearest valid date to reference date. Date validity is according std time package, for example no leap seconds.
Motivation for this package is large number of financial protocols and file formats, where abbreviated dates are transmitted in relation to transmitted date. (For example expiration date printed on credit cards have 2 year digits and month: YY/MM)
Supported date formats are:
YYY-MM-DD find X such that XYYY-MM-DD is nearest valid date YY-MM-DD find XX such that XXYY-MM-DD is nearest valid date Y-MM-DD find XXX such that XXXY-MM-DD is nearest valid date MM-DD find XXXX such that XXXX-MM-DD is nearest valid date DD find XXXX-ZZ such that XXXX-ZZ-DD is nearest valid date YYYY-MM return YYYY-MM-01 YYY-MM find X such that XYYY-MM-01 is nearest valid date YY-MM find XX such that XXYY-MM-01 is nearest valid date Y-MM find XXX such that XXXY-MM-01 is nearest valid date MM find XXXX such that XXXX-MM-01 is nearest valid date YYYY-JJJ year+julian day JJJ=1..365/6 YYY-JJJ find X such that XYYY-JJJ is nearest valid date YY-JJJ find XX such that XXYY-JJJ is nearest valid date Y-JJJ find XXX such that XXXY-JJJ is nearest valid date JJJ find XXXX such that XXXX-JJJ is nearest valid date +/-RRR RRR days after/before today YYYY-MM-DD full date YYYY return YYYY-01-01 YYY find X such that XYYY-01-01 is nearest valid date YY find XX such that XXYY-01-01 is nearest valid date Y find XXX such that XXXY-01-01 is nearest valid date "nothing" return reference date
Above, 'nearest valid date' means nearest to reference date.
Additionally, time components can be specified, but they don't participate in finding nearest date. If they are missing, hour, minute, second and fraction defaults to 0, location is copied from reference time.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Convert ¶
Convert IDate to time.Time. rt is reference time. All missing time parts defaults to 0. All missing date parts(day & month), not subject to finding, defaults to 1. Missing location defaults to coping location from reference time. If no any date component present, converts to reference date.
Example ¶
package main import ( "fmt" "time" "github.com/djadala/yy" ) func main() { var d yy.IDate var ref = time.Date(2013, time.June, 10, 23, 1, 2, 3, time.UTC) d.Mo.SetI(2) d.D.SetI(29) r, err := yy.Convert(ref, &d) if err != nil { panic(err) } fmt.Println(r) // 29 Feb in 2013 is 2012-02-29 }
Output: 2012-02-29 00:00:00 +0000 UTC
Example (R) ¶
package main import ( "fmt" "time" "github.com/djadala/yy" ) func main() { var d yy.IDate var ref = time.Date(2013, time.June, 10, 23, 1, 2, 3, time.UTC) err := d.R.Set([]byte("-22")) if err != nil { panic(err) } r, err := yy.Convert(ref, &d) if err != nil { panic(err) } fmt.Println(r) ///// err = d.R.Set([]byte("22")) if err != nil { panic(err) } r, err = yy.Convert(ref, &d) if err != nil { panic(err) } fmt.Println(r) ///// err = d.R.Set([]byte("+23")) if err != nil { panic(err) } r, err = yy.Convert(ref, &d) if err != nil { panic(err) } fmt.Println(r) }
Output: 2013-05-19 00:00:00 +0000 UTC 2013-07-02 00:00:00 +0000 UTC 2013-07-03 00:00:00 +0000 UTC
func FromFormat ¶
FromFormat converts date according to format to time.Time date & format are treated as strings.
format define date, at positions of chars 'Y,M,D,J,h,m,s,f,L,R' in format, are expected symbols of 'year,month,day,julian day,hour,minute,second,fraction,timezone,relative days' in date. All other chars in format are ignored, corresponding positions in date also are ignored.
Accepted patterns are:
Y `\d{1,4}` year, number of 'Y's is equal to number of year digits M `\d{2}` month D `\d{2}` day J `\d{3}` julian day h `\d{2}` hour m `\d{2}` minute s `\d{2}` seconds f `\d{1,9}` fraction L `[+-](\d\d):?(\d\d)` timezone offset or `.+` timezone name Special names 'l' & 'z' are Local & UTC zones R `[+-]?\d+` relative days
rt are reference time.
Example ¶
package main import ( "fmt" "time" "github.com/djadala/yy" ) func main() { var ref = time.Date(2013, time.June, 10, 23, 1, 2, 3, time.UTC) r, err := yy.FromFormat([]byte("99-123"), []byte("YY-JJJ"), ref) if err != nil { panic(err) } fmt.Println(r) // year 99 julian day 123 in 2013 is 1999-05-03 }
Output: 1999-05-03 00:00:00 +0000 UTC
Types ¶
type Int ¶
type Int struct {
// contains filtered or unexported fields
}
Int indicate if various parts of date (day,month,hour,min,sec) present/absent in incomplete date
type Loc ¶
type Loc struct {
// contains filtered or unexported fields
}
Loc indicate if timezone present/absent in incomplete date
type Tm ¶
Tm helps to record date components && check date validity
func (*Tm) FromValues ¶
FromValues sets fields in Tm from separated values