Time Layout in Golang - go

I know we need to use the time.Time interface for dates in Go.
And to format, we need to use the format function.
http://golang.org/pkg/time/#Time.Format
But what are the valid and different formatters available for time.Time in Golang?

The docs for time.Format say http://golang.org/pkg/time/#Time.Format:
Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard and convenient representations of the reference time. For more information about the formats and the definition of the reference time, see the documentation for ANSIC and the other constants defined by this package.
So, in the constants http://golang.org/pkg/time/#pkg-constants:
To define your own format, write down what the reference time would look like formatted your way; see the values of constants like ANSIC, StampMicro or Kitchen for examples. The model is to demonstrate what the reference time looks like so that the Format and Parse methods can apply the same transformation to a general time value.
In short: you write the reference time Mon Jan 2 15:04:05 MST 2006 in the format you want and pass that string to Time.Format()
As #Volker said, please read the docs and read about the difference between types and interfaces.

package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Format(time.Kitchen))
t := time.Now()
tf := t.Format("2006-01-02 15:04:05-07:00")
fmt.Println(tf)
}

In case you need custom layout and/or struggling with build in layouts.
type Rtime struct {
Year int
Month int
Day int
Hour int
Minute int
Second int
Nanosecond int
Millisecond int
Offset int
OffsetString string
Zone string
}
func (rt *Rtime) LocalNow() {
t := time.Now()
rt.Hour,rt.Minute,rt.Second = t.Clock()
rt.Nanosecond = t.Nanosecond()
rt.Millisecond = rt.Nanosecond / 1000000
rt.Month = int(t.Month())
rt.Day = t.Day()
rt.Year = t.Year()
rt.OffsetString = ""
rt.Zone, rt.Offset = t.Local().Zone()
if rt.Offset > 0 {
rt.OffsetString = fmt.Sprintf("+%02d%02d",
rt.Offset/(60*60),
rt.Offset%(60*60)/60)
} else {
rt.OffsetString = fmt.Sprintf("%02d%02d",
rt.Offset/(60*60),
rt.Offset%(60*60)/60)
}
}
str := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %s %s %d",
rt.Year,rt.Month,rt.Day,rt.Hour,rt.Minute,rt.Second,
rt.Millisecond,rt.Zone,rt.OffsetString,rt.Nanosecond)
fmt.Println(str)
output
2021-06-06 09:21:54.949 EEST +0300 949861778

Golang prescribes different standards to be followed for getting valid dates.
Available in http://golang.org/pkg/time/#Time.Format

Related

How to use multiple formats to parse dates in a loop?

I am trying to read a huge csv file with a date column having value in 2 possible formats which are non-standard...
12/28/2015 -- mm/dd/yyyy
11/2/2013 -- mm/d/yyyy
...meaning the middle day component can be single or double digit.
I learnt how to use format from this nice old question: Parsing date/time strings which are not 'standard' formats. But since i am going in a loop trying to parse each row, i can specify only one format to be used at a time. Now it errors on finding date value of different format. Maybe i can code to catch error when parse-using-format#1 fails and then apply format#2, rather than erroring out. But could someone please point me to a better/correct way?
A sample code with array of date strings: https://play.golang.org/p/aloIQnrkOjK
package main
import (
"fmt"
"time"
)
func main() {
const format = "01/02/2006" //mm/dd/yyyy
var list [2]string = [2]string{"12/28/2015", "11/2/2013"}
for _, data := range list {
t, err := time.Parse(format, data)
if err != nil {
fmt.Println("Error is: ", err)
} else {
fmt.Println("Value is: ", t)
}
}
}
//Output:
Value is: 2015-12-28 00:00:00 +0000 UTC
Error is: parsing time "11/2/2013" as "01/02/2006": cannot parse "2/2013" as "02"
The code in the question parses multiple dates with a single format. The problem is that one of the dates does not match the format (missing leading zero on day). Fix by making the leading zero optional:
const format = "1/2/2006" // remove zero before day (and month just in case)

How to convert time.Time variable to atomic in Go?

