计算当前时间所属的15分钟时间段,使用go语言。

huangapple go评论115阅读模式
英文:

Calculate the 15 min time bin the current time falls into using go

问题

所以我不知道如何计算当前时间的15分钟时间段。
一天有1440分钟。所以有96个15分钟的时间段。那么我如何在golang中计算时间段?

  1. func getCurrentMinutes(current time.Time) (int, error) {
  2. min, err := strconv.Atoi(current.Format("04"))
  3. if err != nil {
  4. return 0, err
  5. }
  6. return min, nil
  7. }
  8. func GetTimeBin(current time.Time, binDuration float64) float64 {
  9. min, _ := getCurrentMinutes(current)
  10. bin := float64(min) / binDuration
  11. return math.Ceil(bin)
  12. }

我上面的实现是错误的,因为我考虑了一个小时内的15分钟时间段。我需要找到当前时间在一天中的15分钟时间段。

提前感谢!

英文:

So I am unaware on how to calculate the 15 min time bin for the current time.
A day has 1440 minutes. So 96 - 15 min bins. So how can i calculate the time bin in golang?

  1. func getCurrentMinutes(current time.Time) (int, error) {
  2. min, err := strconv.Atoi(current.Format("04"))
  3. if err != nil {
  4. return 0, err
  5. }
  6. return min, nil
  7. }
  8. func GetTimeBin(current time.Time, binDuration float64) float64 {
  9. min, _ := getCurrentMinutes(current)
  10. bin := float64(min) / binDuration
  11. return math.Ceil(bin)
  12. }

The above implementation I have done is wrong as I am considering 15 min bins for an hour. I need to find the 15 min bin for the current time in the context of the day.

Thanks in advance!

答案1

得分: 0

这是我想到的一个工作示例。

  1. func GetTimeBin(current time.Time, binDuration float64) float64 {
  2. hours := current.Hour()
  3. min := current.Minute() + hours*60
  4. bin := float64(min) / binDuration
  5. return math.Ceil(bin)
  6. }

这段代码的作用是根据给定的当前时间和时间间隔,计算时间所属的时间段。具体实现是将当前时间转换为分钟数,然后除以时间间隔,最后向上取整得到时间段。

英文:

This is the working example I came up with.

  1. func GetTimeBin(current time.Time, binDuration float64) float64 {
  2. hours := current.Hour()
  3. min := current.Minute() + hours*60
  4. bin := float64(min) / binDuration
  5. return math.Ceil(bin)
  6. }

huangapple
  • 本文由 发表于 2022年2月13日 12:48:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/71098001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定