-2

Timestamp is defined as the number of seconds elapsed since midnight of January 1, 2000, UTC. Using standard C only, how to get unix timestamp with epoch of Jan 1, 2000 UTC and not Jan 1, 1970?

time(NULL) returns seconds since January 1, 1970.

time_t seconds;
seconds = time(NULL); 
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Syler
  • 1,151
  • 1
  • 13
  • 30
  • 2
    Looks like a homework. So what do you think? Come on, this one is easy. :-) – Al Kepp Oct 09 '17 at 22:39
  • 3
    Standard C does not define the epoch; it says only that `time_t` is a real type (integer or floating-point) capable of representing times. POSIX does define the Epoch as Jan 1, 1970. – Keith Thompson Oct 09 '17 at 22:43
  • @AlKepp I posted my answer below :) the key was figuring out the epoch / unix time stamp for year other than 1970 (which is 0) – Syler Oct 09 '17 at 22:56
  • @KeithThompson I realized that after spending several hours!! – Syler Oct 09 '17 at 23:06
  • Just subtract the number of seconds between 2000-01-01T00:00:00Z and 1970-01-01T00:00:00Z – ikegami Oct 09 '17 at 23:56
  • @ikegami yup it works, instead of using the - operator that was suggested below, I am using difftimes. Wow I did not expect so many replies on this one. the question seems fair enough does not deserve -2 points. – Syler Oct 10 '17 at 18:02
  • @KeithThompson is the answer below satisfactory? If not I can modify it otherwise I like to mark it as the solution. I couldn't find this anywhere on SO so it should help other folks. – Syler Oct 10 '17 at 21:56
  • @ikegami can you take a look at the answer posted below? is there anything that should be changed? if not i wanted to mark it as complete. – Syler Oct 16 '17 at 15:16
  • @Syler, It is correct. – ikegami Oct 16 '17 at 15:24

1 Answers1

-3

I thought someone else might benefit from the time I spent in trying to figure this out. If you have a requirement where in standard C you want to find time elapsed since epoch other than January 1, 1970, then this how you do it,

time_t secondsY2K = 946684800; // Epoch / Unix timestamp for Jan 1, 2000 https://www.epochconverter.com/timestamp-list
time_t unixEpochTime;
time(&unixEpochTime);
double seconds = difftime(unixEpochTime , epochY2k);
printf("seconds since jan 1, 2000 %.f", seconds);
printf("current time in UTC %s\n", asctime(gmtime(&unixEpochTime)));        
Syler
  • 1,151
  • 1
  • 13
  • 30
  • 2
    here is what the `man` page says about the function: `time()` *time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). If tloc is non-NULL, the return value is also stored in the memory pointed to by tloc.* So, NO, the parameter is not used to change the epoch. – user3629249 Oct 09 '17 at 22:55
  • 1
    And then, how about using this wonderful `-` operator instead? –  Oct 09 '17 at 23:04
  • @user3629249, look at the epoch / unix time stamp chart link i posted, 1970, January 1 has a unix /epoch timestamp of 0 (zero) and that of 2000, January 1 is 946684800. These timestamps might not be exact (leap seconds, etc.) calling time(NULL); passes 0 for seconds thus you get time elapsed since 1970. – Syler Oct 09 '17 at 23:29
  • @FelixPalmen: C does not guarantee that subtracting `time_t` values has a meaningful result. `time_t` has to be a real (integer or floating) type able to represent times, but it needn't do so linearly. That's why there's a `difftime()` function. But if you don't need absolute portability, you can assume that `time_t` values advance linearly, and you'll *usually* be right (POSIX requires `time_t` to be an integer type representing time in seconds.) – Keith Thompson Oct 10 '17 at 01:11
  • @KeithThompson The whole question already implicitly assumes POSIX, it would be uttlery meaningless otherwise. So why bother using `difftime()` when you already assume `time_t` is in seconds and 0 means the Unix epoch? –  Oct 10 '17 at 05:11
  • @FelixPalmen subtracting does not work and like keith has pointed out results in unexpected values. the answer i have given is working well. – Syler Oct 10 '17 at 05:16
  • @Syler subtracting **does** work, Keith is talking about "esoteric" implementations that have a non-linear `time_t`. On a POSIX system, it IS linear (well, as linear as time itself). But indeed, argument order is correct, I had the wrong assumption here. –  Oct 10 '17 at 05:18
  • @Syler again, to know more you should read man-pages, e.g. [`difftime(3)`](http://man7.org/linux/man-pages/man3/difftime.3.html) tells you: *On a POSIX system, time_t is an arithmetic type, and one could just define `#define difftime(t1,t0) (double)(t1 - t0)`*. –  Oct 10 '17 at 05:21
  • 1
    If you can assume that Y2K is `946684800`, you can almost certainly assume that subtraction on `time_t` values works as expected. (If you want your code to be painfully portable, you can use `mktime()`.)\ – Keith Thompson Oct 10 '17 at 16:24
  • well the subtraction works in a way but the solution is ugly in the sense that the t = t1 - t2, the t variable has meaningless data pointing back 30 years to 1987, I cannot use this value anywhere. passing a timer_t *t parameter, to time function, it populates the parameter with datetime. When reading timestamp values stored as seconds elapsed after Y2k, i just add 946684800. it works well. – Syler Oct 10 '17 at 17:35
  • @FelixPalmen is there anything else you want changed in this answer? if not I like to mark it as complete and close this thread. – Syler Oct 16 '17 at 15:18
  • @FelixPalmen, Sure, subtraction would work. So what. – ikegami Oct 16 '17 at 15:24