date_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. date = TimeFromExcelTime(401769, false)
  48. c.Assert(date, Equals, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC))
  49. }
  50. func (d *DateSuite) TestTimeFromExcelTimeWithFractionalPart(c *C) {
  51. date := TimeFromExcelTime(0.114583333333333, false)
  52. c.Assert(date.Round(time.Second), Equals, time.Date(1899, 12, 30, 2, 45, 0, 0, time.UTC))
  53. date = TimeFromExcelTime(60.1145833333333, false)
  54. c.Assert(date.Round(time.Second), Equals, time.Date(1900, 2, 28, 2, 45, 0, 0, time.UTC))
  55. date = TimeFromExcelTime(61.3986111111111, false)
  56. c.Assert(date.Round(time.Second), Equals, time.Date(1900, 3, 1, 9, 34, 0, 0, time.UTC))
  57. date = TimeFromExcelTime(37947.75, false)
  58. c.Assert(date.Round(time.Second), Equals, time.Date(2003, 11, 22, 18, 0, 0, 0, time.UTC))
  59. date = TimeFromExcelTime(41275.1145833333, false)
  60. c.Assert(date.Round(time.Second), Equals, time.Date(2013, 1, 1, 2, 45, 0, 0, time.UTC))
  61. }
  62. func (d *DateSuite) TestTimeFromExcelTimeWith1904Offest(c *C) {
  63. date1904Offset := TimeFromExcelTime(39813.0, true)
  64. c.Assert(date1904Offset, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
  65. }