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

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.

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

Get a list of events before now, and a list of events after. The events after you can present as they are, the events before you can add multiples of 7 days to their original time till they are after today and present (probably saving them for future ref too).

Another approach would be to have a sweeper which runs every day on a schedule (your ticking timer) which increments old events by 7 days from the day just past. You might still have to check when presenting if they were already past or too close to get to for the user if happening today.