473,473 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

about difftime()

Hi,

Is there a portable way to convert the value returned by difftime (a
number of seconds of type double) in time_t, which, AFAIK, is not a
number of seconds.

For this reason, I guess, that this code of mine, supposed to give an
example about 'how to use time functions correctly' is not portable, is
it ?

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main (void)
{
time_t now = time (NULL);
struct tm tm_now = *localtime (&now);
char s[64];

strftime (s, sizeof s, "%d/%m/%Y", &tm_now);
printf ("Today is : %s\n", s);
/* next Christmas */
{
struct tm tm_xmas =
{0};

tm_xmas.tm_year = tm_now.tm_year;
tm_xmas.tm_mon = 12 - 1;
tm_xmas.tm_mday = 25;

/* ajustment */
{
time_t xmas = mktime (&tm_xmas);

strftime (s, sizeof s, "%d/%m/%Y", &tm_xmas);
printf ("next Christmas is : %s\n", s);

{
time_t diff = difftime (xmas, now);
struct tm tm_diff = *gmtime (&diff);

printf ("Only %d days remaining before Christmas\n",
tm_diff.tm_yday);
}
}
}

return 0;
}

Today is : 07/01/2006
next Christmas is : 25/12/2006
Only 351 days remaining before Christmas
--
A+

Emmanuel Delahaye
Jan 7 '06 #1
6 6519
Emmanuel Delahaye said:
Hi,

Is there a portable way to convert the value returned by difftime (a
number of seconds of type double) in time_t, which, AFAIK, is not a
number of seconds.


You can use a correctly-initialised struct tm to store your number of
seconds, provided it fits into an int - if not, try a bit of intelligent
scaling, e.g. if you know it'll fit into a year but not a day, you can do
something like: tm_yday = d / 86400; tm_sec = (d / 86400 - tm_yday) * 86400

Then shove the whole mess through mktime to normalise it and hand you a
time_t on a plate.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 7 '06 #2
Emmanuel Delahaye wrote:
Hi,

Is there a portable way to convert the value returned by difftime (a
number of seconds of type double) in time_t, which, AFAIK, is not a
number of seconds.


The only thing I can think of is to round or truncate the
difftime() value to get an integral number of seconds, and use
that to populate a struct tm. Some care is required in case
INT_MAX is small; something like

long seconds = round(difftime(xmas, now));
struct tm tm = { 0 };
tm.tm_mday = seconds / (60 * 60 * 24L);
seconds %= 60 * 60 * 24L;
tm.tm_hour = seconds / (60 * 60);
seconds %= 60 * 60;
tm.tm_sec = seconds; /* < INT_MAX */
gmtime (&tm);

might do the trick. It's a little sleazy about leap seconds,
but I think that'll be true of any method that tries to use
an "absolute" time to represent an interval.

Another, simpler method:

printf ("%.0f days to go\n",
difftime(xmas, now) / (60 * 60 * 24.0));

Again, this ignores leap seconds.

However, since you've got both today's and Christmas'
dates in struct tm form, why not just subract the tm_yday
elements?

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 7 '06 #3
Eric Sosman a écrit :
However, since you've got both today's and Christmas'
dates in struct tm form, why not just subract the tm_yday
elements?


Yes, sounds obvious once you mentioned it ! Anyway, thanks for your
answers Eric and Richard, and Happy New Year.

--
A+

Emmanuel Delahaye
Jan 7 '06 #4
Emmanuel Delahaye wrote:
Eric Sosman a écrit :
However, since you've got both today's and Christmas'
dates in struct tm form, why not just subract the tm_yday
elements?

Yes, sounds obvious once you mentioned it ! Anyway, thanks for your
answers Eric and Richard, and Happy New Year.


Wouldn't that work only within the same year?
Bjørn
Jan 7 '06 #5
Bjørn Augestad wrote:
Emmanuel Delahaye wrote:
Eric Sosman a écrit :
However, since you've got both today's and Christmas'
dates in struct tm form, why not just subract the tm_yday
elements?


Yes, sounds obvious once you mentioned it ! Anyway, thanks for your
answers Eric and Richard, and Happy New Year.


Wouldn't that work only within the same year?


Yes. Emmanuel's code constructs the date of Christmas
by plugging December 25 into the struct tm for the current
time, so "same year" is certain.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 7 '06 #6
Bjørn Augestad a écrit :
Eric Sosman a écrit :
However, since you've got both today's and Christmas'
dates in struct tm form, why not just subract the tm_yday
elements?
Wouldn't that work only within the same year?


Thinking more about it, yes, it could be a problem between in the 26 to
31 December period. But at this time of the year, a few people are
concerned by the question !

--
A+

Emmanuel Delahaye
Jan 7 '06 #7

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

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
4
by: collinm | last post by:
hi parseDate(&prod->tmbEndRun, date_rtu); parseTime(&prod->tmbEndRun, prod->htime_rtu_fin); printf("end %s\n", prod->htime_rtu_fin); return me
1
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, In MSDN for time function, time_t time( time_t *timer ); http://msdn2.microsoft.com/en-us/library/1f4c8f33(VS.80).aspx
1
by: patrickv | last post by:
Hello All, I have a problem with difftime. I have 2 strings containing a time. I must calculate the difference of this 2 strings into seconds. This is my code: double Seconds;...
1
by: aramakri | last post by:
Hi, I am using difftime() to compare two time_t values.If the difference between the two time_t values is 6, difftime() returns 3606. Can anybody tell me where I am doing a mistake
5
by: Yapy | last post by:
This is part of the program I wrote. I need to use the difftime(time_t, time_t) function but I am not sure of how to use it. do { switch(operation) { case 1: answer=random_integer1 +...
0
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.