Error parsing time in Go with variable number of microseconds - go

I'm trying to parse a string into a time object. The issue is that the number of digits in the microseconds term changes, which breaks the parsing. For example, this works fine:
package main
import (
"fmt"
"time"
)
func main() {
timeText := "2017-03-25T10:01:02.1234567Z"
layout := "2006-01-02T15:04:05.0000000Z"
t, _ := time.Parse(layout, timeText)
fmt.Println(t)
}
But this causes an error, because the number of microseconds digits doesn't match the layout:
package main
import (
"fmt"
"time"
)
func main() {
timeText := "2017-03-25T10:01:02.123Z" // notice only 3 microseconds digits here
layout := "2006-01-02T15:04:05.0000000Z"
t, _ := time.Parse(layout, timeText)
fmt.Println(t)
}
How do I fix this so that the microseconds term is still parsed, but it doesn't matter how many digits there are?

Use 9s instead of zeros in the subsecond format, for example:
timeText := "2017-03-25T10:01:02.1234567Z"
layout := "2006-01-02T15:04:05.99Z"
t, _ := time.Parse(layout, timeText)
fmt.Println(t) //prints 2017-03-25 10:01:02.1234567 +0000 UTC
From the docs:
// Fractional seconds can be printed by adding a run of 0s or 9s after
// a decimal point in the seconds value in the layout string.
// If the layout digits are 0s, the fractional second is of the specified
// width. Note that the output has a trailing zero.
do("0s for fraction", "15:04:05.00000", "11:06:39.12340")
// If the fraction in the layout is 9s, trailing zeros are dropped.
do("9s for fraction", "15:04:05.99999999", "11:06:39.1234")

Related

need more input or information regarding binary.write error invalid type xxx

I'm trying to write protobuf *Timestamp.timestamp to binary, and the error I got is invalid type *Timestamp.timestamp and I've tried to no avail, can anyone point me to some direction? thanks!
package main
import (
"bytes"
"encoding/binary"
"fmt"
google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
"time"
)
func main() {
buff := new(bytes.Buffer)
ts := &google_protobuf.Timestamp{
Seconds: time.Now().Unix(),
Nanos: 0,
}
err := binary.Write(buff, binary.LittleEndian, ts)
if err != nil {
panic(err)
}
fmt.Println("done")
}
can anyone point me to some direction?
Read the error message.
binary.Write: invalid type *timestamp.Timestamp
Read the documentation for binary.Write and timestamp.Timestamp.
Package binary
import "encoding/binary"
func Write
func Write(w io.Writer, order ByteOrder, data interface{}) error
Write writes the binary representation of data into w. Data must be a
fixed-size value or a slice of fixed-size values, or a pointer to such
data. Boolean values encode as one byte: 1 for true, and 0 for false.
Bytes written to w are encoded using the specified byte order and read
from successive fields of the data. When writing structs, zero values
are written for fields with blank (_) field names.
package
timestamp
import "github.com/golang/protobuf/ptypes/timestamp"
type
Timestamp
type Timestamp struct {
// Represents seconds of UTC time since Unix epoch
// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
// 9999-12-31T23:59:59Z inclusive.
Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"`
// Non-negative fractions of a second at nanosecond resolution. Negative
// second values with fractions must still have non-negative nanos values
// that count forward in time. Must be from 0 to 999,999,999
// inclusive.
Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
A Timestamp represents a point in time independent of any time zone or
calendar, represented as seconds and fractions of seconds at
nanosecond resolution in UTC Epoch time. It is encoded using the
Proleptic Gregorian Calendar which extends the Gregorian calendar
backwards to year one. It is encoded assuming all minutes are 60
seconds long, i.e. leap seconds are "smeared" so that no leap second
table is needed for interpretation. Range is from 0001-01-01T00:00:00Z
to 9999-12-31T23:59:59.999999999Z. By restricting to that range, we
ensure that we can convert to and from RFC 3339 date strings. See
https://www.ietf.org/rfc/rfc3339.txt.
As the error message says: *timestamp.Timestamp is not a fixed-size value or a slice of fixed-size values, or a pointer to such data.
To confirm that, comment out the XXX_unrecognized variable-sized field; there is no error.
package main
import (
"bytes"
"encoding/binary"
"fmt"
"time"
)
// https://github.com/golang/protobuf/blob/master/ptypes/timestamp/timestamp.pb.go
type Timestamp struct {
// Represents seconds of UTC time since Unix epoch
// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
// 9999-12-31T23:59:59Z inclusive.
Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"`
// Non-negative fractions of a second at nanosecond resolution. Negative
// second values with fractions must still have non-negative nanos values
// that count forward in time. Must be from 0 to 999,999,999
// inclusive.
Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
// XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func main() {
buff := new(bytes.Buffer)
ts := &Timestamp{
Seconds: time.Now().Unix(),
Nanos: 0,
}
err := binary.Write(buff, binary.LittleEndian, ts)
if err != nil {
panic(err)
}
fmt.Println("done")
}
Playground: https://play.golang.org/p/Q5NGnO49Dsc
Output:
done

string to big Int in Go?

Is there a way to convert a string (which is essentially a huge number) from string to Big int in Go?
I tried to first convert it into bytes array
array := []byte(string)
Then converting the array into BigInt.
I thought that worked, however, the output was different than the original input. So I'm guessing the conversion didn't do the right thing for some reason.
The numbers I'm dealing with are more than 300 digits long, so I don't think I can use regular int.
Any suggestions of what is the best approach for this?
Package big
import "math/big"
func (*Int) SetString
func (z *Int) SetString(s string, base int) (*Int, bool)
SetString sets z to the value of s, interpreted in the given base, and
returns z and a boolean indicating success. The entire string (not
just a prefix) must be valid for success. If SetString fails, the
value of z is undefined but the returned value is nil.
The base argument must be 0 or a value between 2 and MaxBase. If the
base is 0, the string prefix determines the actual conversion base. A
prefix of “0x” or “0X” selects base 16; the “0” prefix selects base 8,
and a “0b” or “0B” prefix selects base 2. Otherwise the selected base
is 10.
For example,
package main
import (
"fmt"
"math/big"
)
func main() {
n := new(big.Int)
n, ok := n.SetString("314159265358979323846264338327950288419716939937510582097494459", 10)
if !ok {
fmt.Println("SetString: error")
return
}
fmt.Println(n)
}
Playground: https://play.golang.org/p/ZaSOQoqZB_
Output:
314159265358979323846264338327950288419716939937510582097494459
See Example for string to big int conversion.
package main
import (
"fmt"
"log"
"math/big"
)
func main() {
i := new(big.Int)
_, err := fmt.Sscan("18446744073709551617", i)
if err != nil {
log.Println("error scanning value:", err)
} else {
fmt.Println(i)
}
}
Output:
18446744073709551617

How to convert a string time with milliseconds (hh:mm:ss.xxx) to time.Time?

Basically I have times like this one as a string:
15:56:36.113
I want to convert it to time.Time.
From what I am reading I cannot use milliseconds when using time.Parse().
Is there another way to convert my string to time.Time ?
Package time
Format Reference Time
A decimal point followed by one or more zeros represents a fractional
second, printed to the given number of decimal places. A decimal point
followed by one or more nines represents a fractional second, printed
to the given number of decimal places, with trailing zeros removed.
When parsing (only), the input may contain a fractional second field
immediately after the seconds field, even if the layout does not
signify its presence. In that case a decimal point followed by a
maximal series of digits is parsed as a fractional second.
For example,
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse("15:04:05", "15:56:36.113")
if err != nil {
fmt.Println(err)
}
fmt.Println(t)
fmt.Println(t.Format("15:04:05.000"))
h, m, s := t.Clock()
ms := t.Nanosecond() / int(time.Millisecond)
fmt.Printf("%02d:%02d:%02d.%03d\n", h, m, s, ms)
}
Output:
0000-01-01 15:56:36.113 +0000 UTC
15:56:36.113
15:56:36.113
Note: The zero value of type Time is 0000-01-01 00:00:00.000000000 UTC.
package main
import (
"fmt"
"time"
)
func main() {
s := "15:56:36.113"
t,_ := time.Parse("15:04:05.000", s)
fmt.Print(t)
}
Output:
0000-01-01 15:56:36.113 +0000 UTC
You can play with it more here: https://play.golang.org/p/3A3e8zHQ8r

Generating Random Timestamps in Go

I'd like to generate a random timestamp within the last relative 3 years and have it be printed out with this format: %d/%b/%Y:%H:%M:%S %z
Here is what I have right now:
package main
import (
"strconv"
"time"
"math/rand"
"fmt"
)
func randomTimestamp() time.Time {
randomTime := rand.Int63n(time.Now().Unix() - 94608000) + 94608000
randomNow, err := time.Parse("10/Oct/2000:13:55:36 -0700", strconv.FormatInt(randomTime, 10))
if err != nil {
panic(err)
}
return randomNow
}
func main() {
fmt.Println(randomTimestamp().String())
}
This always throws: panic: parsing time "...": month out of range. How can I generate a random timestamp for a given range, then convert it to the string format I want with the standard library?
Don't use time.Parse. You have a Unix time, not a time string. Use the Unix() method instead. https://golang.org/pkg/time/#Unix. You can also choose a minimum time value, say 1/1/1900 and add a random Duration of seconds to the time using the Add method on Time and passing a Duration you made with the Ticks() method. https://golang.org/pkg/time/#Duration
Here's a Go Playground link. Just remember that the Go Playground doesn't support actual randomness. https://play.golang.org/p/qYTpnbml_N
package main
import (
"time"
"math/rand"
"fmt"
)
func randomTimestamp() time.Time {
randomTime := rand.Int63n(time.Now().Unix() - 94608000) + 94608000
randomNow := time.Unix(randomTime, 0)
return randomNow
}
func main() {
fmt.Println(randomTimestamp().String())
}

why does time.Parse parse the time incorrectly?

I'm trying to parse a string as time with but unfortunately go gets the wrong month (January instead of June)
package main
import "fmt"
import "time"
func main() {
t := "2014-06-23T20:29:39.688+01:00"
tc, _ := time.Parse("2006-01-02T15:04:05.000+01:00", t)
fmt.Printf("t was %v and tc was %v", t, tc)
}
Play
The problem is that your timezone offset is ill-defined in the layout: the reference offset is -0700. You defined yours as +01:00, so the 01 is interpreted as the month and erase the previously defined one. And as your working offset is 01 as well, it is parsed as january.
The following example works for me playground
package main
import "fmt"
import "time"
func main() {
t := "2014-06-23T20:29:39.688+01:00"
tc, _ := time.Parse("2006-01-02T15:04:05.000-07:00", t)
fmt.Printf("t was %v and tc was %v", t, tc)
}
Your layout string is incorrect. The numbers in the layout string have special meanings, and you are using 1 twice: once in the month portion and once in the time zone portion. The time zone in the string you are parsing is 01:00, so you are storing 1 into the month. This explains why the returned month was January (the first month).
A corrected layout string is 2006-01-02T15:04:05.000-07:00. Or, if you're happy with using Z to represent UTC, the time.RFC3339 constant might be appropriate.

Resources