yy

package module
v0.0.0-...-edf31d9 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2017 License: MIT Imports: 3 Imported by: 1

README

yy

Package yy converts incomplete dates to time.Time

Conversion is done by finding nearest valid date to reference date.

Incomplete dates are:

  • with missing year digits
  • with missing year and month
  • with missing year,month and day

Date validity is according std time package, for example no leap seconds

GoDoc

examples:

package main

import (
	"fmt"
	"github.com/djadala/yy"
	"time"
)

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
}

package main

import (
	"fmt"
	"github.com/djadala/yy"
	"time"
)

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
}

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

func Convert(rt time.Time, p *IDate) (time.Time, error)

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

func FromFormat(date, format []byte, rt time.Time) (time.Time, error)

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 Frac

type Frac struct {
	Int
}

Frac indicate if fractions present/absent in incomplete date

func (*Frac) Set

func (f *Frac) Set(v []byte) error

Set sets Frac from chars in v

type IDate

type IDate struct {
	R, J, Mo, D, H, M, S Int
	F                    Frac
	Y                    Year
	L                    Loc
}

IDate represent components of incomplete date

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

func (*Int) Get

func (i *Int) Get() int

Get returns Int value

func (*Int) Present

func (i *Int) Present() bool

Present returns presence of Int value

func (*Int) Set

func (i *Int) Set(v []byte) error

Set sets Int from chars in v

func (*Int) SetI

func (i *Int) SetI(v int)

SetI sets Int value to v

type Loc

type Loc struct {
	// contains filtered or unexported fields
}

Loc indicate if timezone present/absent in incomplete date

func (*Loc) Get

func (t *Loc) Get() *time.Location

Get Returns Location

func (*Loc) Present

func (t *Loc) Present() bool

Present returns if timezone is present in incomplete date

func (*Loc) Set

func (t *Loc) Set(v []byte) error

Set sets timezone accept `[+-]\d\d:{0,1}\d\d` or `.+`

func (*Loc) SetHHMM

func (t *Loc) SetHHMM(hs, ms []byte) error

SetHHMM sets timezone to hh:mm

func (*Loc) SetName

func (t *Loc) SetName(v []byte) error

SetName sets timezone from name, special names 'z' && 'l' set UTC && Local timezones

func (*Loc) SetS

func (t *Loc) SetS(s int) error

SetS sets timezone from seconds offset

type Tm

type Tm struct {
	Year                      int
	Month                     time.Month
	Day, Hour, Min, Sec, Nsec int
	Loc                       *time.Location
}

Tm helps to record date components && check date validity

func (*Tm) Date

func (t *Tm) Date() time.Time

Date converts Tm to time.Time

func (*Tm) From

func (t *Tm) From(d time.Time)

From sets fields in Tm from time.Time

func (*Tm) FromValues

func (t *Tm) FromValues(y int, mo time.Month, d, h, m, s, f int, l *time.Location)

FromValues sets fields in Tm from separated values

func (*Tm) IsValid

func (t *Tm) IsValid() bool

IsValid returns if Tm represent valid date/time Validity is according to std time package, for example leap seconds are invalid

type Year

type Year struct {
	// contains filtered or unexported fields
}

Year indicate number of year decimal digits in incomplete date (0 digits=no year)

func (*Year) Digits

func (y *Year) Digits() int8

Digits returns number of decimal digits in year

func (*Year) Get

func (y *Year) Get() int

Get Returns (incomplete) year

func (*Year) Set

func (y *Year) Set(v []byte) error

Set sets year from chars in v, number of digits is set to len(v)

func (*Year) SetDI

func (y *Year) SetDI(d, iy int)

SetDI sets digits & year to integers d, iy

Jump to

Keyboard shortcuts

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