diff options
author | xuri <xuri.me@gmail.com> | 2020-03-04 00:07:13 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-04 00:07:13 +0800 |
commit | cb797540684d82fdb0fab111d0efce2977b24bf3 (patch) | |
tree | 8c96d852ebe67c39e7145d5129c793e83afc4db8 | |
parent | 1e3c85024d3bbc650c2f6a85fb075804af74720b (diff) | |
parent | 83eedce70de7a1ddeb3a4446f86b13bc6ff0b5ec (diff) |
Merge pull request #592 from hexbioc/master
Exported function to convert excel date to time
-rw-r--r-- | date.go | 8 | ||||
-rw-r--r-- | date_test.go | 32 | ||||
-rw-r--r-- | errors.go | 4 | ||||
-rw-r--r-- | errors_test.go | 4 |
4 files changed, 39 insertions, 9 deletions
@@ -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") +} @@ -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") +} |