googleapis / googleapis/google-cloud-go
civil: add AddDate, WeekDay, YearDay, ISOWeek functions to Date
- Dominant language
- Go
- Stars
- 4.5k
- Forks
- 1.6k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 109
Description
**Is your feature request related to a problem? Please describe.**
Monzo is a bank written in Go. We write a lot of code that depends on accurate date calculations in order to schedule payments for our customers. Go doesn't provide a built-in date-only type, so our engineers have historically always represented dates using `time.Time` with the time set to 00:00 and the location set to UTC. However, this has never been a great solution for us because the presence of the time and location can be a source of date logic bugs (if accidentally populated with non-zero values). We are exploring using `civil.Date` but have hit a few snags where `civil.Date` is missing date functionality that the `time` package provides.
**Describe the solution you'd like**
Add 4 new functions to `civil.Date` that mirror existing functions in the `time` package:
```golang
func (Date) AddDate(year, month, day int) Date
func (Date) YearDay() int
func (Date) WeekDay() time.Weekday
func (Date) ISOWeek() (year, week int)
```
`AddDate(year, month, day int)` is a more generalized and useful form of the `AddDays(n int)` function which already exists on `civil.Date`.
**Describe alternatives you've considered**
We can always convert from `civil.Date` to `time.Time` to call the functions we need. But it would be much nicer if `civil.Date` contained these functions already because then it would be a drop-in replacement for `time.Time`. This would make our migration much easier.
**Additional context**
I found an earlier PR https://github.com/googleapis/google-cloud-go/issues/1063 where a proposal to add `func (Date) Weekday()` was rejected based on an assumption that the weekday is affected by the TZ location. In actuality the weekday is not affected by the TZ location. See https://cs.opensource.google/go/go/+/refs/tags/go1.20.1:src/time/time.go;l=527-537
Contributor guide
Assessment
This issue has not been assessed yet.