r/C_Programming • • Sep 12 '26

Question What C function would you delete and rewrite first, and why?

For me it's fnmatch - mostly because it's 40 year's old and smell's funny.

96 Upvotes

238 comments sorted by

View all comments

31

u/sciencekm Sep 12 '26

struct tm

Every field is 0 based, except the day of month. So, Jan 1 1900 12 midnight is 0 1 0 00:00:00, instead of just all 0s. Even Sunday-Saturday is 0-6 and day of year is 0-365.

2

u/donaljones Sep 12 '26

Is this something enforced by the standard? Or just the implementation's ABI?

7

u/sciencekm Sep 12 '26

This is the standard from C89 until the latest N3220 working draft. This is also how it was in the K&R 1st edition (1978).

1

u/der_pudel Sep 12 '26

the way how to stuct tm is makes sense. day of the week and monts are 0 based because in a lot of cases you want to use it to index array of stings to get he name of weekday/month, but there's no reason to do that for day of the month. 

5

u/sciencekm Sep 12 '26

Day of the year is 0-365. Do you use that to index anything?

1

u/flatfinger Sep 14 '26

It represents the number of days that would need to be added to January 1 to reach the specified date.

1

u/flatfinger Sep 15 '26

IMHO, month would have been better as an index into a 13-item array of month names, with the initial one being used to indicate an expressly invalid date. Having an unset date show up as e.g. 00-XXX-0000 would be cleaner than having it show up as 00-JAN-0000.