Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timezone location is ignored when formatting time.Time to Excel date. #1069

Closed
btnmasher opened this issue Nov 18, 2021 · 4 comments
Closed
Projects

Comments

@btnmasher
Copy link

btnmasher commented Nov 18, 2021

Description

Using the StreamWriter (possibly with non-streaming API as well but haven't tested as much), outputting a cell with a time.Time value that has its loc field defined with a valid time.Location does not result in a timezone-accurate float value being written to Excel.

If the original time value was parsed from a UTC timestamp string, and it had its location specified by using .In(timezone) to set the loc field, it seems this is ignored by excelize's conversion to an excel float value (ie: it never applies the timezone offset).

A workaround for this for the time being:

	// Ignoring errors for demonstration
	timeStringUTC := "2009-11-10T23:00:00Z"
	timezone, _ := time.LoadLocation("America/New_York")
	date, _ := time.Parse(time.RFC3339Nano, timeStringUTC)
	_, offset := date.In(timezone).Zone() // Get the offset (in seconds) of the timestamp as if it were from the specified target timezone
	fixedDate := date.Add(time.Duration(offset) * time.Second) // Manually apply the offset
	// Use fixedDate as the value for a Cell ....

Steps to reproduce the issue:

  1. Start a StreamWriter write flow.
  2. Create a time.Time value and set its location:
        date := time.Parse(time.RFC3339Nano, "2009-11-10T23:00:00Z") // a UTC value
        timezone := time.LoadLocation("America/New_York")
        dateWithTz := date.In(timezone) // Set the location of the time value
  1. Create a Cell{} and set it's Value field with the time value.
  2. Write the Cell to a row with .SetRow()
  3. Observe the time value in spreadsheet software in comparison with the specified timezone.

Describe the results you received:
Cell in spreadsheet software shows the time as UTC instead of specified timezone.

Describe the results you expected:
Cell in spreadsheet software shows the correct time for the timezone specified.

Output of go version:

go version go1.17.2 darwin/amd64

Excelize version or commit ID:

adecf44

Environment details (OS, Microsoft Excel™ version, physical, etc.):

MacOS Big Sur

@btnmasher btnmasher changed the title Timezone location is ignored when formatting time.TIme to Excel date. Timezone location is ignored when formatting time.Time to Excel date. Nov 18, 2021
@xuri xuri closed this as completed in 7907650 Nov 23, 2021
@xuri xuri added this to Bugfix in v2.5.0 Nov 23, 2021
@xuri
Copy link
Member

xuri commented Nov 23, 2021

Thanks for your issue, I have fixed it, please upgrade to the master branch code, and this patch will be released in the next version.

@dolmen
Copy link
Contributor

dolmen commented Feb 25, 2022

The change is in commit 7907650.

@dolmen
Copy link
Contributor

dolmen commented Feb 25, 2022

Unfortunately the change is, like the suggested workaround in the description of this issue, just incorrect because it assumes that the time zone offset is fixed. But this is not the case over time: time offset change in history, but also much more often in timezone that use daylight saving.

So the only safe timezone to use with excelize is still UTC.

@btnmasher
Copy link
Author

Unfortunately the change is, like the suggested workaround in the description of this issue, just incorrect because it assumes that the time zone offset is fixed. But this is not the case over time: time offset change in history, but also much more often in timezone that use daylight saving.

So the only safe timezone to use with excelize is still UTC.

Can you elaborate further why this doesn't work, or perhaps demonstrate an edge case?

jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v2.5.0
Bugfix
Development

No branches or pull requests

3 participants