dongxiang7276 2016-12-24 12:22 采纳率: 0%
浏览 260
已采纳

TimeZone字符串格式

How can I translate this code from Python to Go?

>>> from time import gmtime
>>> from time import strftime
>>> strftime("%z", gmtime())
'-0800'

I tried:

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Now().Local().Format("-0700"))
}

But it's obviously not working:

+0000

I am somewhat confused in general about the formatting part of time because the const symbols (e.g. stdNumTZ) are not exported from the time package.

Is time.Now().Local() in Go the real substitute for gmtime.gmtime() in Python?

  • 写回答

1条回答 默认 最新

  • douzhao6584 2016-12-24 13:45
    关注

    You don't need to call Local() there since time.Now() returns local time. When I try to run your code on my machine, I get the correct result. It looks like the time zone where you're running this is UTC. (You can see the time zone by printing the result of time.Now().Zone()).

    gmtime in Python returns UTC time. In Go, you can convert a time to UTC by calling In(time.UTC) on the time value. (I'm not a Python expert, and I don't know why the time zone of gmtime() is reported as the local time zone (I reproduced that on my machine).)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部