Sponsored Content
Full Discussion: Calculate time to some date
Top Forums Programming Calculate time to some date Post 302331830 by Sapfeer on Tuesday 7th of July 2009 09:40:46 AM
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);
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
LOCALTIME(3)								 1							      LOCALTIME(3)

localtime - Get the local time

SYNOPSIS
array localtime ([int $timestamp = time()], [bool $is_associative = false]) DESCRIPTION
The localtime(3) function returns an array identical to that of the structure returned by the C function call. PARAMETERS
o $timestamp - The optional $timestamp parameter is an integer Unix timestamp that defaults to the current local time if a $timestamp is not given. In other words, it defaults to the value of time(3). o $is_associative - If set to FALSE or not supplied then the array is returned as a regular, numerically indexed array. If the argument is set to TRUE then localtime(3) returns an associative array containing all the different elements of the structure returned by the C func- tion call to localtime. The names of the different keys of the associative array are as follows: o "tm_sec" - seconds, 0 to 59 o "tm_min" - minutes, 0 to 59 o "tm_hour" - hours, 0 to 23 o "tm_mday" - day of the month, 1 to 31 o "tm_mon" - month of the year, 0 (Jan) to 11 (Dec) o "tm_year" - years since 1900 o "tm_wday" - day of the week, 0 (Sun) to 6 (Sat) o "tm_yday" - day of the year, 0 to 365 o "tm_isdst" - is daylight savings time in effect? Positive if yes, 0 if not, negative if unknown. ERRORS
/EXCEPTIONS Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT or E_WARNING message if using the system settings or the $TZ environment variable. See also date_default_timezone_set(3) CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.1.0 | | | | | | | Now issues the E_STRICT and E_NOTICE time zone | | | errors. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 localtime(3) example <?php $localtime = localtime(); $localtime_assoc = localtime(time(), true); print_r($localtime); print_r($localtime_assoc); ?> The above example will output something similar to: Array ( [0] => 24 [1] => 3 [2] => 19 [3] => 3 [4] => 3 [5] => 105 [6] => 0 [7] => 92 [8] => 1 ) Array ( [tm_sec] => 24 [tm_min] => 3 [tm_hour] => 19 [tm_mday] => 3 [tm_mon] => 3 [tm_year] => 105 [tm_wday] => 0 [tm_yday] => 92 [tm_isdst] => 1 ) SEE ALSO
getdate(3). PHP Documentation Group LOCALTIME(3)
All times are GMT -4. The time now is 08:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy