dongqiang8058 2015-09-15 17:44 采纳率: 100%
浏览 144
已采纳

为什么不能解析提供的格式表示的时间?

Consider this example:

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Parse(time.RFC3339, time.RFC3339))
}

The output is:

0001-01-01 00:00:00 +0000 UTC parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00

Why can't time.Parse() handle a layout as a value? What's missing here?


UPDATE: Cutting off the time zone value (but not the 'Z' delimiting the time from the zone) fixes it:

fmt.Println(time.Parse(time.RFC3339, "2015-09-15T11:50:00Z"))

Why can't time.Parse() handle time zone info when using time.RFC3339 as the layout string?

http://play.golang.org/p/p3fHfJNHVK


UPDATE: JimB's answer led me to read from RFC3339 and I found these examples that clarify further:

Here are some examples of Internet date/time format.

1985-04-12T23:20:50.52Z

This represents 20 minutes and 50.52 seconds after the 23rd hour of April 12th, 1985 in UTC.

1996-12-19T16:39:57-08:00

This represents 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with an offset of -08:00 from UTC (Pacific Standard Time). Note that this is equivalent to 1996-12-20T00:39:57Z in UTC.

  • 写回答

2条回答 默认 最新

  • douzhi3667 2015-09-15 18:13
    关注

    The time.RFC3339 format is a case where the format string itself isn't a valid time. You can't have a Z and an offset in the time string, but the format string has both because the spec can contain either type of timezone specification.

    Both of these are valid RFC3339 times:

    "2015-09-15T14:00:12-00:00"
    "2015-09-15T14:00:13Z"
    

    And the time package needs to be able to parse them both using the same RFC3339 format string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab