writes:
> If I have a year and the day of the year (e.g. yyyy.ddd), is there
> some simple way to convert this to a time_t? mktime(), of course, does
> just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.
>
> There's the obvious brute force approach of subtracting 31, 28, 31,
> 30, etc... until you arrive at the right result (being careful to allow
> for leap years, of course), but it seems like there ought to be some
> standard library routine for this purpose.
>
> It only has to work for "modern" days, post 1970.
>
So, you have the year and the day of the year and you want to get the
right year, month and day (or the right time_t value based on those
values)?
mktime() need a pointer to a struct tm because it modifies it's
argument, meaning that it normalizes your date. For example, the
following program:
<code>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void) {
struct tm mytm;
mytm.tm_year=2006-1900;
mytm.tm_mon=0;
mytm.tm_mday=365;
mytm.tm_isdst=-1;
mytm.tm_hour=0;
mytm.tm_min=0;
mytm.tm_sec=0;
if(mktime(&mytm)==-1) {
printf("I cannot represent the calendar time

\n");
exit(EXIT_FAILURE);
}
printf("%d\t%d\t%d\n",mytm.tm_year+1900,mytm.tm_mo n,mytm.tm_mday);
return 0;
}
</code>
Will output
2006 11 31
(if mktime can represent the time).
The values of the 6 fields that matter for mktime (I don't count
"dst" here, now) don't have to be in the specified ranges and mktime
will change it's argument by setting the components to the correct
values. This means that if you say year=106, mon=0 (Jan) mday=32 (1
more than the max number), hour=0, min=0 and sec=0 the values that
exceed the ranges will be added (or substracted) to the higher group
i.e. mday=32 means 31 day in January + 1 the next month which will
make mon=1 and leave everything else unchanged.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)