PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > gmtime_r or strftime problem or just my code?????
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
gmtime_r or strftime problem or just my code?????

Réponse
 
LinkBack Outils de la discussion
Vieux 07/04/2008, 18h42   #1
eskgwin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut gmtime_r or strftime problem or just my code?????

I have this piece of code that takes a day of the year and converts it
to the month and day of month:

char start_mon[40];
char start_day[40];
int s_day = 84;
time_t daysecs = (s_day * 86400) - 1;
struct tm tm_result;
struct tm *tm_time = &tm_result;
tm_time = 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);

I am just testing it with 84, but this is a parameter to the method.
Anyway, the month and day should be 03 24, but I am getting 03 25. It
seems like it is not taking into account leap year. Is it gmtime_r or
strftime or something else? Do I need to actually check the year and
figure out if it is a leap year? I was hoping there was something that
would do that automatically, but probably not. Thanks.

Allyson
  Réponse avec citation
Vieux 07/04/2008, 18h51   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gmtime_r or strftime problem or just my code?????

eskgwin@gmail.com wrote:
> I have this piece of code that takes a day of the year and converts it
> to the month and day of month:
>
> char start_mon[40];
> char start_day[40];
> int s_day = 84;
> time_t daysecs = (s_day * 86400) - 1;
> struct tm tm_result;
> struct tm *tm_time = &tm_result;
> tm_time = 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);
>
> I am just testing it with 84, but this is a parameter to the method.
> Anyway, the month and day should be 03 24, but I am getting 03 25. It
> seems like it is not taking into account leap year. Is it gmtime_r or
> strftime or something else? Do I need to actually check the year and
> figure out if it is a leap year? I was hoping there was something that
> would do that automatically, but probably not. Thanks.


What's "gmtime_r"?

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


  Réponse avec citation
Vieux 07/04/2008, 18h58   #3
eskgwin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gmtime_r or strftime problem or just my code?????

On Apr 7, 10:51am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> eskg...@gmail.com wrote:
> > I have this piece of code that takes a day of the year and converts it
> > to the month and day of month:

>
> > char start_mon[40];
> > char start_day[40];
> > int s_day = 84;
> > time_t daysecs = (s_day * 86400) - 1;
> > struct tm tm_result;
> > struct tm *tm_time = &tm_result;
> > tm_time = 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);

>
> > I am just testing it with 84, but this is a parameter to the method.
> > Anyway, the month and day should be 03 24, but I am getting 03 25. It
> > seems like it is not taking into account leap year. Is it gmtime_r or
> > strftime or something else? Do I need to actually check the year and
> > figure out if it is a leap year? I was hoping there was something that
> > would do that automatically, but probably not. Thanks.

>
> What's "gmtime_r"?
>
> 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 -



gmtime_r() -- Convert Time (Restartable)
Format

#include <time.h>
struct tm *gmtime_r(const time_t *time, struct tm *result);

Language Level: XPG4

Threadsafe: Yes,

Description

This function is the restartable version of gmtime().

The gmtime_r() function breaks down the time value, in seconds, and
stores it in result. result is a pointer to the tmstructure, defined
in <time.h>. The value time is usually obtained by a call to the
time() function.

The fields of the tm structure include:


tm_sec
Seconds (0-61)

tm_min
Minutes (0-59)

tm_hour
Hours (0-23)

tm_mday
Day of month (1-31)

tm_mon
Month (0-11; January = 0)

tm_year
Year (current year minus 1900)

tm_wday
Day of week (0-6; Sunday = 0)

tm_yday
Day of year (0-365; January 1 = 0)

tm_isdst
Zero if Daylight Saving Time is not in effect; positive if Daylight
Saving Time is in effect; negative if the information is not
available.
Return Value

The gmtime_r() function returns a pointer to the resulting tm
structure.

Notes:


The range (0-61) for tm_sec allows for as many as two leap seconds.

The gmtime() and localtime() functions may use a common, statically
allocated buffer for the conversion. Each call to one of these
functions may alter the result of the previous call. The asctime_r(),
ctime_r(), gmtime_r(), and localtime_r() functions do not use a
common, statically-allocated buffer to hold the return string. These
functions can be used in the place of the asctime(), ctime(),
gmtime(), and localtime() functions if reentrancy is desired.

Calendar time is the number of seconds that have elapsed since EPOCH,
which is 00:00:00, January 1, 1970 Universal Coordinate Time (UTC).

  Réponse avec citation
Vieux 07/04/2008, 19h10   #4
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gmtime_r or strftime problem or just my code?????

eskgwin@gmail.com wrote:
> On Apr 7, 10:51 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> eskg...@gmail.com wrote:
>>> I have this piece of code that takes a day of the year and converts
>>> it to the month and day of month:

