The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare file modification date/time to current date/time Bill Ma Shell Programming and Scripting 5 08-27-2009 12:02 PM
calculate the time dndoon UNIX for Dummies Questions & Answers 5 01-09-2009 05:12 PM
calculate the date of next satureday of current date. rinku Shell Programming and Scripting 3 09-02-2008 11:05 PM
calculate time itik AIX 2 02-15-2008 02:08 AM
how to calculate CPU time under HP-UX limame HP-UX 1 06-18-2007 05:28 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-07-2009
Sapfeer Sapfeer is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 97
Calculate time to some date

Hello!
I need to find how many days, hours and minutes remain to some specific date and I wonder why the following program shows incorrect values, such as 4 days 23 hours etc to 14:00 this Saturday from 17:33 today...

Code:
#include <stdio.h>
#include <time.h>

int main()
{
    time_t elaps, timettx, curtime;
    struct tm *elaps_st, time_x;

    time_x.tm_year = 2009-1900;
    time_x.tm_mon = 6;
    time_x.tm_mday = 11;
    time_x.tm_hour = 14;
    time_x.tm_min = 0;
    time_x.tm_sec = 0;
    time_x.tm_isdst = 1;

    time(&curtime);
    printf("Current time:   %s", ctime(&curtime));
    timettx = mktime(&time_x);
    printf("Time X:         %s", ctime(&timettx));
    elaps = timettx - curtime;
    printf("Diff time:      %s", ctime(&elaps));
    elaps_st = localtime(&elaps);

    printf("To time X remain %d days %d hours %d min %d sec\n", elaps_st->tm_mday, elaps_st->tm_hour, elaps_st->tm_min, elaps_st->tm_sec);
}
  #2 (permalink)  
Old 07-07-2009
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Using function ctime here is wrong as it gives the elapsed time in secs. from the epoch...just divide the elapsed time "elaps" by the number of hours in a day to give the days left until Sat 14:00. Do same to get hrs/mins/secs until Sat 14:00...
Code:
d = elaps / (60*60*24);
h = (elaps - (d * 60*60*24)) / (3600);
printf("%d days %d hrs left\n", d, h);
  #3 (permalink)  
Old 07-07-2009
Sapfeer Sapfeer is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 97
shamrock
Thanks a lot! I've implemented all my stuff with your code! But as you can see, I use localtime() function. Could you kindly explain, why localtime() finction here does not convert seconds to tm struct correctly?.. As I know, all unix time is just amount of seconds from 1 Jan 1970, so when I have some rather small value in seconds, why localtime() put incorrect amount of days, hours etc in tm struct?..
  #4 (permalink)  
Old 07-07-2009
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
That's because the "elaps_st = localtime(&elaps);" command gets the date and time stamp that is "elaps" seconds from the epoch...which is very different from how many days hours minutes and seconds separate two dates. Post the output of the program you wrote as it will be easier to explain in relation to that.
  #5 (permalink)  
Old 07-08-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
1. call strptime() to convert a date into a struct tm.
2. get current epoch seconds using time(),
(if the second "date" is not today, then use
#1 and #3 to get another time_t value in seconds.)
3. call strftime() with format %s on the struct from #1.
4. you now have two time_t values in seconds.

With modulo arutmetic and subtraction - 86400 seconds per day, 3600 seconds per hour, 60 seconds per minute. You now have the number of days hours, etc between two times.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0