ComputeUsagePeriodBounds returns the UTC-aligned start and end bounds for the active usage-limit period containing now.
(now time.Time, period codersdk.ChatUsageLimitPeriod)
| 18 | // ComputeUsagePeriodBounds returns the UTC-aligned start and end bounds for the |
| 19 | // active usage-limit period containing now. |
| 20 | func ComputeUsagePeriodBounds(now time.Time, period codersdk.ChatUsageLimitPeriod) (start, end time.Time) { |
| 21 | utcNow := now.UTC() |
| 22 | |
| 23 | switch period { |
| 24 | case codersdk.ChatUsageLimitPeriodDay: |
| 25 | start = time.Date(utcNow.Year(), utcNow.Month(), utcNow.Day(), 0, 0, 0, 0, time.UTC) |
| 26 | end = start.AddDate(0, 0, 1) |
| 27 | case codersdk.ChatUsageLimitPeriodWeek: |
| 28 | // Walk backward to Monday of the current ISO week. |
| 29 | // ISO 8601 weeks always start on Monday, so this never |
| 30 | // crosses an ISO-week boundary. |
| 31 | start = time.Date(utcNow.Year(), utcNow.Month(), utcNow.Day(), 0, 0, 0, 0, time.UTC) |
| 32 | for start.Weekday() != time.Monday { |
| 33 | start = start.AddDate(0, 0, -1) |
| 34 | } |
| 35 | end = start.AddDate(0, 0, 7) |
| 36 | case codersdk.ChatUsageLimitPeriodMonth: |
| 37 | start = time.Date(utcNow.Year(), utcNow.Month(), 1, 0, 0, 0, 0, time.UTC) |
| 38 | end = start.AddDate(0, 1, 0) |
| 39 | default: |
| 40 | panic(fmt.Sprintf("unknown chat usage limit period: %q", period)) |
| 41 | } |
| 42 | |
| 43 | return start, end |
| 44 | } |
| 45 | |
| 46 | // ResolveUsageLimitStatus resolves the current usage-limit status for |
| 47 | // userID within organizationID. When organizationID is invalid (Valid |
no outgoing calls