title: Standardizing the incoming restful API receiving times & timestamps in json
body: <p>I have a restful api which has multiple clients all using the same endpoints. Everything was going nicely when my clients happened to be written in Go, and hosted in the same regions (I don't know if the same clients in different regions would cause the following issue), however when someone attached a javascript frontend to my restful endpoint and sent a payload with a timestamp in, it was a) in a different format, b) to a differing degree of resolution and c) in the timezones of the machine it was run on (as expected).</p>
  
  <p>My restful API is receiving the payloads with timestamps in, processing the data and forwarding the data to another endpoint. When they leave my service, the timestamps must all be standardized.</p>
  
  <p>So as an example, I receive these two types of timestamps at the moment:</p>
  
  <p><code>2020-06-02T10:45:54.317494786Z</code>
  <code>2020-05-22T11:48:45.927376+01:00</code></p>
  
  <p>I think the top one is ISO timestamp and is the standard in javascript, and the second is time.RFC3339.</p>
  
  <p>In my Go applications (server and clients), whenever I generate a time, I am using <code>time.Now()</code>, however for a guaranteed standard I plan on changing that to <code>time.Now.UTC()</code></p>
  
  <p>Then I would like to convert any incoming timestamp to the same format, however I don't think javascript can offer a 9dp resolution of seconds, milliseconds and microseconds, so I think I need to trim all incoming timestamps and then somehow standardize them all.</p>
  
  <p>It seems at first glance a trivial issue, however on researching, I realise there is quite a lot of philosophy around how timezones work and I can't seem to pin down a solid way of making sure all incoming timestamps and generated times have the same format and readability by any application that then recieves them.</p>
  
  <p>So to summarize:
  API generates timestamps
  API receives timestamps from json
  json timestamps have the format defined by a client (so I need to standardize them)
  Struggling to work out the most eloquent way to convert and standardize times to that the output (marshalled back into json) all have the same format for other APIs to process</p>
  
  <p>My test application that I have tried to understand this is</p>
  
  <pre><code>func main() {
      wcJson := `{"timestamp":"2020-05-22T11:48:45.927376+01:00"}`
      dmJson := `{"timestamp":"2020-06-02T10:45:54.317494786Z"}`
      fmt.Println("time.Now().UTC()", time.Now().UTC())
      fmt.Println("time.Now()", time.Now())
      jsonObject := Obj{}
      webObject := Obj{}
      {
          unmarshalError := json.Unmarshal([]byte(dmJson), &amp;jsonObject)
          if unmarshalError != nil {
              fmt.Println("error unmarshalling dmJson", unmarshalError)
          }
          fmt.Println("dm ", jsonObject.Time.String())
          fmt.Println("RFC dm ", jsonObject.Time.UTC())
      }
      {
          unmarshalError := json.Unmarshal([]byte(wcJson), &amp;webObject)
          if unmarshalError != nil {
              fmt.Println("error unmarshalling webObject ", unmarshalError)
          }
          fmt.Println("webchat ", webObject.Time.String())
          fmt.Println("RFC webchat ", webObject.Time.UTC())
      }
  }
  
  </code></pre>
  
  <p>And that outputs</p>
  
  <pre><code>time.Now().UTC() 2020-06-03 09:45:54.165408 +0000 UTC
  time.Now() 2020-06-03 10:45:54.165507 +0100 BST m=+0.000330629
  dm  2020-06-02 10:45:54.317494786 +0000 UTC
  RFC dm  2020-06-02 10:45:54.317494786 +0000 UTC
  webchat  2020-05-22 11:48:45.927376 +0100 BST
  RFC webchat  2020-05-22 10:48:45.927376 +0000 UTC
  </code></pre>
  
  <p>However it hasn't got me much closer to working out how to standardize the times.</p>
  
  <p>To be clear, I don't want to convert the times to the time in a standard timezone (e.g I'm happy with the <code>+0000</code> but I do want them all to be the same format.</p>
  
  <p>Thanks</p>