Calculate time to some date


 
Thread Tools Search this Thread
Top Forums Programming Calculate time to some date
# 1  
Old 07-07-2009
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  
Old 07-07-2009
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  
Old 07-07-2009
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  
Old 07-07-2009
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  
Old 07-08-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

2. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

3. Shell Programming and Scripting

Calculate Julian date of a given date

How to get Julian date (Three digit) of a given date (Not current date)? I do not have root privilege - so can not use date -d. Assume that we have three variables year, month and date. Thx (5 Replies)
Discussion started by: Soham
5 Replies

4. Shell Programming and Scripting

How to calculate time

Hello Guys, I am trying to calculate total hours and minutes a given user has used the system since the beginning of the current month. #!/usr/bin/sh hr=0 min=0 last $1 | grep -w `date "+%b"` | grep -v '\(0:.*\)' | grep -vw sshd | cut -c 66- | tr -d "\(\)" | cut -f1 -d ":" | grep -v '.*' |... (9 Replies)
Discussion started by: kasparov
9 Replies

5. Shell Programming and Scripting

calculate time from a given format

Hi i have a file which consists of the time records in following format H:MM:SS.sss 0:00:09.249 0:00:00.102 0:00:00.105 0:00:08.499 0:00:08.499 0:00:06.980 0:00:04.249 0:00:05.749 0:00:00.108 0:00:00.107 0:00:03.014 0:00:00.000 I need to calculate their equivalent milliseconds... (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

6. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

7. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

8. UNIX for Dummies Questions & Answers

calculate the time

hello i want to display the time firstly when i run my shell script and after 25 min i want to display a message it says that the time left is 5 min. When the calculated time is 30 mins, the script should exit. can any one help me with that! Thanks in advance Regards :o (5 Replies)
Discussion started by: dndoon
5 Replies

9. Shell Programming and Scripting

calculate the date of next satureday of current date.

I want to calculate the date of next satureday of current date using shell script. Suppose, today is 27-feb-08 I want to get the date of next satureday, which means 01-mar-08, in the formate '' YYMMDD ". I do this in ksh.. Please tell me any type of command which help me out. Thanks in... (3 Replies)
Discussion started by: rinku
3 Replies

10. AIX

calculate time

Hi, How do I calculate time? I need to create an alert if a process is running more than 30 minutes. I need to get the first time and then get another, calculate it if more than 30 mins and then alert it to pager. Can't find it in internet. Thanks in advance, itik (2 Replies)
Discussion started by: itik
2 Replies
Login or Register to Ask a Question