Add support for %G for week year and %g format for week year without century#67
Conversation
|
@lestrrat Here is the MR with the code :) |
| if year < 1000 { | ||
| if year < 10 { | ||
| b = append(b, '0', '0', '0') | ||
| } else if year < 100 { | ||
| b = append(b, '0', '0') | ||
| } else { | ||
| b = append(b, '0') | ||
| } | ||
| } |
There was a problem hiding this comment.
Be careful of year before era, so negative year.
One solution could be to convert the year as a string, trim the possible - prefix, then check for the cleaned string length.
Anyway no matter the implementation you need test to cover this
There was a problem hiding this comment.
I think I'd prefer if year < 0 { return error }. Oh wait, I have no way to return an error.
I hate myself from 9 years ago.
There was a problem hiding this comment.
The preferable solution would be to return an error. But like @lestrrat says errors are not supported at the moment. Introduicing the error for the appender is a breaking change for all the Appender functions. I think It should be done in another task.
@ccoVeille I propose supporting the before era years by adding the - before processing the padding :
func appendWeekYear(b []byte, t time.Time) []byte {
year, _ := t.ISOWeek()
// Handle negative years (BCE)
if year < 0 {
b = append(b, '-')
year = -year
}
// Ensure 4-digit formatting
if year < 1000 {
if year < 10 {
b = append(b, '0', '0', '0')
} else if year < 100 {
b = append(b, '0', '0')
} else {
b = append(b, '0')
}
}
return append(b, strconv.Itoa(year)...)
}
func appendWeekYearNoCentury(b []byte, t time.Time) []byte {
year, _ := t.ISOWeek()
if year < 0 {
b = append(b, '-')
year = -year
}
yearNoCentury := year % 100
if yearNoCentury < 10 {
b = append(b, '0')
}
return append(b, strconv.Itoa(yearNoCentury)...)
}
WDYT ?
There was a problem hiding this comment.
I was expecting for a solution like this.
So 👍
…century Handle negative years for %G and %g format
5d625cd to
3c31e6f
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #67 +/- ##
==========================================
+ Coverage 81.75% 82.43% +0.68%
==========================================
Files 7 5 -2
Lines 411 427 +16
==========================================
+ Hits 336 352 +16
Misses 60 60
Partials 15 15 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@flomedja @ccoVeille Thanks, merged & released |
Description
Add support for week year and week year without century format