Skip to content

Add support for %G for week year and %g format for week year without century#67

Merged
lestrrat merged 1 commit intolestrrat-go:masterfrom
flomedja:66-support-yearweek
Jul 29, 2025
Merged

Add support for %G for week year and %g format for week year without century#67
lestrrat merged 1 commit intolestrrat-go:masterfrom
flomedja:66-support-yearweek

Conversation

@flomedja
Copy link
Copy Markdown
Contributor

Description

Add support for week year and week year without century format

@flomedja
Copy link
Copy Markdown
Contributor Author

@lestrrat Here is the MR with the code :)

@flomedja flomedja mentioned this pull request Jul 25, 2025
Comment thread appenders.go
Comment on lines +267 to +281
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')
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

@flomedja flomedja Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was expecting for a solution like this.

So 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

…century

Handle negative years for %G and %g format
@flomedja flomedja force-pushed the 66-support-yearweek branch from 5d625cd to 3c31e6f Compare July 29, 2025 01:58
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.43%. Comparing base (b0b9070) to head (3c31e6f).
⚠️ Report is 12 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lestrrat lestrrat merged commit 75ce574 into lestrrat-go:master Jul 29, 2025
6 checks passed
@lestrrat
Copy link
Copy Markdown
Collaborator

@flomedja @ccoVeille Thanks, merged & released

@flomedja flomedja deleted the 66-support-yearweek branch December 12, 2025 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants