date_test.go 2.8 KB
Newer Older
1 2 3
package excelize

import (
V
Veniamin Albaev 已提交
4
	"fmt"
5 6
	"testing"
	"time"
V
Veniamin Albaev 已提交
7 8

	"github.com/stretchr/testify/assert"
9 10 11
)

type dateTest struct {
12 13
	ExcelValue float64
	GoValue    time.Time
14 15
}

16 17 18 19
var trueExpectedDateList = []dateTest{
	{0.0000000000000000, time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)},
	{25569.000000000000, time.Unix(0, 0).UTC()},

20
	// Expected values extracted from real spreadsheet
21 22 23 24 25 26 27 28 29 30
	{1.0000000000000000, time.Date(1900, time.January, 1, 0, 0, 0, 0, time.UTC)},
	{1.0000115740740740, time.Date(1900, time.January, 1, 0, 0, 1, 0, time.UTC)},
	{1.0006944444444446, time.Date(1900, time.January, 1, 0, 1, 0, 0, time.UTC)},
	{1.0416666666666667, time.Date(1900, time.January, 1, 1, 0, 0, 0, time.UTC)},
	{2.0000000000000000, time.Date(1900, time.January, 2, 0, 0, 0, 0, time.UTC)},
	{43269.000000000000, time.Date(2018, time.June, 18, 0, 0, 0, 0, time.UTC)},
	{43542.611111111109, time.Date(2019, time.March, 18, 14, 40, 0, 0, time.UTC)},
	{401769.00000000000, time.Date(3000, time.January, 1, 0, 0, 0, 0, time.UTC)},
}

31 32 33 34 35 36 37 38
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)},
}

39
func TestTimeToExcelTime(t *testing.T) {
40 41
	for i, test := range trueExpectedDateList {
		t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
42 43 44
			excelTime, err := timeToExcelTime(test.GoValue)
			assert.NoError(t, err)
			assert.Equalf(t, test.ExcelValue, excelTime,
45 46
				"Time: %s", test.GoValue.String())
		})
47
	}
48
}
49

50
func TestTimeToExcelTime_Timezone(t *testing.T) {
51
	location, err := time.LoadLocation("America/Los_Angeles")
52 53 54 55
	if !assert.NoError(t, err) {
		t.FailNow()
	}
	for i, test := range trueExpectedDateList {
V
Veniamin Albaev 已提交
56
		t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
57
			_, err := timeToExcelTime(test.GoValue.In(location))
58
			assert.NoError(t, err)
V
Veniamin Albaev 已提交
59
		})
60
	}
61 62 63
}

func TestTimeFromExcelTime(t *testing.T) {
64
	for i, test := range excelTimeInputList {
V
Veniamin Albaev 已提交
65 66 67
		t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
			assert.Equal(t, test.GoValue, timeFromExcelTime(test.ExcelValue, false))
		})
68
	}
69
}
70 71

func TestTimeFromExcelTime_1904(t *testing.T) {
xurime's avatar
xurime 已提交
72
	_, _ = shiftJulianToNoon(1, -0.6)
73 74 75
	timeFromExcelTime(61, true)
	timeFromExcelTime(62, true)
}
76 77 78 79 80 81 82 83 84 85 86 87

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)
xurime's avatar
xurime 已提交
88
	assert.EqualError(t, err, "invalid date value -1.000000, negative values are not supported")
89
}