123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package main
- import "testing"
- func TestYMD(t *testing.T) {
- table := map[YMD][5]int{
- YMD(20220127): {2022, 1, 27, 20220101, 20220131},
- YMD(22000220): {2200, 2, 20, 22000201, 22000228}, // Not a leap year
- YMD(20200217): {2020, 2, 17, 20200201, 20200229}, // 2020 Leap year
- YMD(19991031): {1999, 10, 31, 19991001, 19991031},
- YMD(19900228): {1990, 2, 28, 19900201, 19900228},
- YMD(20381231): {2038, 12, 31, 20381201, 20381231},
- }
- for d, ts := range table {
- newDate := NewYMD(ts[0], ts[1], ts[2])
- if newDate != d {
- t.Errorf("NewDBDate %v, got %v expected %v\n",
- ts, newDate, d)
- }
- year, month, day := newDate.YMD()
- if (year != ts[0]) || (month != ts[1]) || (day != ts[2]) {
- t.Errorf("YMD %v, got %d,%d,%d expected %v\n",
- newDate, year, month, day, ts)
- }
- // convert DBDate to time.Time
- newTime := ToTime(d)
- // convert time.Time to DBDate
- dateFromTime := NewYMDFromTime(newTime)
- if newTime.Year() != ts[0] {
- t.Errorf("DBDateTime %v, Year got %d expected %d\n", d, newTime.Year(), ts[0])
- }
- if int(newTime.Month()) != ts[1] {
- t.Errorf("DBDateTime %v, Month got %d expected %d\n", d, newTime.Month(), ts[1])
- }
- if newTime.Day() != ts[2] {
- t.Errorf("DBDateTime %v, Day got %d expected %d\n", d, newTime.Day(), ts[2])
- }
- if newTime.Year() != newDate.Year() {
- t.Errorf("Year %v, got %d and %d\n",
- d, newTime.Year(), newDate.Year())
- }
- if int(newTime.Month()) != newDate.Month() {
- t.Errorf("Month %v, got %d and %d\n",
- d, newTime.Month(), newDate.Month())
- }
- if newTime.Day() != newDate.Day() {
- t.Errorf("Year %v, got %d and %d\n",
- d, newTime.Day(), newDate.Day())
- }
- if dateFromTime.Year() != newDate.Year() {
- t.Errorf("DFT Year %v, got %d and %d\n",
- d, dateFromTime.Year(), newDate.Year())
- }
- if dateFromTime.Month() != newDate.Month() {
- t.Errorf("DFT Month %v, got %d and %d\n",
- d, dateFromTime.Month(), newDate.Month())
- }
- if dateFromTime.Day() != newDate.Day() {
- t.Errorf("DFT Year %v, got %d and %d\n",
- d, dateFromTime.Day(), newDate.Day())
- }
- newDate.First()
- if int(newDate) != ts[3] {
- t.Errorf("First %v, got %d, expected %d\n",
- d, newDate, ts[3])
- }
- newDate.Last()
- if int(newDate) != ts[4] {
- t.Errorf("Last %v, got %d, expected %d\n",
- d, newDate, ts[4])
- }
- newYM := newDate.YM()
- if int(newYM) != int(newDate)/100 {
- t.Errorf("YM got %d, expected %d\n", newYM, int(newDate)/100)
- }
- backToDate := newYM.YMD()
- newDate.First()
- if newDate != backToDate {
- t.Errorf("YM to YMD got %d, expected %d\n", backToDate, newDate)
- }
- }
- }
- func TestYM(t *testing.T) {
- table := map[YM][2]int{
- YM(202201): {2022, 1},
- YM(220002): {2200, 2},
- YM(202002): {2020, 2},
- YM(199910): {1999, 10},
- YM(199002): {1990, 2},
- YM(203812): {2038, 12},
- }
- for d, ts := range table {
- if d.Year() != ts[0] {
- t.Errorf("Year got %d, expected %d\n", d.Year(), ts[0])
- }
- if d.Month() != ts[1] {
- t.Errorf("Month got %d, expected %d\n", d.Month(), ts[1])
- }
- newYM := NewYM(ts[0], ts[1])
- if newYM != d {
- t.Errorf("NewYM got %d, expected %d\n", newYM, d)
- }
- }
- }
|