Group: comp.lang.c++
From: "Victor Bazarov"
Date: Monday, April 07, 2008 3:09 PM
Subject: Re: gmtime_r or strftime problem or just my code?????

eskgwin@gmail.com wrote:
> On Apr 7, 11:56 am, "Victor Bazarov" wrote:
>> eskg...@gmail.com wrote:
>>> [..time_t is not defined in terms of seconds..]
>>> What is the general definition? Maybe I have it wrong.
>>
>> You have it specific to your platform. time_t is an arithmetic
>> type, but it isn't necessarily the number of seconds, generally.
>>
>>
>>
>>
>>
>>> I changed it to this:
>>
>>> char start_mon[40];
>>> char start_day[40];
>>> int s_day = 84;
>>
>>> time_t daysecs = (s_day * 86400);
>>> struct tm tm_result;
>>> struct tm *tm_time = &tm_result;
>>> gmtime_r(&daysecs,&tm_result);
>>
>>> strftime(start_mon, sizeof(start_mon), (char*)"%m", tm_time);
>>> strftime(start_day, sizeof(start_day), (char*)"%d", tm_time);
>>
>>> and get these results:
>>
>>> tm_time->tm_mday = 26
>>> tm_time->tm_mon = 2
>>> tm_time->tm_yday = 84
>>> tm_time->tm_isdst = 0
>>
>> What's tm_time->tm_year? Maybe this will open your eyes...
>>
>>> start_mon = 03
>>> start_day = 26
>>
>>> Maybe I am going about it the wrong way.
>>
>> What year do you get? You want to get the current year, don't you?
>>
>>> You would think it would be
>>> easy. I have a number of days since Jan. 1 of the year I am in (2008
>>> in this case). I just want to find the month/day of that year. I
>>> guess I will have to find out if it is a leap year or not.
>>
>> You have the number of days since Jan 1st 2008. 'gmtime' needs the
>> number of seconds since Jan 1st 1970. Where did the 38 years go?
>> Did you just omit them? You know, thirty-eight years is, like, half
>> a live of an average human; you can't just discount that... :-)
>>
>>> Is there an easier way to do this? I am open to all criticisms
>>> and/or suggestions. Thanks.
>>
>> You're almost there. You just have to understand what exactly your
>> 'gmtime_r' gives you if you supply (number * 86400) to it. What is
>> the meaning of the number?
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask- Hide
>> quoted text -
>>
>> - Show quoted text -
>
> I do get the correct year (2008).

You do? Really??? So, if you supply 0 as your 'daysecs' value,
you get the year 2008? I strongly doubt it, considering the
definition of 'gmtime_r' you've given.

> So, I have to supply it the number
> of seconds since Jan 1, 1970? That seems like a huge number. Is there
> a standard for it somewhere that is the correct number of seconds?

I don't know.

> Maybe then I can get it to work.
>
> Thanks for the help and advice. I hope I can get this soon. It is
> really bugging me.

Well, there are probably other ways which would require you to do
a bit more calculations than just multiplying by 86400 and calling
a function. They might be more reliable and portable, of course.
The sequence 0,31,59,90,120,151,... is the number of days from the
beginning of the year to the first of the respective month. This
requires a correction for a leap year (and for the next ninety
years you can simply use !(year % 4) as your leap year indicator.
So, you could subtract the numbers in the sequence to learn the
month (while adjusting for the leap) and the difference would give
you the day of the month (less one). Should be relatively easy to
implement. Try it. There is one loop, one 'if', probably. One
minus, two pluses. No need to multiply.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Safety Articles | Usenet Groups | Usenet News | Bluegrass