首页 > 其他分享 >Go - Representing Time Zones

Go - Representing Time Zones

时间:2023-10-05 16:23:16浏览次数:32  
标签:UTC 00 Singapore Time Zones location time Go Representing

Problem: You want to include the time zone information in a Time struct.


Solution: The Time struct includes a Location , which is the representation of the time zone.

 

A time zone is an area that follows a standard time that roughly follows longitude but in practice tends to follow political boundaries. All time zones are defined as offsets of the Coordinated Universal Time (UTC), ranging from UTC - 12:00 to UTC+14:00.

The Location struct in the time package represents a time zone. Go’s time package, like many other libraries in different programming languages, uses the time zone database managed by the Internet Assigned Numbers Authority (IANA).

This database, also known as tz or zoneinfo , contains data for many locations around the world, and the latest as of this writing is 28 March 2023. The naming convention for time zones in the tz database is in the form of Area/Location , for example Asia/Singapore or America/New_York .

There are a few ways to create a Location struct. First, you can use LoadLocation (loading the location from the tz database):

func   main ()   { 
      location ,   err   :=   time . LoadLocation ( "Asia/Singapore" ) 
      if   err   !=   nil   { 
          log . Println ( "Cannot  load  location:" ,   err ) 
      } 
      fmt . Println ( "location:" ,   location ) 
      utcTime   :=   time . Date ( 2009 ,   time . November ,   10 ,   23 ,   0 ,   0 ,   0 ,   time . UTC ) 
      fmt . Println ( "UTC  time:" ,   utcTime ) 
      fmt . Println ( "equivalent  in  Singapore:" ,   utcTime . In ( location )) 
}

This is what you should see:

location:  Asia/Singapore
UTC  time:  2009 - 11 - 10  23:00:00  +0000  UTC
equivalent  in  Singapore:  2009 - 11 - 11  07:00:00  +0800  +08

You can see that the name of the time zone is just 08 instead of Asia/Singapore . Lo⁠adLoc⁠ati⁠on simply loads the location from the tz database that’s in the computer it’s running on. If you want to use different data, you can use the LoadLocationFromTZData function instead.

Another way of creating a Location is to use the FixedZone function. This allows you to create any location you want (without being in the tz database) and also name it whatever you want:

func   main ()   { 
      location   :=   time . FixedZone ( "Singapore  Time" ,   8 * 60 * 60 ) 
      fmt . Println ( "location:" ,   location ) 
      utcTime   :=   time . Date ( 2009 ,   time . November ,   10 ,   23 ,   0 ,   0 ,   0 ,   time . UTC ) 
      fmt . Println ( "UTC  time:" ,   utcTime ) 
      fmt . Println ( "equivalent  in  Singapore:" ,   utcTime . In ( location )) 
}

This is what you should see:

location: Singapore Time
UTC time: 2009 - 11 - 10 23:00:00 +0000 UTC
equivalent in Singapore: 2009 - 11 - 11 07:00:00 +0800 Singapore Time

As you can see, the name of the time zone is now Singapore Time.

 

标签:UTC,00,Singapore,Time,Zones,location,time,Go,Representing
From: https://www.cnblogs.com/zhangzhihui/p/17743479.html

相关文章

  • MongoDBHelper + Expression+ JsonResult
    usingMongoDB.Driver;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Linq.Expressions;namespaceMongodbTest.Common{///<summary>///MongoDb帮助类///</summary>publicclassMongoDbHelper......
  • vue-router.esm.js:2065 Uncaught (in promise) Error: Redirected when going from "
    原因:  vue-router路由版本更新产生的问题,导致路由跳转失败抛出该错误;真正的原因是由于返回了一个Promise对象,正常的跳转由then方法执行当正常的路由跳转,被"路由导航守卫"拦截并重新指定路由时,由于this.$router.push()返回的是Promise对象,此时then方法不能正常执......
  • kubelet.go 2466 Error getting node not found
    kubeadm初始化kubernetes集群报错,kubelet服务提示:kubelet.go:2466]"Errorgettingnode"err="node\"k8s3-master\"notfound处理记录。  0.ENV kubernetes1.22.x/1.23.x/1.24.x(不限于所列版本)CentOS7.x/Ubuntu22.04(不限于所列版本)  1.问题现象 kube......
  • Go - Decoding Data with a Customized Binary Format to Structs
    Problem: Youwanttodecodethecustomizedbinaryformatbacktostructs.Solution: Usetheencoding/binarypackagetotakedatafromthebinaryformatand reconstructstructsfromit. funcmain(){vardataMeterfile,err......
  • Go - Encoding Data to a Customized Binary Format
    Problem: Youwanttoencodestructdatatoacustomizedbinaryformat.Solution: Designyourcustomizedformatandusetheencoding/binarypackagetowritedatainstructstoit. Usinggobhasacoupleofdrawbacks.First,gobissupportedbyGoonlya......
  • Go - Decoding gob Format Data to Structs
    Problem: Youwanttodecodegobformatdatabacktostructs.Solution: Usetheencoding/gobpackagetodecodethegobformatdatabacktostructs. funcread(datainterface{},filenamestring){file,err:=os.Open(&quo......
  • Go - Encoding Data to gob Format Data
    Problem: Youwanttoencodestructsintobinarygobformat.Solution: Usetheencoding/gobpackagetoencodethestructsintobytesthatcanbestoredorsentelsewhere. Theencoding/gobpackageisaGolibrarytoencodeanddecodeabinaryformat.The......
  • 工具 | 极其方便的谷歌翻译软件 Myna for Google Translate for Mac | Mac
    工具|极其方便的谷歌翻译软件MynaforGoogleTranslateforMac|Mac前言Mac哪款翻译软件好用呢?市面有太多的翻译工具了,如:百度、谷歌、有道等等。但是不得不说作为对外交流学习或学术阅览,谷歌翻译算得上是比较专业和让人信赖的。而MynaforGoogleTranslateforMac是......
  • MongoDB高阶特性:副本集、分片、事务、索引
    一、副本集(主从复制)1、docker-compose.ymlversion:'3'services:mongo1:image:mongocontainer_name:mongo1command:mongod--replSetrs0--port27017volumes:-./mongodb-cluster/mongod1:/data/dbports:-"27017:2......
  • 2023-10-04:用go语言,现有一棵无向、无根的树,树中有 n 个节点,按从 0 到 n - 1 编号 给你
    2023-10-04:用go语言,现有一棵无向、无根的树,树中有n个节点,按从0到n-1编号给你一个整数n和一个长度为n-1的二维整数数组edges,其中edges[i]=[ai,bi]表示树中节点ai和bi之间存在一条边。每个节点都关联一个价格。给你一个整数数组price,其中price[i]是第i......