get the a 2days time dif fromcurrent time.
-
Hi ALL, I am trying to get the timing exactly 2 days diff from current time.please advice i am not getting it correctly. here is my code (-2 does not work)
#include <time.h>
#include <stdlib.h>
#include <stdio.h>int main() {
time\_t current\_time; char\* c\_time\_string; current\_time = time(NULL); current\_time = current\_time -2 /\* Convert to local time format. \*/ c\_time\_string = ctime(¤t\_time); printf("Current time is %s", c\_time\_string); return 0;
}
-
Hi ALL, I am trying to get the timing exactly 2 days diff from current time.please advice i am not getting it correctly. here is my code (-2 does not work)
#include <time.h>
#include <stdlib.h>
#include <stdio.h>int main() {
time\_t current\_time; char\* c\_time\_string; current\_time = time(NULL); current\_time = current\_time -2 /\* Convert to local time format. \*/ c\_time\_string = ctime(¤t\_time); printf("Current time is %s", c\_time\_string); return 0;
}
http://en.cppreference.com/w/c/chrono/time[^] Read the notes carefully.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill
-
Hi ALL, I am trying to get the timing exactly 2 days diff from current time.please advice i am not getting it correctly. here is my code (-2 does not work)
#include <time.h>
#include <stdlib.h>
#include <stdio.h>int main() {
time\_t current\_time; char\* c\_time\_string; current\_time = time(NULL); current\_time = current\_time -2 /\* Convert to local time format. \*/ c\_time\_string = ctime(¤t\_time); printf("Current time is %s", c\_time\_string); return 0;
}
The time function returns the time as the number of seconds since the Epoch (1970-01-01 00:00:00 +0000 UTC). See also the C runtime time function: http://linux.die.net/man/2/time[^] (Linux) and https://msdn.microsoft.com/en-us/library/1f4c8f33.aspx[^] (Windows). So you must add / subtract 60 * 60 * 2 = 7200 seconds to apply a difference of two days.
-
The time function returns the time as the number of seconds since the Epoch (1970-01-01 00:00:00 +0000 UTC). See also the C runtime time function: http://linux.die.net/man/2/time[^] (Linux) and https://msdn.microsoft.com/en-us/library/1f4c8f33.aspx[^] (Windows). So you must add / subtract 60 * 60 * 2 = 7200 seconds to apply a difference of two days.
Oops! Try 60 * 60 * 24 * 2. btw, seconds in a day is "eight six four two zeros" ie 86 400. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
Oops! Try 60 * 60 * 24 * 2. btw, seconds in a day is "eight six four two zeros" ie 86 400. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
Ooops. Lack of coffee. Thank you for the correction.
-
Hi ALL, I am trying to get the timing exactly 2 days diff from current time.please advice i am not getting it correctly. here is my code (-2 does not work)
#include <time.h>
#include <stdlib.h>
#include <stdio.h>int main() {
time\_t current\_time; char\* c\_time\_string; current\_time = time(NULL); current\_time = current\_time -2 /\* Convert to local time format. \*/ c\_time\_string = ctime(¤t\_time); printf("Current time is %s", c\_time\_string); return 0;
}
The
time
function returns seconds since Epoch. In order to obtain two days back you have to subtract the correct amount of seconds, e.g.#include <stdio.h>
#include <time.h>#define SECONDS_PER_DAY 86400
int main()
{
time_t current_time;
char* c_time_string;current\_time = time(NULL); current\_time = current\_time - 2 \* SECONDS\_PER\_DAY; /\* Convert to local time format. \*/ c\_time\_string = ctime(¤t\_time); printf("Current time is %s\\n", c\_time\_string); return 0;
}
-
The
time
function returns seconds since Epoch. In order to obtain two days back you have to subtract the correct amount of seconds, e.g.#include <stdio.h>
#include <time.h>#define SECONDS_PER_DAY 86400
int main()
{
time_t current_time;
char* c_time_string;current\_time = time(NULL); current\_time = current\_time - 2 \* SECONDS\_PER\_DAY; /\* Convert to local time format. \*/ c\_time\_string = ctime(¤t\_time); printf("Current time is %s\\n", c\_time\_string); return 0;
}
The C-2011 standard does not specify the epoch or the resolution of time(). The POSIX standard specifies the epoch as 1970-01-01 00:00:00, and the resolution as seconds. The fact that most runtime libraries follow the POSIX standard should not blind you to the fact that using this function to perform date arithmetic is inherently non-portable. If you wish to perform date/time manipulations, you should use the functions that operate on the 'tm' structure.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill