16

I need to add seconds to a date. For example, if I have a date such as 2009127000000, I need to add the seconds to this date. Another example, add 50 seconds to 20091231235957.

Is this possible in C?

htoip
  • 437
  • 5
  • 19
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • I think the date format in your example is underspecified. 2009127000000 could be the 7th of December or the 27th of January. – Thomas Dec 07 '09 at 10:43
  • @benjamin, have a read of the man pages for ctime (date / time conversion functions) and strptime (converts string representation of a time to a time tm structure) – Glen Dec 07 '09 at 10:53

4 Answers4

39

In POSIX a time_t value is specified to be seconds, however that's not guaranteed by the C standard, so it might not be true on non-POSIX systems. It commonly is (in fact, I'm not sure how often it isn't a value representing seconds).

Here's an example of adding time values that doesn't assume a time_t represents seconds using the standard library facilities, which are really not particularly great for manipulating time:

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

int main()
{
    time_t now = time( NULL);

    struct tm now_tm = *localtime( &now);


    struct tm then_tm = now_tm;
    then_tm.tm_sec += 50;   // add 50 seconds to the time

    mktime( &then_tm);      // normalize it

    printf( "%s\n", asctime( &now_tm));
    printf( "%s\n", asctime( &then_tm));

    return 0;
}

Parsing your time string into an appropriate struct tm variable is left as an exercise. The strftime() function can be used to format a new one (and the POSIX strptime() function can help with the parsing).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 1
    how do I get back a `time_t` from the modified `then_tm`? – Lazer Nov 06 '10 at 16:08
  • 2
    @Lazer: `mktime` returns the `time_t` you want. – Steve Jessop Nov 06 '10 at 16:49
  • 1
    @Michael Burr. This will be true only if seconds is less then 60. I mean otherwise you have to do the entire calculation yourself – abhi Jan 20 '16 at 01:50
  • @abhi why? it works with any seconds you want: 50, 200, 3600, 86400 etc. Obviously the user will know how much time is 3600 seconds. – cesargastonec Sep 14 '18 at 20:17
  • 1
    No guys, the seconds and minutes you have to manage yourself. Adding 120 to `tm_min` gives something like `Current time: 19:56:21 will fire at: 19:176:21` using `std::put_time(my_tm,"%X")` – Vassilis Nov 11 '18 at 17:59
12

Use types and functions from <time.h>.

time_t now = time(0);
time_t now_plus_50_seconds = now + 50;
time_t now_plus_2_hours = now + 7200;

<time.h> declares functions that deal with time_t and struct tm types. These functions can do all you want.

pmg
  • 106,608
  • 13
  • 126
  • 198
8

The C date/time type time_t is implemented as the number of seconds since a certain date, so to add seconds to it you simply use normal arithmetic. If this is not what you are asking about please make your question clearer.

1

Try something like this: (Note: no error checking)

include <time.h>

char* string = ...;
char  buf[80];
struct tm;
strptime(string, "%Y%m...", &tm);
tm->tm_isdst = 0;
strftime(buf, sizeof(buf), "%Y%m...", localtime(mktime(&tm) + 50));
Steve Emmerson
  • 7,702
  • 5
  • 33
  • 59