Skip to content

Commit 83eedce

Browse files
committed
Export ExcelDateToTime function to convert excel date to time
Signed-off-by: Vaibhav Nayak <vaibhav.nayak@gmail.com>
1 parent 1e3c850 commit 83eedce

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

date.go

+8
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,11 @@ func timeFromExcelTime(excelTime float64, date1904 bool) time.Time {
172172
durationPart := time.Duration(dayNanoSeconds * floatPart)
173173
return date.Add(durationDays).Add(durationPart)
174174
}
175+
176+
// ExcelDateToTime converts a float-based excel date representation to a time.Time.
177+
func ExcelDateToTime(excelDate float64, use1904Format bool) (time.Time, error) {
178+
if excelDate < 0 {
179+
return time.Time{}, newInvalidExcelDateError(excelDate)
180+
}
181+
return timeFromExcelTime(excelDate, use1904Format), nil
182+
}

date_test.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ var trueExpectedDateList = []dateTest{
2828
{401769.00000000000, time.Date(3000, time.January, 1, 0, 0, 0, 0, time.UTC)},
2929
}
3030

31+
var excelTimeInputList = []dateTest{
32+
{0.0, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)},
33+
{60.0, time.Date(1900, 2, 28, 0, 0, 0, 0, time.UTC)},
34+
{61.0, time.Date(1900, 3, 1, 0, 0, 0, 0, time.UTC)},
35+
{41275.0, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC)},
36+
{401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
37+
}
38+
3139
func TestTimeToExcelTime(t *testing.T) {
3240
for i, test := range trueExpectedDateList {
3341
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
@@ -53,15 +61,7 @@ func TestTimeToExcelTime_Timezone(t *testing.T) {
5361
}
5462

5563
func TestTimeFromExcelTime(t *testing.T) {
56-
trueExpectedInputList := []dateTest{
57-
{0.0, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)},
58-
{60.0, time.Date(1900, 2, 28, 0, 0, 0, 0, time.UTC)},
59-
{61.0, time.Date(1900, 3, 1, 0, 0, 0, 0, time.UTC)},
60-
{41275.0, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC)},
61-
{401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
62-
}
63-
64-
for i, test := range trueExpectedInputList {
64+
for i, test := range excelTimeInputList {
6565
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
6666
assert.Equal(t, test.GoValue, timeFromExcelTime(test.ExcelValue, false))
6767
})
@@ -73,3 +73,17 @@ func TestTimeFromExcelTime_1904(t *testing.T) {
7373
timeFromExcelTime(61, true)
7474
timeFromExcelTime(62, true)
7575
}
76+
77+
func TestExcelDateToTime(t *testing.T) {
78+
// Check normal case
79+
for i, test := range excelTimeInputList {
80+
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
81+
timeValue, err := ExcelDateToTime(test.ExcelValue, false)
82+
assert.Equal(t, test.GoValue, timeValue)
83+
assert.NoError(t, err)
84+
})
85+
}
86+
// Check error case
87+
_, err := ExcelDateToTime(-1, false)
88+
assert.EqualError(t, err, "invalid date value -1.000000, negative values are not supported supported")
89+
}

errors.go

+4
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ func newInvalidRowNumberError(row int) error {
2222
func newInvalidCellNameError(cell string) error {
2323
return fmt.Errorf("invalid cell name %q", cell)
2424
}
25+
26+
func newInvalidExcelDateError(dateValue float64) error {
27+
return fmt.Errorf("invalid date value %f, negative values are not supported supported", dateValue)
28+
}

errors_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ func TestNewInvalidCellNameError(t *testing.T) {
1919
assert.EqualError(t, newInvalidCellNameError("A"), "invalid cell name \"A\"")
2020
assert.EqualError(t, newInvalidCellNameError(""), "invalid cell name \"\"")
2121
}
22+
23+
func TestNewInvalidExcelDateError(t *testing.T) {
24+
assert.EqualError(t, newInvalidExcelDateError(-1), "invalid date value -1.000000, negative values are not supported supported")
25+
}

0 commit comments

Comments
 (0)