[Golang] processing region in Go

 

 

 Many novice developers confused when dealing with time zones.

  • How they are stored in the database
  • How to resolve them in Go

When in the database, always follow a standard time zone storage area, the ideal approach is to save time UTC, and displayed according to the time zone needs to be translated into various time zones.

As an example in storage time MYSQL

The following solution has nothing to do with the DB. According to the MySQL documentation, there are two methods may be stored in a MySQL time.

  • DATETIME --DATETIME the type used to contain a date and time value portion. And to retrieve MYSQL DATETIME 'YYYY-MM-DD hh: mm: ss' format value. The supported range is '1000-01-01 00:00:00'to '9999-12-31 23:59:59'.
  • TIMESTAMP -TIMESTAMP for simultaneous data type contains the date and time value portion. UTC  TIMESTAMPrange is UTC.'1970-01-01 00:00:01''2038-01-19 03:14:07'。

In this article, I will use DATETIME example.

Now, the other most important thing is to read and convert it to a different time zone.

Go time zone conversion

The following code shows how we can do is convert the time zone in the Go language. First, let's define dictionary locale and time zone. The district can get from here to identify time zone list the IANA Time Zones ,

Package main 

Import ( 
    " FMT " 
    " errors " 
    " Time " 
) 

type Country String 

const ( 
    Germany Country = " Germany " 
    UnitedStates Country   = " United States " 
    China Country = " China " 

) 

// timeZoneID country => IANA standard time zone identifier the key character dictionary 
var timeZoneID = Map [Country] String { 
    Germany:       " Europe / Berlin " ,
    UnitedStates: " America / Los_Angeles " , 
    China:    " Asia / of Shanghai " , 
} 

// time zone identifiers acquired IANA 
FUNC (Country C) the TimeZoneID () ( String , error) {
     IF ID, OK: = timeZoneID [C]; OK {
         return ID, nil 
    } 
    return  "" , errors.New ( " timeZone ID Not found for Country " ) 
} 

// Get the time tz time zone identifier formatting characters 
FUNC TimeIn (T time.time, tz, the format String ) String { 
    
    //HTTPS: /golang.org/pkg/time/#LoadLocation loads LOCATION ON
     // loading zone 
    LOC, ERR: = time.LoadLocation (TZ)
     IF ERR =! nil {
         // handle error 
    }
     // when formatting the acquired specified zone time string 
    return t.In (LOC) .Format (the format) 
} 

FUNC main () { 
    // Get the time zone U.S. structural body 
    TZ, ERR: = UnitedStates.TimeZoneID ()
     IF ! ERR = nil {
         // handle error 
    }
     // formatted time zone U.S. 
    usTime: = TimeIn (Time.now (), TZ, time.RFC3339) 

    fmt.Printf ( "Time in %s: %s",
        UnitedStates,
        usTime,
    )
}

Go language time.LoadLocation possible pit

As stated in the standard library documentation:

The time zone database needed by LoadLocation may not be present on all systems, especially non-Unix systems. LoadLocation looks in the directory or uncompressed zip file named by the ZONEINFO environment variable, if any, then looks in known installation locations on Unix systems, and finally looks in $GOROOT/lib/time/zoneinfo.zip.

LoadLocation required time zone database may not exist on all systems, particularly non-unix system, if any, LoadLocation find ZONEINFO environment variable named by the directory or uncompressed  ZONEINFO environment variable named zip file, and then look on Unix systems known installation location, final look  $GOROOT/lib/time/ ZONEINFO .zip.

Docker Go language to use time zone

Already exists. But in case you compile and deploy docker use of multi-stage-docker Alpine image. You can manually use the following command to add the data area when the time zone information in file Go installed by default.

RUN apk add tzdata

This will add to the time zone information alpine mirror  /usr/share/timezone. But do not forget to set the environment variable  ZONEINFO value /usr/share/timezone

ZONEINFO=/usr/share/timezone

Here is an example of a reference Dockerfile

FROM golang:1.12-alpine as build_base
RUN apk add --update bash make git
WORKDIR /go/src/github.com/your_repo/your_app/
ENv GO111MODULE on
COPY go.mod .
COPY go.sum .
RUN go mod download

FROM build_bash AS server_builder
COPY . ./
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/your_app

FROM alpine
RUN apk add tzdata

# 自定义运行阶段的命令

Examples

You can Playground Go  https://play.golang.org/p/UCKSpIWmiX7 View full example

 

Guess you like

Origin www.cnblogs.com/landv/p/11582640.html