Introduction
When I tried to get the timestamp of the Facebook post, I found that the
timestamp is embedded in the data-utime attribute of abbr element:
<abbr title="Wednesday, February 15, 2017 at 7:00am" data-utime="1487113202" data-shorten="1" class="_5ptz"><span class="timestampContent">Yesterday at 7:00am</span></abbr>
The string 1487113202 looks familiar, so I did some googling and
found that it represents Unix time, seconds and nanoseconds that have elapsed
since January 1, 1970 UTC.
In this post, we will show how to parse the string of Unix time (also known
as POSIX time or Epoch time) in Go programmming language.
Solution
Steps:
- Use strconv.ParseInt to convert the string to integer.
- Use time.Unix to convert the integer to time.Time type,
which represents an instant in time with nanosecond precision.
Run Code on Go Playground
import (
"strconv"
"time"
)
func ParseTimeStamp(utime string) (string, error) {
i, err := strconv.ParseInt(utime, 10, 64)
if err != nil {
return "", err
}
t := time.Unix(i, 0)
return t.Format(time.UnixDate), nil
}
Result returned from running code on Go Playground:
Tue Feb 14 23:00:02 UTC 2017
Tested on:
References