首页 > 其他分享 >Go - Parsing Time Displays Into Structs

Go - Parsing Time Displays Into Structs

时间:2023-10-06 13:22:05浏览次数:31  
标签:will layout zone Into Parsing abbreviation Displays time 2021

 

func   main ()   { 
   str   :=   "4:31am  +0800  on  Oct  1,  2021" 
   layout   :=   "3:04pm  - 0700  on  Jan  2,  2006" 
   t ,   err   :=   time . Parse ( layout ,   str ) 
   if   err   !=   nil   { 
    log . Println ( "Cannot  parse:" ,   err ) 
   } 
   fmt . Println ( t . Format ( time . RFC3339 )) 
}

This is the result:

2021 - 10 - 01T04:31:00+08:00

Earlier you saw that using the wrong numeral for the layout will result in wrong formatting. This happens in Parse as well, except that this time you will be getting an error. Say you change the layout to this:

layout   :=   "3:09pm  - 0700  on  Jan  2,  2006"

Notice that you changed the layout to :09 instead of :04. You will get this error:

2021/10/23  20:46:36  Cannot  parse:  parsing  time  "4:31am  +0800  on  Oct  1,  2021"  as
"3:09pm  - 0700  on  Jan  2,  2006":  cannot  parse  "31am  +0800  on  Oct  1,  2021"  as  ":09"

If you’re using a time zone abbreviation like SGT or EST, it will still be parsed and it will be considered a location. However, the offset will be zero. Yes, you got that right. It will say what you want it to say but totally ignore your intention of using it as the time zone.

Take a look at what this means. If your value string and layout are like this:

str   :=   "4:31am  SGT  on  Oct  1,  2021" 
layout   :=   "3:04pm  MST  on  Jan  2,  2006"

Print the parsed Time struct instance with a few more layouts from the package:

fmt . Println ( t . Format ( time . RFC822 ))    //  "02  Jan  06  15:04  MST" 
fmt . Println ( t . Format ( time . RFC822Z ))   //  "02  Jan  06  15:04  - 0700" 
fmt . Println ( t . Format ( time . RFC3339 ))   //  "2006 - 01 - 02T15:04:05Z07:00"

This is what you get:

01  Oct  21  04:31  SGT
01  Oct  21  04:31  +0000
2021 - 10 - 01T04:31:00Z

As you can see, the abbreviation is printed nicely, and no error is returned but the offset is obviously wrong since SGT is +0800. In fact, in the RFC 3339 layout shows that it is actually in UTC (it shows a Z).

This is something that can easily trip up someone who is not aware or careless because there is an error returned.

Why is it like this? According to the package documentation:
When parsing a time with a zone abbreviation like MST, if the zone abbreviation has a defined offset in the current location, then that offset is used. The zone abbreviation “UTC” is recognized as UTC regardless of location. If the zone abbreviation is unknown, Parse records the time as being in a fabricated location with the given zone abbreviation and a zero offset.
time package documentation
To solve this problem, you should use a numeric offset like +0800 instead of an abbreviation like SGT.

标签:will,layout,zone,Into,Parsing,abbreviation,Displays,time,2021
From: https://www.cnblogs.com/zhangzhihui/p/17744481.html

相关文章

  • Go - Parsing JSON Data Streams Into Structs
    Problem: YouwanttoparseJSONdatafromastream.Solution: CreatestructstocontaintheJSONdata.CreateadecoderusingNewDecoderintheencoding/jsonpackage,thencallDecodeonthedecodertodecodedataintothestructs. InthisfirstJSONf......
  • [EFI]华硕 Asus VivoBook S510UA 电脑 Hackintosh 黑苹果efi引导文件
    此文章资源下载请前往黑果魏叔官网下载硬件型号驱动情况主板AsusVivoBookS510UABQ514T处理器IntelCorei5-8250UKabyLakeR8thGen.i5已驱动内存8GB(三星DDR42400MHz8GB)已驱动硬盘镁光_1100_MTFDDAV512TBN(512GB/固态硬盘)已驱动显卡IntelUHD620已驱动声卡C......
  • Python用于解析和修改文本数据-pyparsing模块教程
    Python库解析地址PyParsing人们普遍认为,Python编程语言的pyparsing模块是对文本数据进行操作的一个宝贵工具。用于解析和修改文本数据的pyparsing包,简化了对地址的操作。这是因为该模块可以转换和帮助解析地址。在这篇文章中,我们将讨论PyParsing模块在处理解析以及修改时的......
  • destoon添加自定义字段报错INSERT INTO [pre]fields
     今天做destoon开发时候在后台添加自定义字段时候出现:destoon7.0-8.0添加自定义字段报错MySQLQuery:INSERTINTO[pre]fields(tb,name,title,note,type,length,html,default_value,option_value,width,height,input_limit,addition,search,display,front)VALUES('article_21',......
  • ClickHouse使用之四 ——外部数据源导入通用方案之insert into select from
    需求:1、在工作中,我们常常需要将外部hive或者mysql、oracle等数据源导入到clickhouse中,对于多种外部数据源,是否有通用的数据导入方案?2、我们在clickhouse上维持一张查询主表,但外部数据源表是hive增量表,新增数据需要同步更新到clickhouse上,是否有不通过第三方组件的插入方式......
  • ORA-00600 kauupd:2 merge into
    alert日志显示如下:ORA-00600:internalerrorcode,arguments:[kauupd:2],[],[],[],[],[],[],[],[],[],[],[]执行的sql类似如下:mergeintotab1ausing(selectcodefromcus1wherecode>1)bon(a.code=b.code)whenmatchedthenupdateseta.total=a.to......
  • mysql insert into on duplicate key update
    新增如果遇到主键冲突,则更新新建一张表,除了主键id,还有唯一健mobilecreatetableexample_user(idint(4)notnullauto_increment,namevarchar(20),mobilevarchar(20),addressvarchar(100),view_countint(4),primarykey(id),uniqu......
  • replace into
    MySQLreplaceinto用法(insertinto的增强版)在向表中插入数据的时候,经常遇到这样的情况:1.首先判断数据是否存在;2.如果不存在,则插入;3.如果存在,则更新。在SQLServer中可以这样处理:ifnotexists(select1fromtwhereid=1)insertintot(id,update_time)valu......
  • MySQL 使用Navicat delete/insert into/update 大量数据表锁死,kill的线程后线程处于ki
      MySQL使用delete/insertinto/update大量数据表锁死,kill的线程后线程处于killed状态问题解决实际生产环境问题描述:使用Navicat备份BigData数据表时不小心点到了取消按钮,导致数据表被锁。  查看MySQL线程队列,找到刚刚执行的SQL看是属于什么状态。showprocessli......
  • ORACLE SELECT INTO 赋值为空,抛出 NO DATA FOUND 异常
    例子:DECLAREORDER_NUMVARCHAR2(20);BEGINSELECTS.ORDER_NUMINTOORDER_NUMFROMSALES_ORDERSWHERES.ID=122344;DBMS_OUTPUT.PUT_LINE('单号:'||ORDER_NUM);END;在查询结果为空的情况下,以上代码会报错:未找到任何数据解决方法:改为SELECTMAX(S.ORDER_NUM)INTO......