>>
>>> char start_mon[40];
>>> char start_day[40];
>>> int s_day = 84;
>>> time_t daysecs = (s_day * 86400) - 1;
>>> struct tm tm_result;


What is this for?

>>> struct tm *tm_time = &tm_result;


Why do you need to initialise 'tm_time' just to lose the value and get
a new one in the next statement?

>>> tm_time = 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);

>>
>>> I am just testing it with 84, but this is a parameter to the method.
>>> Anyway, the month and day should be 03 24, but I am getting 03 25.


Have you tried subtracting more than 1 when calculating 'daysecs'?
Put it in a loop and see when you get a different value. Just curiuos
because it seems that day 0 would actually mean that 'daysecs' is -1,
which doesn't seem right.

>>> It seems like it is not taking into account leap year. Is it
>>> gmtime_r or strftime or something else? Do I need to actually check
>>> the year and figure out if it is a leap year? I was hoping there
>>> was something that would do that automatically, but probably not.
>>> Thanks.

>>
>> What's "gmtime_r"?
>>
>> 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 -

>
>
> gmtime_r() -- Convert Time (Restartable)
> [..]


So, it's exacly like 'gmtime', only the standard function does not
define 'time_t' type as the number of seconds since some date. There
is no requirement that 'time_t' is implemented in seconds.

>
> Calendar time is the number of seconds that have elapsed since EPOCH,
> which is 00:00:00, January 1, 1970 Universal Coordinate Time (UTC).


Well, this is apparently the definition on *your* system, it is not
the general definition, just so that you know...

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


  Réponse avec citation
Vieux 07/04/2008, 19h28   #5
eskgwin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gmtime_r or strftime problem or just my code?????

On Apr 7, 11:10am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> eskg...@gmail.com wrote:
> > On Apr 7, 10:51 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> >> eskg...@gmail.com wrote:
> >>> I have this piece of code that takes a day of the year and converts
> >>> it to the month and day of month:

>
> >>> char start_mon[40];
> >>> char start_day[40];
> >>> int s_day = 84;
> >>> time_t daysecs = (s_day * 86400) - 1;
> >>> struct tm tm_result;

>
> What is this for?
>
> >>> struct tm *tm_time = &tm_result;

>
> Why do you need to initialise 'tm_time' just to lose the value and get
> a new one in the next statement?
>
> >>> tm_time = 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);

>
> >>> I am just testing it with 84, but this is a parameter to the method.
> >>> Anyway, the month and day should be 03 24, but I am getting 03 25.

>
> Have you tried subtracting more than 1 when calculating 'daysecs'?
> Put it in a loop and see when you get a different value. Just curiuos
> because it seems that day 0 would actually mean that 'daysecs' is -1,
> which doesn't seem right.
>
>
>
>
>
> >>> It seems like it is not taking into account leap year. Is it
> >>> gmtime_r or strftime or something else? Do I need to actually check
> >>> the year and figure out if it is a leap year? I was hoping there
> >>> was something that would do that automatically, but probably not.
> >>> Thanks.

>
> >> What's "gmtime_r"?

>
> >> 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 -

>
> > gmtime_r() -- Convert Time (Restartable)
> > [..]

>
> So, it's exacly like 'gmtime', only the standard function does not
> define 'time_t' type as the number of seconds since some date. There
> is no requirement that 'time_t' is implemented in seconds.
>
>
>
> > Calendar time is the number of seconds that have elapsed since EPOCH,
> > which is 00:00:00, January 1, 1970 Universal Coordinate Time (UTC).

>
> Well, this is apparently the definition on *your* system, it is not
> the general definition, just so that you know...
>
> 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 -


What is the general definition? Maybe I have it wrong.

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
start_mon = 03
start_day = 26


Maybe I am going about it the wrong way. 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.


Is there an easier way to do this? I am open to all criticisms and/or
suggestions. Thanks.

Allyson
  Réponse avec citation
Vieux 07/04/2008, 19h56   #6
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gmtime_r or strftime problem or just my code?????

eskgwin@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


  Réponse avec citation
Vieux 07/04/2008, 20h24   #7
eskgwin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gmtime_r or strftime problem or just my code?????

On Apr 7, 11:56am, "Victor Bazarov" <v.Abaza...@comAcast.net> 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). 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?
Maybe then I can get it to work.

Thanks for the and advice. I hope I can get this soon. It is
really bugging me.

Allyson
  Réponse avec citation
Vieux 07/04/2008, 21h09   #8
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gmtime_r or strftime problem or just my code?????

eskgwin@gmail.com wrote:
> On Apr 7, 11:56 am, "Victor Bazarov" <v.Abaza...@comAcast.net> 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 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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h41.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21125 seconds with 16 queries