tm_yday to time_t?

B

bob

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.

Thanks,
Bob Armstrong
 
A

Al Balmer

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.

Google for "serial date" or "Julian date". You should find algorithms
that can be adapted. I'd say more, but I'm out of here until next
Wednesday!
 
N

Nelu

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_mon,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.
 
J

Joe Wright

Nelu said:
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_mon,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.
November 31 ?
 
N

Nelu

Joe Wright said:
November 31 ?

The month is 0 based. (0 - January, 11 - December). I set
mytm.tm_mon=0. If you complain about 11 why don't you complain about
0? :)
 
N

Nelu

Nelu said:
Joe Wright said:
Nelu said:
November 31 ?

The month is 0 based. (0 - January, 11 - December). I set
mytm.tm_mon=0. If you complain about 11 why don't you complain about
0? :)

Then again... scratch the last two sentences. Sorry. mday=365 is also out
of range for day in month so it could've been interpreted that way for
the month, too. I should've put tm_mon+1 instead on tm_mon in printf
so it won't lead to confusion. I hope the OP got it right.
 
J

Joe Wright

Nelu said:
Nelu said:
Joe Wright said:
Nelu wrote:
[snip]
November 31 ?
The month is 0 based. (0 - January, 11 - December). I set
mytm.tm_mon=0. If you complain about 11 why don't you complain about
0? :)

Then again... scratch the last two sentences. Sorry. mday=365 is also out
of range for day in month so it could've been interpreted that way for
the month, too. I should've put tm_mon+1 instead on tm_mon in printf
so it won't lead to confusion. I hope the OP got it right.
Well, I apologize. tm_mon is indeed 0..11 (12 months) and tm_yday is
0..365 (366 days).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top