how to convert time from unix int64 to local int64

6,094 views
Skip to first unread message

dlin

unread,
Apr 13, 2012, 5:30:31 AM4/13/12
to golan...@googlegroups.com
I got unix time from in UTC second, in the sample code it is 1334289777.
I wish to plot in 'gnuplot'.
I require to convert it into local time.

What's the correct method?

package main

import "fmt"
import "time"

func main() {
t := time.Unix(1334289777, 0)
fmt.Println(t.Local())            // 2012-04-13 12:02:57 +0800 CST
fmt.Println(t.Local().Unix()) // 1334289777
fmt.Println(t.UTC()) // 2012-04-13 04:02:57 +0000 UTC
fmt.Println(t.Unix()) // 1334289777
}


roger peppe

unread,
Apr 13, 2012, 7:59:34 AM4/13/12
to dlin, golan...@googlegroups.com
On 13 April 2012 10:30, dlin <dli...@gmail.com> wrote:
> I got unix time from in UTC second, in the sample code it is 1334289777.
> I wish to plot in 'gnuplot'.
> I require to convert it into local time.
>
> What's the correct method?

> t := time.Unix(1334289777, 0)

this is all you need to do.

dlin

unread,
Apr 15, 2012, 10:12:44 PM4/15/12
to golan...@googlegroups.com, dlin
Rog,  
I think I explain my problem in wrong way.  Let me try again.

My time zone is UTC+8.

I export '1334289777' to gnuplot for plot the time.(It is time.Unix())
But, in fact I require '1334289777 + 8*60*60'  to mapping my correct timezone.

So, my problem is how to export correct 'seconds' in local time.
(I don't want write a const 8*60*60).

Assume 
  t := time.Unix(1334289777, 0)
I guess the answer maybe
  t.Unix() + time.XXX()  //   to fix the time zone second

On Friday, April 13, 2012 7:59:34 PM UTC+8, rog wrote:

andrey mirtchovski

unread,
Apr 15, 2012, 11:04:51 PM4/15/12
to dlin, golan...@googlegroups.com
> But, in fact I require '1334289777 + 8*60*60'  to mapping my correct
> timezone.

no, you do not. that would be time in the future, not time converted
to UTC. the "seconds elapsed since January 1, 1970 UTC" (what t.Unix
returns) is the same for two identical times regardless of which time
zone they reference (provided you're not moving close to the speed of
light).

time.Unix(1334289777, 0) is one second earlier than
time.Unix(1334289778, 0) everywhere in the world.

dlin

unread,
Apr 15, 2012, 11:37:06 PM4/15/12
to golan...@googlegroups.com, dlin
aam,

What's I want is to export the number (epoch second) into gnu-plot.

So, I require to export the localtime epoch second.
I require to know how many second I should add into the t.Unix() according to timezone.

andrey mirtchovski

unread,
Apr 15, 2012, 11:43:25 PM4/15/12
to dlin, golan...@googlegroups.com
> So, I require to export the localtime epoch second.

again, for a given time t the "seconds since epoch start" are the
same, regardless of which timezone you convert the time to.

dlin

unread,
Apr 16, 2012, 12:05:11 AM4/16/12
to golan...@googlegroups.com, dlin
OK, I understood the output value of t.Unix() is always the same value.

I just want to know how to PRINT OUT a value which is t.Unix() + time-zone-seconds.

andrey mirtchovski

unread,
Apr 16, 2012, 12:35:19 AM4/16/12
to dlin, golan...@googlegroups.com
> I just want to know how to PRINT OUT a value which is t.Unix() +
> time-zone-seconds.

http://play.golang.org/p/FGppm9-kEt

dlin

unread,
Apr 16, 2012, 12:43:54 AM4/16/12
to golan...@googlegroups.com, dlin
Thanks,  it works.

t := time.Now()
name, offset := t.Zone()
fmt.Println(name, offset, t.Unix(), t.Unix()+int64(offset))

// CST 28800 1334551295 1334580095

Stephen Day

unread,
Apr 18, 2012, 11:24:34 AM4/18/12
to golan...@googlegroups.com, dlin
fmt.Println(name, offset, t.Unix(), t.Unix()+int64(offset))
This code may not always be correct nor would I ever recommend localizing an epoch time. The point of an epoch is that its always an offset from the same time. You're just going to cause yourself headaches by trying to do this. Time should only be converted to a local time at the point it is displayed. It might be a good idea to give the gnuplot documentation another look, as gnuplot should handle epoch correctly.
Reply all
Reply to author
Forward
0 new messages