I am trying to convert date/time string to unix timestamp string:
<?php
echo strtotime("20140921040000");
?>
Output: 1411286400 //timestamp
Doing same in GO lang, but not getting desired result.
Go code is below:
package main
import (
"fmt"
"time"
)
func main() {
tm := time.Unix(1411286400, 0)
fmt.Println(tm) //output: 2014-09-21 08:00:00 +0000 UTC
//================
layout := "20060102150405"
str := "20140921040000"
t, err := time.Parse(layout, str)
if err != nil {
fmt.Println(err)
}
fmt.Println(t) // output: 2014-09-21 04:00:00 +0000 UTC
}
In your last line do:
fmt.Println(t.Unix())
You can compare it with this PHP code: http://sandbox.onlinephpfunctions.com/code/b520953ea26e2d84ed85db6f5657ceeccade08d4
Related
I wish to convert a string timestamp (for which no timezone was provided) to a time with timezone of UTC -08:00.
Code:
package main
import (
"fmt"
"log"
"time"
)
func main() {
layout := "1/02/2006 15:04:05 -700"
cellContent := "7/28/2021 22:45:34"
t, err := time.Parse(layout, fmt.Sprintf("%s %s", cellContent, "-800"))
if err == nil {
fmt.Println(t.String())
} else {
log.Fatal(err)
}
}
This fails with message:
parsing time "7/28/2021 22:45:34 -800" as "1/02/2006 15:04:05 -700":
cannot parse "800" as " -700"
I believe I have an error in my layout string, but haven't been able to identify it. What am I doing wrong?
Go Playground
See comment from #Adrian, who nailed it.
The layout timezone must have a leading zero. Thanks!
Given an arbitrary time offset, how does one go about creating a usable time.Location object that represents that time offset?
The following code parses a time using an offset, but fmt.Println(t.Location()) subsequently returns no information:
func main() {
offset := "+1100"
t, err := time.Parse("15:04 GMT-0700","15:06 GMT"+offset)
if err != nil {
fmt.Println("fail", err)
}
fmt.Println(t)
fmt.Println(t.UTC())
fmt.Println(t.Location())
}
Playground: https://play.golang.org/p/j_E28qJ8Vgy
Basically I have some time data with time offsets, but without location data, I want to create a time.Location object to ensure the GMT offset is recorded. And then be able to output the time relative to the end users actual location time offset.
Use:
loc := time.FixedZone("UTC+11", +11*60*60)
Then set to this location:
t = t.In(loc)
Try this:
package main
import (
"fmt"
"time"
)
func main() {
loc := time.FixedZone("UTC+11", +11*60*60)
t := time.Now()
fmt.Println(t)
fmt.Println(t.Location())
t = t.In(loc)
fmt.Println(t)
fmt.Println(t.Location())
fmt.Println(t.UTC())
fmt.Println(t.Location())
}
Output:
2009-11-10 23:00:00 +0000 UTC m=+0.000000001
UTC
2009-11-11 10:00:00 +1100 UTC+11
UTC+11
2009-11-10 23:00:00 +0000 UTC
UTC+11
if len(offset) == 5 {
hours, ok1 := strconv.ParseInt(offset[:3], 10, 0)
mins, ok2 := strconv.ParseInt(offset[3:5], 10, 0)
if ok1 == nil && ok2 == nil {
t = t.In(time.FixedZone("Fixed", int((hours*60+mins)*60)))
fmt.Println(t)
fmt.Println(t.Location())
}
}
In the wikipedia entry for Common Log Format, the strftime format is given as:
[10/Oct/2000:13:55:36 -0700] is the date, time, and time zone that the
request was received, by default in strftime format %d/%b/%Y:%H:%M:%S
%z.
When I try using the time.Format function:
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse(time.UnixDate, "Tue Oct 10 13:55:36 PDT 2000")
fmt.Println(time.Time(t).Format("01/Feb/2006:15:04:05 -0700"))
}
I get the output [10/Feb/2000:13:55:36 +0000], while I was expecting [10/Oct/2000:13:55:36 -0700] (per Wikipedia). What is wrong with my formatting?
I checked that day was a Tuesday and the time zone was -7h (PDT) for that date.
For the Format layout, Jan not Feb. For example,
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse(time.UnixDate, "Tue Oct 10 13:55:36 PDT 2000")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(time.Time(t).Format("01/Jan/2006:15:04:05 -0700"))
}
Output:
10/Oct/2000:13:55:36 +0000
Also, for the time zone, use ParseInLocation,
package main
import (
"fmt"
"time"
)
func main() {
loc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
fmt.Println(err)
return
}
t, err := time.ParseInLocation(time.UnixDate, "Tue Oct 10 13:55:36 PDT 2000", loc)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(t)
fmt.Println(time.Time(t).Format("01/Jan/2006:15:04:05 -0700"))
}
Playground: https://play.golang.org/p/tBLw5oJSE5t
Output:
2000-10-10 13:55:36 -0700 PDT
10/Oct/2000:13:55:36 -0700
I want to show some RFC3339 time as seconds. I found how to parse times string, but it not that
t, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
For example,
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(t)
// Unix returns t as a Unix time,
// the number of seconds elapsed since January 1, 1970 UTC.
fmt.Println(t.Unix())
}
Playground: https://play.golang.org/p/LG6G4lMIWt
Output:
2012-11-01 22:08:41 +0000 UTC
1351807721
I'm working for first time on Go, in this case i have a string on UTC format, I would like to know, how can I get the difference in hours between my date and the time now.
This is my current string
v := "2014-05-03 20:57 UTC"
Use time.Parse and time.Since:
package main
import (
"fmt"
"time"
)
const (
// See http://golang.org/pkg/time/#Parse
timeFormat = "2006-01-02 15:04 MST"
)
func main() {
v := "2014-05-03 20:57 UTC"
then, err := time.Parse(timeFormat, v)
if err != nil {
fmt.Println(err)
return
}
duration := time.Since(then)
fmt.Println(duration.Hours())
}
Have a look at the time package.
package main
import "fmt"
import "time"
func main() {
a, err := time.Parse("2006-01-02 15:04 MST", "2014-05-03 20:57 UTC")
if err != nil {
// ...
return
}
delta := time.Now().Sub(a)
fmt.Println(delta.Hours())
}