Fully Parsing Timestamps in Golang
<p>I'm trying to make a simple tool which parses JSON-formatted lines in a file and performs an <code>INSERT</code> operation into a database.</p>

<p>I have a struct which looks like this:</p>

<pre><code>type DataBlob struct {
  ....
  Datetime time.Time `json:"datetime, string"`
  ....
}
</code></pre>

<p>And parsing code which looks like this:</p>

<pre><code>scanner := bufio.NewScanner(file)
// Loop through all lines in the file
for scanner.Scan() {
    var t DataBlob

    // Decode the line, parse the JSON
    dec := json.NewDecoder(strings.NewReader(scanner.Text()))
    if err := dec.Decode(&amp;t);
    err != nil {
        panic(err)
    }

    // Perform the database operation
    executionString: = "INSERT INTO observations (datetime) VALUES ($1)"
    _, err := db.Exec(executionString, t.Datetime)
    if err != nil {
        panic(err)
    }
}
</code></pre>

<p>My JSON file has lines, each containing a <code>datetime</code> value that looks like this:</p>

<pre><code>{ "datetime": 1465793854 }
</code></pre>

<p>When the <code>datetime</code> is formatted as a Unix timestamp, the Marshaller complains:</p>

<pre><code>panic: parsing time "1465793854" as ""2006-01-02T15:04:05Z07:00"": cannot parse "1465793854" as """
</code></pre>

<hr>

<p>In the script that generates the JSON (also written in Golang), I tried simply printing the String representation of the <code>Time.time</code> type, producing the following:</p>

<pre><code>{ "datetime": "2016-06-13 00:23:34 -0400 EDT" }
</code></pre>

<p>To which the Marshaller complains when I go to parse it:</p>

<pre><code>panic: parsing time ""2016-06-13 00:23:34 -0400 EDT"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 00:23:34 -0400 EDT"" as "T"
</code></pre>

<p>If I also treat this timestamp (which looks pretty standard) as a String and avoid the Marshaling problem, Postgres complains when I try to perform the insertion:</p>

<pre><code>panic: pq: invalid input syntax for type timestamp: "2016-06-13 00:23:34 -0400 EDT"
</code></pre>

<p>This is frustrating on a number of levels, but mainly because if I serialize a <code>Time.time</code> type, I would think it should still be understood at the other side of the process.</p>

<p>How can I go about parsing this timestamp to perform the database insertion? Apologizes for the lengthy question and thanks for your help!</p>