• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ReturnlocalbeginningofdaytimeobjectinGo

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Both the title and the text of the question asked for "a local [Chicago] beginning of today time." The Bod function in the question did that correctly. The accepted Truncate function claims to be a better solution, but it returns a different result; it doesn't return a local [Chicago] beginning of today time. For example,

package main

import (
    "fmt"
    "time"
)

func Bod(t time.Time) time.Time {
    year, month, day := t.Date()
    return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}

func Truncate(t time.Time) time.Time {
    return t.Truncate(24 * time.Hour)
}

func main() {
    chicago, err := time.LoadLocation("America/Chicago")
    if err != nil {
        fmt.Println(err)
        return
    }
    now := time.Now().In(chicago)
    fmt.Println(Bod(now))
    fmt.Println(Truncate(now))
}

Output:

2014-08-11 00:00:00 -0400 EDT
2014-08-11 20:00:00 -0400 EDT

The time.Truncate method truncates UTC time.

The accepted Truncate function also assumes that there are 24 hours in a day. Chicago has 23, 24, or 25 hours in a day.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go学习笔记-基础类型发布时间:2022-07-10
下一篇:
go1.6的vendor不生效原因发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap