How to get short month name from time.Now() in Golang - go

current := time.Now().UTC()
y, m, d := current.Date()
fmt.Println(y, m, d)
Output:
2009 November 10
How can I get short month name? Like:
2009 Nov 10

Use the Format function with Jan for short month name, ie
current := time.Now().UTC()
fmt.Println(current.Format("2006 Jan 02"))

Use time.Now().UTC().Format("Jan") or m.String()[:3] to get short month name:
current := time.Now().UTC()
y, m, d := current.Date()
fmt.Println(y, m.String()[:3], d)
Also you may use fmt.Sprintf("%d %s %02d", t.Year(), t.Month().String()[:3], t.Day())
like this working sample code:
package main
import "fmt"
import "time"
func main() {
fmt.Println(time.Now().UTC().Format("Jan")) // Aug
t := time.Now()
str := fmt.Sprintf("%d %s %02d", t.Year(), t.Month().String()[:3], t.Day())
fmt.Println(str) // 2016 Aug 03
}
output:
Aug
2016 Aug 03

Related

Timezone parsing from date in string gives empty string

I want to parse a date (having timezone) represented by a string into Time.
I tried the below code:
package main
import (
"fmt"
"time"
)
func main() {
dateString := "Sat Jan 02 15:04:05 -0700 2021"
dateTime, _ := time.Parse("Mon Jan 02 15:04:05 -0700 2006", dateString)
zoneName, _ := dateTime.Zone()
fmt.Println("Zone Name is "+ zoneName)
loc, _ := time.LoadLocation(zoneName)
fmt.Println(loc)
}
On running the code I am getting the zoneName as empty string which eventually transforms into UTC whereas I should be getting a valid time zone? What mistake have I done to parse the date?

parse string returns month out of range error

I am going to parse ps -eo pid,lstart,cmd output to get process start time
the shell output date format is like this:
Mon Dec 17 16:20:07 2018
here is my code,
package main
import (
"fmt"
"time"
)
func main () {
myDateString := "Mon Dec 17 16:20:07 2018"
myDate, err := time.Parse("Mon Jan 02 15:04:05 2016", myDateString)
if err != nil {
fmt.Println(err)
}
fmt.Println(myDate)
}
go out
parsing time "Mon Dec 17 16:20:07 2018": month out of range
0001-01-01 00:00:00 +0000 UTC
is there any wrong in my usage?
It should be
myDate, err := time.Parse("Mon Jan 02 15:04:05 2006", myDateString)
instead of
myDate, err := time.Parse("Mon Jan 02 15:04:05 2016", myDateString)

For loop while time.Now() is reached

Is it possible in Golang to increment a date in a for loop by a given date variable till it reached the current date/ time.Now()
// Start date
t, _ := time.Parse(time.RFC3339, "2018-07-19T12:25:10.8584224+02:00")
// Current date
ct := time.Now()
for d := t; d.Day() == ct.Day(); d = d.AddDate(0, 0, 1) {
// Print all days between start date and current date
fmt.Println(d)
}
I expect that variable d prints out all dates (with time etc.) till it reached the current date
according to godoc: https://golang.org/pkg/time/#Time.Day
func (t Time) Day() int
Day returns the day of the month specified by t.
So comparing d.Day() and ct.Day() is not the right approaches. What if today is "2019-01-01",and you start time is "2018-12-23"?
The right way to compare two time.Time is https://golang.org/pkg/time/#Time.After
func (t Time) After(u Time) bool
func (t Time) Before(u Time) bool
After reports whether the time instant t is after u.
Before reports whether the time instant t is before u.
So #Alex Pliutau's solution is more in common use. But need more careful with today.
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse(time.RFC3339, "2009-11-02T12:25:10.8584224+02:00")
// truncate to 0:0:0
t = t.Truncate(24 * time.Hour)
fmt.Println("start time is:", t)
// Current date truncate to 0:0:0
ct := time.Now().Truncate(24 * time.Hour)
fmt.Println("now is:", ct)
fmt.Println("---------------")
// for t.Before(ct) { //if you don't want to print the date of today
for !t.After(ct) {
// Print all days between start date and current date
fmt.Println(t.Format("2006-01-02 15:04:05"))
t = t.AddDate(0, 0, 1)
}
}
Output:
start time is: 2009-11-02 02:00:00 +0200 +0200
now is: 2009-11-10 00:00:00 +0000 UTC
---------------
2009-11-02 02:00:00
2009-11-03 02:00:00
2009-11-04 02:00:00
2009-11-05 02:00:00
2009-11-06 02:00:00
2009-11-07 02:00:00
2009-11-08 02:00:00
2009-11-09 02:00:00
2009-11-10 02:00:00
https://play.golang.org/p/iMr7M5W9K4N
get the loop condition right and..
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Hello, playground")
t, _ := time.Parse(time.RFC3339, "2018-07-19T12:25:10.8584224+02:00")
// Current date
ct := time.Now()
for d := t; d.Day() >= ct.Day(); d = d.AddDate(0, 0, 1) {
// Print all days between start date and current date
fmt.Println(d)
}
}
Hello, playground
2018-07-19 12:25:10.8584224 +0200 +0200
2018-07-20 12:25:10.8584224 +0200 +0200
2018-07-21 12:25:10.8584224 +0200 +0200
2018-07-22 12:25:10.8584224 +0200 +0200
2018-07-23 12:25:10.8584224 +0200 +0200
2018-07-24 12:25:10.8584224 +0200 +0200
2018-07-25 12:25:10.8584224 +0200 +0200
2018-07-26 12:25:10.8584224 +0200 +0200
2018-07-27 12:25:10.8584224 +0200 +0200
2018-07-28 12:25:10.8584224 +0200 +0200
2018-07-29 12:25:10.8584224 +0200 +0200
2018-07-30 12:25:10.8584224 +0200 +0200
2018-07-31 12:25:10.8584224 +0200 +0200
https://play.golang.org/p/yRBTUZKfseG
Based on your comments, you need to actually tell it to Format the date to something of value:
package main
import (
"fmt"
"log"
"time"
)
func main() {
start, err := time.Parse("2006-1-2", "2018-1-1")
if err != nil {
log.Fatal(err)
}
for d := start; d.Month() == start.Month(); d = d.AddDate(0, 0, 1) {
fmt.Println(d.Format("2006-1-2"))
}
}
Here's a simpler version of your code (I used a custom time format, cause I didn't wanna edit the RFC syntax, but ultimately it's the same thing) = I'm also iterating Month for brevity.
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse(time.RFC3339, "2018-07-19T12:25:10.8584224+02:00")
ct := time.Now()
for t.Before(ct) {
fmt.Println(t)
t.AddDate(0, 0, 1)
}
}

time format converted to strftime default format

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

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

Resources