dongmu5246 2019-03-06 12:23
浏览 265
已采纳

如何获得一天中的秒数[关闭]

How to get seconds of day (1 - 86400) in Go?

Just like http://joda-time.sourceforge.net/apidocs/org/joda/time/base/AbstractDateTime.html#getSecondOfDay()

Update/Clarifications

  • Wanted equivalent of the joda/SecondOfDay in golang
  • Expected to start at 0 and 86400 at end of the day
  • Required when rewriting in golang a java opensource function which in turn is using joda/secondOfDay
  • Googled '24 hour to seconds' to get 86400
  • While asking question I could only think of now.Unix()-yesterdayMidnight.Unix() and the simple accepted answer didn't come to my mind
  • Obviously didn't think about Daylight Saving
  • Wanted to see if there is some built in function or popular/standard library
  • 写回答

2条回答 默认 最新

  • dongzhanlian6289 2019-03-06 13:19
    关注

    Note: This function returns the nominal number of seconds of day in the (0 - 86399) range. If you're looking for the "number of seconds elapsed since midnight", which may not be in (0 - 86399) range due to daylight saving time, please see @icza's answer.

    Update: Please note also that the question refers to Joda Time implementation, which, according to Joda Time getSecondOfDay on DST switch day, seems to correspond to the nominal number of seconds implementation (as in my answer below) as opposed to the "number of seconds elapsed since midnight" (as in @icza's answer).

    package main 
    
    import (
        "fmt"
        "time"
    )
    
    func getSecondOfDay(t time.Time) int {
        return 60*60*t.Hour() + 60*t.Minute() + t.Second()
    }
    
    func main() {
        t := time.Now()
        fmt.Println(getSecondOfDay(t))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分