Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > tm_yday to time_t?

Reply
Thread Tools

tm_yday to time_t?

 
 
bob@jfcl.com
Guest
Posts: n/a
 
      06-30-2006

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

 
Reply With Quote
 
 
 
 
Al Balmer
Guest
Posts: n/a
 
      06-30-2006
On 30 Jun 2006 15:14:32 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Nelu
Guest
Posts: n/a
 
      07-01-2006
(E-Mail Removed) 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...)
 
Reply With Quote
 
bob@jfcl.com
Guest
Posts: n/a
 
      07-01-2006

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

 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      07-01-2006
Nelu wrote:
> (E-Mail Removed) 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 ---
 
Reply With Quote
 
Nelu
Guest
Posts: n/a
 
      07-01-2006
Joe Wright <(E-Mail Removed)> writes:

> Nelu wrote:
> > (E-Mail Removed) 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...)
 
Reply With Quote
 
Nelu
Guest
Posts: n/a
 
      07-01-2006
Nelu <(E-Mail Removed)> writes:

> Joe Wright <(E-Mail Removed)> 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...)
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      07-01-2006
Nelu wrote:
> Nelu <(E-Mail Removed)> writes:
>
>> Joe Wright <(E-Mail Removed)> 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 ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments