summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVaibhav Nayak <vaibhav.nayak@gmail.com>2020-03-03 17:01:02 +0530
committerVaibhav Nayak <vaibhav.nayak@gmail.com>2020-03-03 18:12:02 +0530
commit83eedce70de7a1ddeb3a4446f86b13bc6ff0b5ec (patch)
tree8c96d852ebe67c39e7145d5129c793e83afc4db8
parent1e3c85024d3bbc650c2f6a85fb075804af74720b (diff)
Export ExcelDateToTime function to convert excel date to time
Signed-off-by: Vaibhav Nayak <vaibhav.nayak@gmail.com>
-rw-r--r--date.go8
-rw-r--r--date_test.go32
-rw-r--r--errors.go4
-rw-r--r--errors_test.go4
4 files changed, 39 insertions, 9 deletions
diff --git a/date.go b/date.go
index dad39b5..172c32c 100644
--- a/date.go
+++ b/date.go
@@ -172,3 +172,11 @@ func timeFromExcelTime(excelTime float64, date1904 bool) time.Time {
durationPart := time.Duration(dayNanoSeconds * floatPart)
return date.Add(durationDays).Add(durationPart)
}
+
+// ExcelDateToTime converts a float-based excel date representation to a time.Time.
+func ExcelDateToTime(excelDate float64, use1904Format bool) (time.Time, error) {
+ if excelDate < 0 {
+ return time.Time{}, newInvalidExcelDateError(excelDate)
+ }
+ return timeFromExcelTime(excelDate, use1904Format), nil
+}
diff --git a/date_test.go b/date_test.go
index 2885af0..ee01356 100644
--- a/date_test.go
+++ b/date_test.go
@@ -28,6 +28,14 @@ var trueExpectedDateList = []dateTest{
{401769.00000000000, time.Date(3000, time.January, 1, 0, 0, 0, 0, time.UTC)},
}
+var excelTimeInputList = []dateTest{
+ {0.0, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)},
+ {60.0, time.Date(1900, 2, 28, 0, 0, 0, 0, time.UTC)},
+ {61.0, time.Date(1900, 3, 1, 0, 0, 0, 0, time.UTC)},
+ {41275.0, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC)},
+ {401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
+}
+
func TestTimeToExcelTime(t *testing.T) {
for i, test := range trueExpectedDateList {
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
@@ -53,15 +61,7 @@ func TestTimeToExcelTime_Timezone(t *testing.T) {
}
func TestTimeFromExcelTime(t *testing.T) {
- trueExpectedInputList := []dateTest{
- {0.0, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)},
- {60.0, time.Date(1900, 2, 28, 0, 0, 0, 0, time.UTC)},
- {61.0, time.Date(1900, 3, 1, 0, 0, 0, 0, time.UTC)},
- {41275.0, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC)},
- {401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
- }
-
- for i, test := range trueExpectedInputList {
+ for i, test := range excelTimeInputList {
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
assert.Equal(t, test.GoValue, timeFromExcelTime(test.ExcelValue, false))
})
@@ -73,3 +73,17 @@ func TestTimeFromExcelTime_1904(t *testing.T) {
timeFromExcelTime(61, true)
timeFromExcelTime(62, true)
}
+
+func TestExcelDateToTime(t *testing.T) {
+ // Check normal case
+ for i, test := range excelTimeInputList {
+ t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
+ timeValue, err := ExcelDateToTime(test.ExcelValue, false)
+ assert.Equal(t, test.GoValue, timeValue)
+ assert.NoError(t, err)
+ })
+ }
+ // Check error case
+ _, err := ExcelDateToTime(-1, false)
+ assert.EqualError(t, err, "invalid date value -1.000000, negative values are not supported supported")
+}
diff --git a/errors.go b/errors.go
index 4560497..5576ecd 100644
--- a/errors.go
+++ b/errors.go
@@ -22,3 +22,7 @@ func newInvalidRowNumberError(row int) error {
func newInvalidCellNameError(cell string) error {
return fmt.Errorf("invalid cell name %q", cell)
}
+
+func newInvalidExcelDateError(dateValue float64) error {
+ return fmt.Errorf("invalid date value %f, negative values are not supported supported", dateValue)
+}
diff --git a/errors_test.go b/errors_test.go
index 89d241c..207e80a 100644
--- a/errors_test.go
+++ b/errors_test.go
@@ -19,3 +19,7 @@ func TestNewInvalidCellNameError(t *testing.T) {
assert.EqualError(t, newInvalidCellNameError("A"), "invalid cell name \"A\"")
assert.EqualError(t, newInvalidCellNameError(""), "invalid cell name \"\"")
}
+
+func TestNewInvalidExcelDateError(t *testing.T) {
+ assert.EqualError(t, newInvalidExcelDateError(-1), "invalid date value -1.000000, negative values are not supported supported")
+}