date_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package xlsx
  2. import (
  3. . "gopkg.in/check.v1"
  4. "time"
  5. )
  6. type DateSuite struct{}
  7. var _ = Suite(&DateSuite{})
  8. func (d *DateSuite) TestFractionOfADay(c *C) {
  9. var h, m, s, n int
  10. h, m, s, n = fractionOfADay(0)
  11. c.Assert(h, Equals, 0)
  12. c.Assert(m, Equals, 0)
  13. c.Assert(s, Equals, 0)
  14. c.Assert(n, Equals, 0)
  15. h, m, s, n = fractionOfADay(1.0 / 24.0)
  16. c.Assert(h, Equals, 1)
  17. c.Assert(m, Equals, 0)
  18. c.Assert(s, Equals, 0)
  19. c.Assert(n, Equals, 0)
  20. }
  21. func (d *DateSuite) TestJulianDateToGregorianTime(c *C) {
  22. c.Assert(julianDateToGregorianTime(2400000.5, 51544.0),
  23. Equals, time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
  24. c.Assert(julianDateToGregorianTime(2400000.5, 51544.5),
  25. Equals, time.Date(2000, 1, 1, 12, 0, 0, 0, time.UTC))
  26. c.Assert(julianDateToGregorianTime(2400000.5, 51544.245),
  27. Equals, time.Date(2000, 1, 1, 5, 52, 48, 0, time.UTC))
  28. c.Assert(julianDateToGregorianTime(2400000.5, 51544.2456),
  29. Equals, time.Date(2000, 1, 1, 5, 53, 39, 840000000, time.UTC))
  30. /* test rounding: 0.24560789123*24*3600 = 21220.521802272 */
  31. c.Assert(julianDateToGregorianTime(2400000.5, 51544.24560789123),
  32. Equals, time.Date(2000, 1, 1, 5, 53, 40, 521802000, time.UTC))
  33. c.Assert(julianDateToGregorianTime(2400000.5, 51544.1),
  34. Equals, time.Date(2000, 1, 1, 2, 24, 00, 0, time.UTC))
  35. c.Assert(julianDateToGregorianTime(2400000.5, 51544.75),
  36. Equals, time.Date(2000, 1, 1, 18, 0, 0, 0, time.UTC))
  37. }
  38. func (d *DateSuite) TestTimeFromExcelTime(c *C) {
  39. date := TimeFromExcelTime(0, false)
  40. c.Assert(date, Equals, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC))
  41. date = TimeFromExcelTime(60, false)
  42. c.Assert(date, Equals, time.Date(1900, 2, 28, 0, 0, 0, 0, time.UTC))
  43. date = TimeFromExcelTime(61, false)
  44. c.Assert(date, Equals, time.Date(1900, 3, 1, 0, 0, 0, 0, time.UTC))
  45. date = TimeFromExcelTime(41275.0, false)
  46. c.Assert(date, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
  47. }
  48. func (d *DateSuite) TestTimeFromExcelTimeWithFractionalPart(c *C) {
  49. date := TimeFromExcelTime(0.114583333333333, false)
  50. c.Assert(date.Round(time.Second), Equals, time.Date(1899, 12, 30, 2, 45, 0, 0, time.UTC))
  51. date = TimeFromExcelTime(60.1145833333333, false)
  52. c.Assert(date.Round(time.Second), Equals, time.Date(1900, 2, 28, 2, 45, 0, 0, time.UTC))
  53. date = TimeFromExcelTime(61.3986111111111, false)
  54. c.Assert(date.Round(time.Second), Equals, time.Date(1900, 3, 1, 9, 34, 0, 0, time.UTC))
  55. date = TimeFromExcelTime(37947.75, false)
  56. c.Assert(date.Round(time.Second), Equals, time.Date(2003, 11, 22, 18, 0, 0, 0, time.UTC))
  57. date = TimeFromExcelTime(41275.1145833333, false)
  58. c.Assert(date.Round(time.Second), Equals, time.Date(2013, 1, 1, 2, 45, 0, 0, time.UTC))
  59. }
  60. func (d *DateSuite) TestTimeFromExcelTimeWith1904Offest(c *C) {
  61. date1904Offset := TimeFromExcelTime(39813.0, true)
  62. c.Assert(date1904Offset, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
  63. }