You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.4KB

  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 xlsx file
  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. func TestTimeToExcelTime(t *testing.T) {
  26. for i, test := range trueExpectedDateList {
  27. t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
  28. excelTime, err := timeToExcelTime(test.GoValue)
  29. assert.NoError(t, err)
  30. assert.Equalf(t, test.ExcelValue, excelTime,
  31. "Time: %s", test.GoValue.String())
  32. })
  33. }
  34. }
  35. func TestTimeToExcelTime_Timezone(t *testing.T) {
  36. location, err := time.LoadLocation("America/Los_Angeles")
  37. if !assert.NoError(t, err) {
  38. t.FailNow()
  39. }
  40. for i, test := range trueExpectedDateList {
  41. t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
  42. _, err := timeToExcelTime(test.GoValue.In(location))
  43. assert.EqualError(t, err, "only UTC time expected")
  44. })
  45. }
  46. }
  47. func TestTimeFromExcelTime(t *testing.T) {
  48. trueExpectedInputList := []dateTest{
  49. {0.0, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)},
  50. {60.0, time.Date(1900, 2, 28, 0, 0, 0, 0, time.UTC)},
  51. {61.0, time.Date(1900, 3, 1, 0, 0, 0, 0, time.UTC)},
  52. {41275.0, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC)},
  53. {401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
  54. }
  55. for i, test := range trueExpectedInputList {
  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. }