In my RESTFUL web service which is an online game, I'm storing starting time of every question in an global variable like this: var MyTime time.Time which I should update it after every level of the game. My application is distributed, so I want to make sure all of my apps are not updating it at the same time. That's why I've decided to make it atomic.
Actually I'm familiar with Golang sync/atomic package.
I tried to use atomic.LoadPointer() method but it needs specific argument type which isn't safe. Do you any other way for this?
Update:
Okay I solved my problem like this.
I defined time variable as atomic.Value and used atomic Load and Store methods. This is the code:
var myTime atomic.Value
myTime.Store(newTime) and load myTime.Load().(time.Time).
Consider that Load() method returns interface, so you should write (time.Time) at the end in order to convert it to time.Time type.
This can't be done, as such, because time.Time is a compound type:
type Time struct {
// wall and ext encode the wall time seconds, wall time nanoseconds,
// and optional monotonic clock reading in nanoseconds.
//
// From high to low bit position, wall encodes a 1-bit flag (hasMonotonic),
// a 33-bit seconds field, and a 30-bit wall time nanoseconds field.
// The nanoseconds field is in the range [0, 999999999].
// If the hasMonotonic bit is 0, then the 33-bit field must be zero
// and the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext.
// If the hasMonotonic bit is 1, then the 33-bit field holds a 33-bit
// unsigned wall seconds since Jan 1 year 1885, and ext holds a
// signed 64-bit monotonic clock reading, nanoseconds since process start.
wall uint64
ext int64
// loc specifies the Location that should be used to
// determine the minute, hour, month, day, and year
// that correspond to this Time.
// The nil location means UTC.
// All UTC times are represented with loc==nil, never loc==&utcLoc.
loc *Location
}
However, you can do this with pointers, so *time.Time would be possible, if this suits your needs. But of course, this is discouraged, by virtue of the fact that atomic.LoadPointer and atomic.StorePointer use the unsafe package to accomplish their magic.
A much better approach, if it will work for you, is just to use a mutex to protect your value. There are many ways to do this, but one minimal example:
type MyTime struct {
t time.Time
mu sync.RWMutex
}
func (t *MyTime) Time() time.Time {
t.mu.RLock()
defer t.mu.RUnlock()
return t.t
}
func (t *MyTime) SetTime(tm time.Time) {
t.mu.Lock()
defer t.mu.Unlock()
t.t = tm
}
You can keep unix time https://golang.org/pkg/time/#example_Time_Unix as atomic which is int64. Then convert to go time after you've read atomic value.
If you are only interested in the timestamp, you could simply keep a reference to the unix time, which is int64 and atomically update that.
var ts int64
func main() {
atomic.StoreInt64(&ts, time.Now().Unix())
t := time.Unix(atomic.LoadInt64(&ts), 0)
fmt.Println(t)
}
Instead, if you need the entire Time struct, read on.
Go 1.19 (still in beta)
If you are okay with storing a pointer to a time.Time object, you can use atomic.Pointer, which is a generic struct that abstracts atomic.LoadPointer and atomic.StorePointer. As a simple example:
// instantiate generic struct with time.Time
var at = atomic.Pointer[time.Time]{}
func main() {
t := time.Now()
at.Store(&t)
t = *at.Load()
fmt.Println(t)
}
Playground: https://go.dev/play/p/KwTMgvJIenx?v=gotip
Note that in Go 1.19 you can also use atomic.Int64. The advantage of these atomic types instead of top-level functions is that it's more fool-proof; it's impossible to access the value non-atomically as it's hidden behind the struct.

Parsing a time with the format HHMMSS00

I'm working with some data from multiple sources and one of these sources is a Sage ERP system.
I am trying to reference two files in Sage in particular, an audit date and audit time (AUDTDATE and AUDTTIME).
I need to parse this and store it as a DATETIME in a Microsoft SQL Server database.
Currently, I am just trying to figure out the best way to parse this.
An example of what the data might look like is below:
+----------+----------+
| AUDTDATE | AUDTTIME |
+----------+----------+
| 20170228 | 5013756 |
+----------+----------+
AUDTDATE is a yyyymmdd format and the AUDTTIME is HHMMSS00.
So I tried the below as a test:
func main() {
value := "20170228 5013756"
layout := "20060102 15040500"
t, _ := time.Parse(layout, value)
fmt.Println(t)
}
This doesn't work, it just returns 0001-01-01 00:00:00 +0000 UTC when run.
If I change the time to this 050137 and the layout to 150405 then this works fine:
func main() {
value := "20170228 050137"
layout := "20060102 150405"
t, _ := time.Parse(layout, value)
fmt.Println(t)
}
One way that I can think of to deal with this is to strip the milliseconds off from the end and then check the length and add a zero to the beginning if it needs one.
This seems like a pretty ugly solution and would involve doing something like this:
func main() {
date := "20170228"
timeString := "5013756"
value := date + prepareTime(timeString)
layout := "20060102150405"
t, _ := time.Parse(layout, value)
fmt.Println(t)
}
func prepareTime(time string) string {
if len(time) == 7 {
time = "0" + time
}
return time[:6]
}
Is there a way to do this without going through the above? Perhaps natively with the time package?
Assuming that you're pulling back 2 separate values from the DB, you can use fmt.Sprintf to 0 pad timeString. Combining it with the date string, you can use the following:
value := fmt.Sprintf("%s %08s", date, timeString)[:15]
In your code:
func main() {
date := "20170228"
timeString := "5013756"
value := fmt.Sprintf("%s %08s", date, timeString)[:15]
layout := "20060102 150405"
t, _ := time.Parse(layout, value)
fmt.Println(t)
}
Results:
2017-02-28 05:01:37 +0000 UTC
This approach is useful because it will also correctly pad any shorter value of time, e.g. 13756 will be converted to 00013756.
The fmt.Sprintf function is useful to format arguments into a string using the formatting you desire as specified by a format string and a list of arguments (...interface{}). The format string tells the function how to render the arguments.
This format string uses two items of note:
String verb (%s): The format string uses a variety of verbs that are used for string substitutions. %s is specifically to render a string or a slice. Other popular verbs include %d for base 10 integer and %f for float with a complete list in the docs. The %v verb is very useful can also be used here as it will render an argument's default value.
0 left padding: To 0 left pad an argument, use 0 followed by a length number in the verb after the %. This will prepended the argument with a maximum number of 0s specified in the length number. For example, %08s will render a string with up to 8 prepended zeros. This means a string "" will be "00000000" while a string "1234567" will result in "01234567". If the string is longer than the length, nothing will be prepended.
From the documentation:
%s the uninterpreted bytes of the string or slice
0 pad with leading zeros rather than spaces;
for numbers, this moves the padding after the sign
More detailed is available in the documentation: https://golang.org/pkg/fmt/

