[PATCH v2 1/1] offset: optimized calculation of year from unix timestamp

Paul Eggert eggert@cs.ucla.edu
Fri Aug 29 22:19:50 GMT 2025


On 2025-08-29 01:03, Gary Gende wrote:
> +  days -= (days / DIVISOR_400) * DIVISOR_400;

This would be clearer if written as "days %= DIVISOR_400;". Similarly 
for other assignments to DAYS.

+  y += MIN_YEAR_GROUP_NUM(days / DIVISOR_100, 3) * 100;
+  days -= MIN_YEAR_GROUP_NUM(days / DIVISOR_100, 3) * DIVISOR_100;

This looks like it calculates the wrong number if days / DIVISOR_100 == 
-4. Similarly for the other use of MIN_YEAR_GROUP_NUM.

Also, why not use sys/param.h's MIN instead of reinventing the wheel?

Also, in calls put a space before "(".

> +#define DAYS_OFFSET   (280 * 365 + 89 * 366)
> +#define DIVISOR_400   (303 * 365 + 97 * 366)
> +#define DIVISOR_100   (76 * 365 + 24 * 366)
> +#define DIVISOR_4     (3 * 365 + 366)

These need comments.

> +  /* Start at the begining of a 400 year leapyear cycle */
> +  y = 1601;

"beginning" is misspelled.

Why not start at 1 instead of at 1601?

+  if (days < 0) {
+    --y;
+    days += 365;
+    if (__isleap(y)) ++days;
+  }

Use usual glibc indenting style.


More information about the Libc-alpha mailing list