LoginSignup
81
54

More than 5 years have passed since last update.

[Go] UTCの時刻を日本時間に変換する

Last updated at Posted at 2013-09-29

日本でローカルで普通に動かすとtime.Now()はJSTになるのですが、Google App Engineの開発サーバーdev_appserver.pyや http://play.golang.org/ ではUTCになります。

以下のようにすればJSTに変換できます。

package main

import (
    "fmt"
    "time"  
)       

func main() {
    now := time.Now()
    fmt.Println(now.Format(time.RFC3339))

    nowUTC := now.UTC() 
    fmt.Println(nowUTC.Format(time.RFC3339))

    // This fails with "panic: unknown time zone Asia/Tokyo" at play.golang.org
    // jst, err := time.LoadLocation("Asia/Tokyo")
    //if err != nil {               
    //  panic(err)
    //}                                 

    jst := time.FixedZone("Asia/Tokyo", 9*60*60)

    nowJST := nowUTC.In(jst)                        
    fmt.Println(nowJST.Format(time.RFC3339))

    // results at local                                 
    // 2013-09-30T02:21:12+09:00                            
    // 2013-09-29T17:21:12Z
    // 2013-09-30T02:21:12+09:00
    //                                                          
    // results at http://play.golang.org/
    // 2009-11-10T23:00:00Z                                         
    // 2009-11-10T23:00:00Z
    // 2009-11-11T08:00:00+09:00                                        
}

func LoadLocation(name string) (*Location, error)も試してみたのですが、play.golang.org ではAsia/Tokyoはunkown time zoneとエラーになってしまいました。

http://play.golang.org/p/-e7unXO1f9 このリンクをクリックするとGo playgroundで試せます。

81
54
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
81
54