12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package main
- import "testing"
- func TestDBDate(t *testing.T) {
- table := map[DBDate][5]int{
- DBDate(20220127): {2022, 1, 27, 20220101, 20220131},
- DBDate(22000220): {2200, 2, 20, 22000201, 22000228}, // Not a leap year
- DBDate(20200217): {2020, 2, 17, 20200201, 20200229}, // 2020 Leap year
- DBDate(19991031): {1999, 10, 31, 19991001, 19991031},
- DBDate(19900228): {1990, 2, 28, 19900201, 19900228},
- DBDate(20381231): {2038, 12, 31, 20381201, 20381231},
- }
- for d, ts := range table {
- newDate := NewDBDate(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 := DBDateTime(d)
- // convert time.Time to DBDate
- dateFromTime := ToDBDate(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])
- }
- }
- }
|