time format converted to strftime default format - go

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

Related

time.Parse offset parsing fails with "cannot parse <x>00 as -0700"

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!

How i can to convert RFC3339 to UNIX in golang

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

Use timestamp from Mysql in humanize.Time() package

I have a mariaDB database with a timestamp field. I want the values from that field being parsed to time.Time() values. This is possible by adding the ?parseTime=true to the connection string. After fetching a row, I want to use the value (which is time.Time) with humanize.Time(). Unfortunately values within the past 60 minutes are converted by humanize.Time() as 1 hour from now. When I put directly a time.Time() value into humanize.Time(), it gives me x seconds ago.
So I don't know what I'm doing wrong here. I think I need to convert 2017-04-23 14:00:16 +0000 UTC to 2017-04-23 14:00:16.370758048 +0200 CEST, but how?
package main
import (
"fmt"
"log"
"time"
humanize "github.com/dustin/go-humanize"
)
// value from database: 2017-04-23 14:00:16 +0000 UTC
// typical time.Now() value: 2017-04-23 14:00:16.370758048 +0200 CEST
func main() {
layout := "2006-01-02 15:04:05 -0700 MST"
beforeParsing := "2017-04-23 14:00:16 +0000 UTC"
t, err := time.Parse(layout, beforeParsing)
if err != nil {
log.Fatal(err)
}
fmt.Println(t)
fmt.Println(humanize.Time(t))
}

PHP Equivalent to strtotime() in GO

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

How to get hours difference between two dates

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())
}

Resources