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 is not considered in MarshalText method #113

Closed
shmsr opened this issue Jul 11, 2020 · 4 comments · Fixed by #118
Closed

Timezone is not considered in MarshalText method #113

shmsr opened this issue Jul 11, 2020 · 4 comments · Fixed by #118
Labels

Comments

@shmsr
Copy link
Contributor

shmsr commented Jul 11, 2020

This code block is taken from the output generated by xsdgen:

func (t *xsdDateTime) UnmarshalText(text []byte) error {
	return _unmarshalTime(text, (*time.Time)(t), "2006-01-02T15:04:05.999999999")
}

func (t xsdDateTime) MarshalText() ([]byte, error) {
	return []byte((time.Time)(t).Format("2006-01-02T15:04:05.999999999")), nil
}

func _unmarshalTime(text []byte, t *time.Time, format string) (err error) {
	s := string(bytes.TrimSpace(text))
	*t, err = time.Parse(format, s)
	if _, ok := err.(*time.ParseError); ok {
		*t, err = time.Parse(format+"Z07:00", s)
	}
	return err
}

_unmarshalTime function is called when the UnmarshalText method is used which takes care of the parsing the timezone from the input. But MarshalText doesn't consider the timezone and hence ignored.

Example

With the MarshalText method generated by xsdgen, it does the marshalling without the timezone:

<granPeriod endTime="2020-03-26T12:30:00"></granPeriod>

But suppose if Z07:00 is added to MarshalText method:

func (t xsdDateTime) MarshalText() ([]byte, error) {
	return []byte((time.Time)(t).Format("2006-01-02T15:04:05.999999999Z07:00")), nil
}

Then it properly considers the timezone when marshalling:

<granPeriod endTime="2020-03-26T12:30:00-05:00"></granPeriod>
@droyo droyo added the bug label Jul 11, 2020
@shmsr
Copy link
Contributor Author

shmsr commented Jul 12, 2020

Would you accept a PR? I'd like to fix this bug.

@droyo
Copy link
Owner

droyo commented Jul 13, 2020

Yes, please file a PR. Thanks for reporting this!

shmsr added a commit to shmsr/go-xml that referenced this issue Aug 21, 2020
Fixes: droyo#113

This issues fixes the method xsdDateTime.MarshalText by adding the
support for considering the timezone offset when marshalling. Previously
it dropped the offset.
shmsr added a commit to shmsr/go-xml that referenced this issue Aug 21, 2020
Fixes: droyo#113

This commit fixes the method xsdDateTime.MarshalText by adding the
support for considering the timezone offset when marshalling. Previously
it dropped the offset.
@shmsr
Copy link
Contributor Author

shmsr commented Aug 21, 2020

@droyo I've added a commit that fixes this bug. Could you please review it? And really sorry for being this late.

@shmsr
Copy link
Contributor Author

shmsr commented Oct 18, 2020

@droyo Did you get a chance to review this? This is just a reminder!

@droyo droyo closed this as completed in #118 Nov 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants