473,499 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Apr 7 '08 #1
7 2475
es*****@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
Apr 7 '08 #2
es*****@gmail.com wrote:
On Apr 7, 10:51 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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
Apr 7 '08 #3
On Apr 7, 11:10*am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
eskg...@gmail.com wrote:
On Apr 7, 10:51 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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
Apr 7 '08 #4
es*****@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
Apr 7 '08 #5
On Apr 7, 11:56*am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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 help and advice. I hope I can get this soon. It is
really bugging me.

Allyson
Apr 7 '08 #6
es*****@gmail.com wrote:
On Apr 7, 11:56 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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
Apr 7 '08 #7
In article <d046102a-3266-41f4-ac7d-96a289872bd3
@a1g2000hsb.googlegroups.com>, es*****@gmail.com says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 8 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1625
by: Richard Shea | last post by:
Hi - I'm trying to use strftime to output the day of the week but I find that I always get told it's Monday. I have tried day, month, year etc and all come out correctly but as soon as I use %a I...
1
2242
by: Allen Unueco | last post by:
I feel that the '%Z' format specifier from strftime() returns the wrong value when daylight savings is in effect. Today the following is always true: time.strftime('%Z') == time.tzname ...
4
9265
by: John Hunter | last post by:
>>> from datetime import date >>> dt = date(1005,1,1) >>> print dt.strftime('%Y') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: year=1005 is before 1900; the...
5
2918
by: Sheila King | last post by:
I have a web app that has been running just fine for several months under Python 2.2.2. We are preparing to upgrade the server to run Python 2.4.1. However, part of my web app is throwing an...
4
2873
by: Andy Leszczynski | last post by:
Python 2.2/Unix >>time.strftime("%T") '22:12:15' >>time.strftime("%X") '22:12:17' Python 2.3/Windows >>time.strftime("%X")
2
3942
by: Mike Conmackie | last post by:
Greetings, Is there any way to force strftime() to ignore locale settings when formatting the resulting string? I have a requirement to create a specific date-time string format in UTC. ...
0
3249
hqprog
by: hqprog | last post by:
Having search extensively I've learned the two functions timegm and gmtime_r though in the GNU standard C library extend the ISO standard. I need to use these two functions in myprog.c (on pc - ...
3
2208
by: HSeganfredo | last post by:
Folks, my code below truncates the output as: chars 0,200709 chars 0,1839 If I change to: i = strftime(date, 9, "%Y%m%d", brokentime); j = strftime(hour, 7, "%H%M%S", brokentime); it...
2
3433
by: Giovanni Bajo | last post by:
Hello, I am trying to find a good way to portably get the output of strftime() and put it onto a dialog (I'm using PyQt, but it doesn't really matter). The problem is that I need to decode the...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.