Jelajahi Sumber

adding unit test coverage for timezone stuff

Daniel Sont 8 tahun lalu
induk
melakukan
bb9725c7e5
2 mengubah file dengan 49 tambahan dan 1 penghapusan
  1. 1 1
      cell.go
  2. 48 0
      cell_test.go

+ 1 - 1
cell.go

@@ -158,7 +158,7 @@ func (c *Cell) SetDateTime(t time.Time) {
 func (c *Cell) SetDateWithOptions(t time.Time, options DateTimeOptions) {
 	_, offset := t.In(options.Location).Zone()
 	t = time.Unix(t.Unix()+int64(offset), 0)
-	c.SetDateTimeWithFormat(timeToExcelTime(t.In(timeLocationUTC)), options.ExcelTimeFormat)
+	c.SetDateTimeWithFormat(TimeToExcelTime(t.In(timeLocationUTC)), options.ExcelTimeFormat)
 }
 
 func (c *Cell) SetDateTimeWithFormat(n float64, format string) {

+ 48 - 0
cell_test.go

@@ -500,3 +500,51 @@ func (s *CellSuite) TestSetValue(c *C) {
 	cell.SetValue([]string{"test"})
 	c.Assert(cell.Value, Equals, "[test]")
 }
+
+func (s *CellSuite) TestSetDateWithOptions(c *C) {
+	cell := Cell{}
+
+	// time
+	cell.SetDate(time.Unix(0, 0))
+	val, err := cell.Float()
+	c.Assert(err, IsNil)
+	c.Assert(math.Floor(val), Equals, 25569.0)
+
+	// get a specific time's float value, that isn't near zero, to compare against
+	date2016UTC := time.Date(2016, 1, 1, 12, 0, 0, 0, time.UTC)
+	cell.SetDate(date2016UTC)
+	utcVal, err := cell.Float()
+	c.Assert(err, IsNil)
+
+	// test ny timezone
+	nyTZ, err := time.LoadLocation("America/New_York")
+	c.Assert(err, IsNil)
+	date2016NY := time.Date(2016, 1, 1, 12, 0, 0, 0, nyTZ)
+	cell.SetDateWithOptions(date2016UTC, DateTimeOptions{
+		ExcelTimeFormat: "test_format1",
+		Location:        nyTZ,
+	})
+	val, err = cell.Float()
+	c.Assert(err, IsNil)
+	_, offset := date2016NY.Zone()
+
+	// the difference in our values (utc and ny) should equal the offset represented as fraction of a day
+	c.Assert(val-utcVal-float64(offset)/86400.0 < 0.00000001, Equals, true)
+
+	// test jp timezone
+	jpTZ, err := time.LoadLocation("Asia/Tokyo")
+	c.Assert(err, IsNil)
+	date2016JP := time.Date(2016, 1, 1, 12, 0, 0, 0, jpTZ)
+
+	cell.SetDateWithOptions(date2016UTC, DateTimeOptions{
+		ExcelTimeFormat: "test_format2",
+		Location:        jpTZ,
+	})
+	val, err = cell.Float()
+	c.Assert(err, IsNil)
+	_, offset = date2016JP.Zone()
+
+	// the difference in our values (utc and ny) should equal the offset represented as fraction of a day
+	c.Assert(val-utcVal-float64(offset)/86400.0 < 0.00000001, Equals, true)
+
+}