Ejemplo n.º 1
0
func TestPredicates(t *testing.T) {
	// The list of case dates must be sorted in ascending order
	cases := []struct {
		year  int
		month time.Month
		day   int
	}{
		{-1234, time.February, 5},
		{0, time.April, 12},
		{1, time.January, 1},
		{1946, time.February, 4},
		{1970, time.January, 1},
		{1976, time.April, 1},
		{1999, time.December, 1},
		{1111111, time.June, 21},
	}
	for i, ci := range cases {
		di := date.New(ci.year, ci.month, ci.day)
		for j, cj := range cases {
			dj := date.New(cj.year, cj.month, cj.day)
			p := di.Equal(dj)
			q := i == j
			if p != q {
				t.Errorf("Equal(%v, %v) == %v, want %v", di, dj, p, q)
			}
			p = di.Before(dj)
			q = i < j
			if p != q {
				t.Errorf("Before(%v, %v) == %v, want %v", di, dj, p, q)
			}
			p = di.After(dj)
			q = i > j
			if p != q {
				t.Errorf("After(%v, %v) == %v, want %v", di, dj, p, q)
			}
			p = di == dj
			q = i == j
			if p != q {
				t.Errorf("Equal(%v, %v) == %v, want %v", di, dj, p, q)
			}
			p = di != dj
			q = i != j
			if p != q {
				t.Errorf("Equal(%v, %v) == %v, want %v", di, dj, p, q)
			}
		}
	}

	// Test IsZero
	zero := date.Date{}
	if !zero.IsZero() {
		t.Errorf("IsZero(%v) == false, want true", zero)
	}
	today := date.Today()
	if today.IsZero() {
		t.Errorf("IsZero(%v) == true, want false", today)
	}
}
Ejemplo n.º 2
0
func TestNew(t *testing.T) {
	cases := []string{
		"0000-01-01T00:00:00+00:00",
		"0001-01-01T00:00:00+00:00",
		"1614-01-01T01:02:03+04:00",
		"1970-01-01T00:00:00+00:00",
		"1815-12-10T05:06:07+00:00",
		"1901-09-10T00:00:00-05:00",
		"1998-09-01T00:00:00-08:00",
		"2000-01-01T00:00:00+00:00",
		"9999-12-31T00:00:00+00:00",
	}
	for _, c := range cases {
		tIn, err := time.Parse(time.RFC3339, c)
		if err != nil {
			t.Errorf("New(%v) cannot parse input: %v", c, err)
			continue
		}
		dOut := date.New(tIn.Year(), tIn.Month(), tIn.Day())
		if !same(dOut, tIn) {
			t.Errorf("New(%v) == %v, want date of %v", c, dOut, tIn)
		}
		dOut = date.NewAt(tIn)
		if !same(dOut, tIn) {
			t.Errorf("NewAt(%v) == %v, want date of %v", c, dOut, tIn)
		}
	}
}
Ejemplo n.º 3
0
func TestArithmetic(t *testing.T) {
	cases := []struct {
		year  int
		month time.Month
		day   int
	}{
		{-1234, time.February, 5},
		{0, time.April, 12},
		{1, time.January, 1},
		{1946, time.February, 4},
		{1970, time.January, 1},
		{1976, time.April, 1},
		{1999, time.December, 1},
		{1111111, time.June, 21},
	}
	offsets := []int{-1000000, -9999, -555, -99, -22, -1, 0, 1, 22, 99, 555, 9999, 1000000}
	for _, c := range cases {
		d := date.New(c.year, c.month, c.day)
		for _, days := range offsets {
			d2 := d.Add(days)
			days2 := d2.Sub(d)
			if days2 != days {
				t.Errorf("AddSub(%v,%v) == %v, want %v", d, days, days2, days)
			}
			d3 := d2.Add(-days)
			if d3 != d {
				t.Errorf("AddNeg(%v,%v) == %v, want %v", d, days, d3, d)
			}
		}
	}
}
Ejemplo n.º 4
0
func ExampleDate_Format() {
	// layout shows by example how the reference time should be represented.
	const layout = "Jan 2, 2006"
	d := date.New(2009, time.November, 10)
	fmt.Println(d.Format(layout))
	// Output: Nov 10, 2009
}
Ejemplo n.º 5
0
func ExampleDate_AddDate() {
	d := date.New(1000, time.January, 1)
	// Months and days do not need to be constrained to [1,12] and [1,365].
	u := d.AddDate(0, 14, -1)
	fmt.Println(u)
	// Output: 1001-02-28
}
Ejemplo n.º 6
0
func ExampleDate_FormatISO() {
	// According to legend, Rome was founded on April 21, 753 BC.
	// Note that with astronomical year numbering, 753 BC becomes -752
	// because 1 BC is actually year 0.
	d := date.New(-752, time.April, 21)
	fmt.Println(d.FormatISO(5))
	// Output: -00752-04-21
}
Ejemplo n.º 7
0
func TestTextMarshalling(t *testing.T) {
	var d date.Date
	cases := []struct {
		value date.Date
		want  string
	}{
		{date.New(-11111, time.February, 3), "-11111-02-03"},
		{date.New(-1, time.December, 31), "-0001-12-31"},
		{date.New(0, time.January, 1), "0000-01-01"},
		{date.New(1, time.January, 1), "0001-01-01"},
		{date.New(1970, time.January, 1), "1970-01-01"},
		{date.New(2012, time.June, 25), "2012-06-25"},
		{date.New(12345, time.June, 7), "+12345-06-07"},
	}
	for _, c := range cases {
		bytes, err := c.value.MarshalText()
		if err != nil {
			t.Errorf("Text(%v) marshal error %v", c, err)
		} else if string(bytes) != c.want {
			t.Errorf("Text(%v) == %v, want %v", c.value, string(bytes), c.want)
		} else {
			err = d.UnmarshalText(bytes)
			if err != nil {
				t.Errorf("Text(%v) unmarshal error %v", c.value, err)
			}
		}
	}
}
Ejemplo n.º 8
0
func TestGobEncoding(t *testing.T) {
	var b bytes.Buffer
	encoder := gob.NewEncoder(&b)
	decoder := gob.NewDecoder(&b)
	cases := []date.Date{
		date.New(-11111, time.February, 3),
		date.New(-1, time.December, 31),
		date.New(0, time.January, 1),
		date.New(1, time.January, 1),
		date.New(1970, time.January, 1),
		date.New(2012, time.June, 25),
		date.New(12345, time.June, 7),
	}
	for _, c := range cases {
		var d date.Date
		err := encoder.Encode(&c)
		if err != nil {
			t.Errorf("Gob(%v) encode error %v", c, err)
		} else {
			err = decoder.Decode(&d)
			if err != nil {
				t.Errorf("Gob(%v) decode error %v", c, err)
			}
		}
	}
}
Ejemplo n.º 9
0
func TestTime(t *testing.T) {
	cases := []struct {
		year  int
		month time.Month
		day   int
	}{
		{-1234, time.February, 5},
		{0, time.April, 12},
		{1, time.January, 1},
		{1946, time.February, 4},
		{1970, time.January, 1},
		{1976, time.April, 1},
		{1999, time.December, 1},
		{1111111, time.June, 21},
	}
	zones := []int{-12, -10, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 8, 12}
	for _, c := range cases {
		d := date.New(c.year, c.month, c.day)
		tUTC := d.UTC()
		if !same(d, tUTC) {
			t.Errorf("TimeUTC(%v) == %v, want date part %v", d, tUTC, d)
		}
		if tUTC.Location() != time.UTC {
			t.Errorf("TimeUTC(%v) == %v, want %v", d, tUTC.Location(), time.UTC)
		}
		tLocal := d.Local()
		if !same(d, tLocal) {
			t.Errorf("TimeLocal(%v) == %v, want date part %v", d, tLocal, d)
		}
		if tLocal.Location() != time.Local {
			t.Errorf("TimeLocal(%v) == %v, want %v", d, tLocal.Location(), time.Local)
		}
		for _, z := range zones {
			location := time.FixedZone("zone", z*60*60)
			tInLoc := d.In(location)
			if !same(d, tInLoc) {
				t.Errorf("TimeIn(%v) == %v, want date part %v", d, tInLoc, d)
			}
			if tInLoc.Location() != location {
				t.Errorf("TimeIn(%v) == %v, want %v", d, tInLoc.Location(), location)
			}
		}
	}
}
Ejemplo n.º 10
0
func ExampleNew() {
	d := date.New(9999, time.December, 31)
	fmt.Printf("The world ends on %s\n", d)
	// Output: The world ends on 9999-12-31
}