This closes #1069, support time zone location when set cell value

This commit is contained in:
xuri 2021-11-24 00:09:35 +08:00
parent 9561976074
commit 7907650a97
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 20 additions and 0 deletions

View File

@ -225,6 +225,8 @@ func (f *File) setCellTimeFunc(sheet, axis string, value time.Time) error {
// timestamp. // timestamp.
func setCellTime(value time.Time) (t string, b string, isNum bool, err error) { func setCellTime(value time.Time) (t string, b string, isNum bool, err error) {
var excelTime float64 var excelTime float64
_, offset := value.In(value.Location()).Zone()
value = value.Add(time.Duration(offset) * time.Second)
excelTime, err = timeToExcelTime(value) excelTime, err = timeToExcelTime(value)
if err != nil { if err != nil {
return return

View File

@ -178,6 +178,24 @@ func TestSetCellBool(t *testing.T) {
assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), `cannot convert cell "A" to coordinates: invalid cell name "A"`) assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
} }
func TestSetCellTime(t *testing.T) {
date, err := time.Parse(time.RFC3339Nano, "2009-11-10T23:00:00Z")
assert.NoError(t, err)
for location, expected := range map[string]string{
"America/New_York": "40127.75",
"Asia/Shanghai": "40128.291666666664",
"Europe/London": "40127.958333333336",
"UTC": "40127.958333333336",
} {
timezone, err := time.LoadLocation(location)
assert.NoError(t, err)
_, b, isNum, err := setCellTime(date.In(timezone))
assert.NoError(t, err)
assert.Equal(t, true, isNum)
assert.Equal(t, expected, b)
}
}
func TestGetCellValue(t *testing.T) { func TestGetCellValue(t *testing.T) {
// Test get cell value without r attribute of the row. // Test get cell value without r attribute of the row.
f := NewFile() f := NewFile()