gmtime_r or strftime problem or just my code?????

E

eskgwin

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
 
E

eskgwin

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).
 
E

eskgwin

(e-mail address removed) 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?

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

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.




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
 
E

eskgwin

[..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 help and advice. I hope I can get this soon. It is
really bugging me.

Allyson
 
J

Jerry Coffin

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

Presumably you want it for the current year. In this case, I'd start by
using time() to get the current time. I'd then convert it to a struct tm
using localtime. After that, change the month to 0, and the day of month
to the day in the year you want to convert. Call mktime on the resulting
struct tm.

If this is successful, the tm_yday member should be set to the value you
originally put into the tm_mday member and mktime should return a value
other than (time_t)-1. tm_mon and tm_mday should now be set to their
correct values.
 
J

James Kanze

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'm guessing that you're under a Unix or Unix-like system,
because of gmtime_r. If so, the date you are giving gmtime_r is
in 1970. 1970 wasn't a leap year, and the function is figuring
this out correctly.

Note that as a general rule, the only way to put a meaningful
value into a time_t is through one of the system functions. The
C/C++ standards say nothing about the representation of a
time_t. As far as you are concerned, it's a magic cookie.

Under Posix, of course, it's guaranteed to be the number of
seconds since the epoch, and I don't know of a Unix system where
the epoch is anything else but midnight, 1 Jan., 1970.
I was hoping there was something that would do that
automatically, but probably not.

I think it is doing it automatically. You gave it a time in
1970, and 1970 isn't a leap year.

If you want a particular day or whatever in this year, the way
to get it is:

std::time_t now = std::time( NULL ) ;
std::tm tmp = *std::localtime( &now ) ;
// modify tmp as desired...
std::time_t target = std::mktime( &tmp ) ;
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top