date_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package excelize
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. type dateTest struct {
  9. ExcelValue float64
  10. GoValue time.Time
  11. }
  12. var trueExpectedDateList = []dateTest{
  13. {0.0000000000000000, time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)},
  14. {25569.000000000000, time.Unix(0, 0).UTC()},
  15. // Expected values extracted from real spreadsheet
  16. {1.0000000000000000, time.Date(1900, time.January, 1, 0, 0, 0, 0, time.UTC)},
  17. {1.0000115740740740, time.Date(1900, time.January, 1, 0, 0, 1, 0, time.UTC)},
  18. {1.0006944444444446, time.Date(1900, time.January, 1, 0, 1, 0, 0, time.UTC)},
  19. {1.0416666666666667, time.Date(1900, time.January, 1, 1, 0, 0, 0, time.UTC)},
  20. {2.0000000000000000, time.Date(1900, time.January, 2, 0, 0, 0, 0, time.UTC)},
  21. {43269.000000000000, time.Date(2018, time.June, 18, 0, 0, 0, 0, time.UTC)},
  22. {43542.611111111109, time.Date(2019, time.March, 18, 14, 40, 0, 0, time.UTC)},
  23. {401769.00000000000, time.Date(3000, time.January, 1, 0, 0, 0, 0, time.UTC)},
  24. }
  25. var excelTimeInputList = []dateTest{
  26. {0.0, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)},
  27. {60.0, time.Date(1900, 2, 28, 0, 0, 0, 0, time.UTC)},
  28. {61.0, time.Date(1900, 3, 1, 0, 0, 0, 0, time.UTC)},
  29. {41275.0, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC)},
  30. {401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
  31. }
  32. func TestTimeToExcelTime(t *testing.T) {
  33. for i, test := range trueExpectedDateList {
  34. t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
  35. excelTime, err := timeToExcelTime(test.GoValue)
  36. assert.NoError(t, err)
  37. assert.Equalf(t, test.ExcelValue, excelTime,
  38. "Time: %s", test.GoValue.String())
  39. })
  40. }
  41. }
  42. func TestTimeToExcelTime_Timezone(t *testing.T) {
  43. location, err := time.LoadLocation("America/Los_Angeles")
  44. if !assert.NoError(t, err) {
  45. t.FailNow()
  46. }
  47. for i, test := range trueExpectedDateList {
  48. t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
  49. _, err := timeToExcelTime(test.GoValue.In(location))
  50. assert.NoError(t, err)
  51. })
  52. }
  53. }
  54. func TestTimeFromExcelTime(t *testing.T) {
  55. for i, test := range excelTimeInputList {
  56. t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
  57. assert.Equal(t, test.GoValue, timeFromExcelTime(test.ExcelValue, false))
  58. })
  59. }
  60. }
  61. func TestTimeFromExcelTime_1904(t *testing.T) {
  62. _, _ = shiftJulianToNoon(1, -0.6)
  63. timeFromExcelTime(61, true)
  64. timeFromExcelTime(62, true)
  65. }
  66. func TestExcelDateToTime(t *testing.T) {
  67. // Check normal case
  68. for i, test := range excelTimeInputList {
  69. t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
  70. timeValue, err := ExcelDateToTime(test.ExcelValue, false)
  71. assert.Equal(t, test.GoValue, timeValue)
  72. assert.NoError(t, err)
  73. })
  74. }
  75. // Check error case
  76. _, err := ExcelDateToTime(-1, false)
  77. assert.EqualError(t, err, "invalid date value -1.000000, negative values are not supported supported")
  78. }