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

Salesforce Date/Time into Golang time.Time #38

Open
gmark-newt opened this issue Sep 2, 2016 · 3 comments
Open

Salesforce Date/Time into Golang time.Time #38

gmark-newt opened this issue Sep 2, 2016 · 3 comments

Comments

@gmark-newt
Copy link

Is there any way to easily read back a Salesforce Date/Time object into a golang time.Time object?

I can write a Golang time.Time object into the Salesforce Date/Time object, but I cannot go the other way. I get this error: "Unable to unmarshal response to object: parsing time ""1965-04-06T00:00:00.000+0000"" as ""2006-01-02T15:04:05Z07:00"": cannot parse "+0000"" as "Z07:00"1"

How can I achieve this?
Thanks!

@nimajalali
Copy link
Owner

@gmark-newt You can create a type that implements the json.Marshaler and json.Unmarshaler interfaces.

Warning untested code ahead!

const SFTIMEFORMAT = "2006-01-02T15:04:05.000-0700"

type SFTime struct {
    time.Time
}

// MarshalJSON implements the json.Marshaler interface.
// The time is a quoted string in Salesforce time format.
func (t SFTime) MarshalJSON() ([]byte, error) {
    b := make([]byte, 0, len(SFTIMEFORMAT)+2)
    b = append(b, '"')
    b = t.AppendFormat(b, SFTIMEFORMAT)
    b = append(b, '"')
    return b, nil
}

// UnmarshalJSON implements the json.Unmarshaler interface.
// The time is expected to be a quoted string in Salesforce time format.
func (t *SFTime) UnmarshalJSON(data []byte) error {
    internalTime, err := time.Parse(`"`+SFTIMEFORMAT+`"`, string(data))
    if err != nil {
        return err
    }

    *t = SFTime{internalTime}
    return nil
}

@dragonsinth
Copy link
Contributor

@nimajalali if I submitted a patch to add a Date type to sobjects and change the type of built-in fields like CreatedDate and LastModifiedDate to the new type, do you think such a patch would be accepted?

@dragonsinth
Copy link
Contributor

#62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants