Browse Source

add method Cell.ToDate(date1904 bool)

Ivan Anfilatov 9 years ago
parent
commit
d553e75f05
2 changed files with 22 additions and 0 deletions
  1. 9 0
      cell.go
  2. 13 0
      cell_test.go

+ 9 - 0
cell.go

@@ -77,6 +77,15 @@ func (c *Cell) SetFloat(n float64) {
 	c.SetValue(n)
 }
 
+//GetTime returns the value of a Cell as a time.Time
+func (c *Cell) GetTime(date1904 bool) (t time.Time, err error) {
+	f, err := c.Float()
+	if err != nil {
+		return t, err
+	}
+	return TimeFromExcelTime(f, date1904), nil
+}
+
 /*
 	The following are samples of format samples.
 

+ 13 - 0
cell_test.go

@@ -117,6 +117,19 @@ func (l *CellSuite) TestSetFloat(c *C) {
 	c.Assert(cell.Value, Equals, "37947.75334343")
 }
 
+func (s *CellSuite) TestGetTime(c *C) {
+	cell := Cell{}
+	cell.SetFloat(0)
+	date, err := cell.GetTime(false)
+	c.Assert(err, Equals, nil)
+	c.Assert(date, Equals, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC))
+	cell.SetFloat(39813.0)
+	date, err = cell.GetTime(true)
+	c.Assert(err, Equals, nil)
+	c.Assert(date, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
+
+}
+
 // FormattedValue returns an error for formatting errors
 func (l *CellSuite) TestFormattedValueErrorsOnBadFormat(c *C) {
 	cell := Cell{Value: "Fudge Cake"}