Go: How to parse only date to time.Time?

I want to parse only date value to time.Time.
For example I have date in this format: 2016-03-31, and I want to parse it, like: time.Parse(FORMAT, "2016-03-31").
But it always fail.
What is the correct format string to use to parse only date with this format?
I have the code below as example, it is on playground also: https://play.golang.org/p/0MNLr9emZd
package main
import (
"fmt"
"time"
)
var dateToParse = "2016-03-31"
func main() {
format := "2006-12-01"
parseDate(format)
}
func parseDate(format string) {
t, err := time.Parse(format, dateToParse)
if err != nil {
fmt.Println("Format:", format)
fmt.Println(err)
fmt.Println("")
return
}
fmt.Println("Works Format:", format)
fmt.Println(t)
fmt.Println("")
}
The output is this:
Format: 2006-12-01
parsing time "2016-03-31" as "2006-12-01": cannot parse "-31" as "2"
Package time
These are predefined layouts for use in Time.Format and Time.Parse.
The reference time used in the layouts is the specific time:
Mon Jan 2 15:04:05 MST 2006
which is Unix time 1136239445. Since MST is GMT-0700, the reference
time can be thought of as
01/02 03:04:05PM '06 -0700
To define your own format, write down what the reference time would
look like formatted your way; see the values of constants like ANSIC,
StampMicro or Kitchen for examples.
Use format := "2006-01-02" for yyyy-mm-dd.
The new format DateOnly = "2006-01-02" of format.go will be added in the Go next release (1.20) per proposal time: add DateTime, DateOnly, TimeOnly format constants and commit
time.Parse(time.DateOnly, dateToParse)

parse time string type back to time type error

package main
import "fmt"
import "time"
func main() {
source := "2014-04-22 23:41:12.518845115 +0800 CST"
Form := "2014-04-22 23:41:12.518845115 +0800 CST"
t, err := time.Parse(Form, source)
if err == nil {
fmt.Println(t.String())
} else {
fmt.Println(err)
}
}
Error :parsing time "2014-04-22 23:41:12 +0800 CST": month out of range
I get source by time.Now().String(), but I could not convert it back. What's wrong with this piece of code?
From the documentation:
Parse parses a formatted string and returns the time value it
represents. The layout defines the format by showing how the reference
time,
Mon Jan 2 15:04:05 -0700 MST 2006 would be interpreted if it were the
value; it serves as an example of the input format. The same
interpretation will then be made to the input string. Predefined
layouts ANSIC, UnixDate, RFC3339 and others describe standard and
convenient representations of the reference time. For more information
about the formats and the definition of the reference time, see the
documentation for ANSIC and the other constants defined by this
package.
(Bolding mine).
So what you want is
Form := "2006-01-02 15:04:05.000000000 -0700 MST"
Which is the date listed in that quote in the format of your input string. One thing to note while I was writing this on the playground to confirm is that it looks like on the part 05.000000000 (the seconds and fractions of seconds) you need the format string to contain exactly as many decimal points as the string you want to parse.
Here's a playground version showing it works: http://play.golang.org/p/dRniJbqgl7

Resources