By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

tm_yday to time_t?

P: n/a
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

Jun 30 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 30 Jun 2006 15:14:32 -0700, bo*@jfcl.com wrote:

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!

--
Al Balmer
Sun City, AZ
Jun 30 '06 #2

P: n/a
bo*@jfcl.com 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...)
Jul 1 '06 #3

P: n/a
bob

Nelu wrote:
mytm.tm_mday=365;
....


That is just extraordinarily clever! And it actually works, too :-)

I knew there was some easier way to do it.

Thanks,
Bob

Jul 1 '06 #4

P: n/a
Nelu wrote:
bo*@jfcl.com 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.

November 31 ?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 1 '06 #5

P: n/a
Joe Wright <jo********@comcast.net> writes:
Nelu wrote:
bo*@jfcl.com 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.

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? :-)

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 1 '06 #6

P: n/a
Nelu <sp*******@gmail.com> writes:
Joe Wright <jo********@comcast.net> writes:
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.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 1 '06 #7

P: n/a
Nelu wrote:
Nelu <sp*******@gmail.com> writes:
Joe Wright <jo********@comcast.net> writes:
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).

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 1 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.