Convert time to Timeval

Suppose i receive time as string into ‘ServTime’ and parse it as below.

layout := time.RFC3339
t, _ := time.Parse(layout, ServTime.(string))

How do i go about setting this new time using

func Settimeofday(tv *Timeval) (err error)

So my question is what is the best or desired format to convert string to a Timeval ?
Correct if i am wrong here.


Assuming the Timeval is like the C systemcall struct, it takes seconds since 1970 and microseconds. On the Go time.Time struct you have Unix() which gives you the seconds and UnixNano() which gives you nanoseconds. The latter you will need to divide and mod to only get the subsecond part. Then you fill in the corresponding values in the timeval.

Edit: Oh, I see the struct. So, something line this, possibly (untested):

t := ... // a time.Time
tv := syscall.Timeval {
    Sec:  t.Unix(), // might be int32 on 32 bit architectures
    Usec: int32(t.UnixNano() / 1000 % 1000),

I’m trying to print it in a readable form and not able to get around it.
Is there a way to format printing syscall.Timeval ?

It contains two integers. What do you consider a readable form?

I get that. Sorry i didn’t make myself clear. I was hoping something similar to say ‘time.Time.String()’ which displays in date/time format that is easily readable. Anyway I was missing on the below part.

Usec: int32(t.UnixNano() / 1000 % 1000)

Your post helped realize that. Thanks @calmh

To format dates as strings, the time.Time is better. Use that. :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.