dsyq40772 2017-10-21 09:10
浏览 36
已采纳

使用时间包转换到下一个工作日

I control a service that has the ability to store Weekday -> Time (also in time.Time format) into a MongoDB for events in a community. The problem I am currently having is trying to rollover these weekdays to the following.

Say every Wednesday at 12:00 there is an event. Once the current weekday is Thursday- I need to roll the time object to the following week at 12:00 Wednesday and store it.

There is no ticking timer. When someone attempts to query all of the current events- I would like for it to check if the event has passed, and if so, update to the next one and display the current. If I was using a persistent timer, the rollover would be easy by just adding 7 days to time.AddDate() or time.Date().

So far I've tried converting the weekday to integers (I'm aware time.Weekday() does this already) then performing math to try to get the amount of days. Then using time.Date() to set the next occurrence. Current: The Go Playground

I have a huge headache and I think I am overthinking and missing something super simple. I've checked the time package documentation and nothing really popped out for me. I'm not worried about printing or storing the data, just getting the proper calculations and manipulating the time package.

  • 写回答

2条回答 默认 最新

  • douhuang1852 2017-10-21 18:38
    关注

    Here's an example for getting the next day from now for a weekly event. If this is not what you want, add some test examples to your question.

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func nextWeeklyEvent(t time.Time, weekday time.Weekday, hour, minute int) time.Time {
        days := int((7 + (weekday - t.Weekday())) % 7)
        y, m, d := t.AddDate(0, 0, days).Date()
        return time.Date(y, m, d, hour, minute, 0, 0, t.Location())
    }
    
    func main() {
        now := time.Now().Round(time.Second)
        for i := 0; i < +7; i++ {
            next := nextWeeklyEvent(now, time.Wednesday, 12, 0)
            fmt.Println("now: ", now, now.Weekday())
            fmt.Println("next:", next, next.Weekday())
            fmt.Println()
            now = now.AddDate(0, 0, 1)
        }
    }
    

    Output:

    now:  2017-10-21 14:30:17 -0400 EDT Saturday
    next: 2017-10-25 12:00:00 -0400 EDT Wednesday
    
    now:  2017-10-22 14:30:17 -0400 EDT Sunday
    next: 2017-10-25 12:00:00 -0400 EDT Wednesday
    
    now:  2017-10-23 14:30:17 -0400 EDT Monday
    next: 2017-10-25 12:00:00 -0400 EDT Wednesday
    
    now:  2017-10-24 14:30:17 -0400 EDT Tuesday
    next: 2017-10-25 12:00:00 -0400 EDT Wednesday
    
    now:  2017-10-25 14:30:17 -0400 EDT Wednesday
    next: 2017-10-25 12:00:00 -0400 EDT Wednesday
    
    now:  2017-10-26 14:30:17 -0400 EDT Thursday
    next: 2017-11-01 12:00:00 -0400 EDT Wednesday
    
    now:  2017-10-27 14:30:17 -0400 EDT Friday
    next: 2017-11-01 12:00:00 -0400 EDT Wednesday
    

    Playground: https://play.golang.org/p/m1rYD72-nx

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题