2

So i am getting my time from an api that returns epoch time and i need to pass that time into a Real Time Clock function which accepts this dateTime structure

datetime_t t = {         // Friday June 5,  Hour 15, Minute 45, Sec 00
        .year  = 2020,
        .month = 06,
        .day   = 05,
        .dotw  = 5, // 0 is Sunday, so 5 is Friday
        .hour  = 15,
        .min   = 45,
        .sec   = 00
};

I need help creating a formula to get those individual values. I think i have made a formula for the easy ones.

sec  = epochTime%60
min  = floor((epochTime%3600)/60)
hour = floor((epochTime%86400)/3600)

as for the others it is not that easy anymore as there are leap years and such. i have to do this with only standard libraries or you can suggest a web api that returns me those value (it has to be only 1 api for all of those data)

DrakeJest
  • 225
  • 3
  • 13
  • 1
    As you mentioned leap days... There are also leap seconds which are inserted sometimes (at the end of certain years). FYI: [Leap second](https://en.wikipedia.org/wiki/Leap_second) ;-) – Scheff's Cat Nov 18 '21 at 12:22
  • @Scheff'sCat does epoch account for that? or epoch is really the number of second elapse since epoch? – DrakeJest Nov 18 '21 at 12:26
  • From the link: _UTC leap seconds are irregularly spaced and unpredictable._ – Scheff's Cat Nov 18 '21 at 12:27
  • *"i have to do this with only standard libraries"* Which language? Please use correct language tag for the language you actually use, and remove others. – user694733 Nov 18 '21 at 12:31
  • FYI: [C Date and time utilities](https://en.cppreference.com/w/cpp/chrono/c) or [C++ Date and time utilities](https://en.cppreference.com/w/cpp/chrono) – Scheff's Cat Nov 18 '21 at 12:33
  • are you using c or c++ they are separate languages, please just tag one – Alan Birtles Nov 18 '21 at 12:41
  • Sorry about the language, i was only looking for a formula hence i used both since the syntaxes are interchangable (or atleast very easy to convert). – DrakeJest Nov 18 '21 at 13:26
  • 1
    Is the epoch the UNIX epoch, i.e. 1970/1/1 00:00:00 UTC? – dbush Nov 18 '21 at 13:27
  • [check this post](https://stackoverflow.com/questions/18582119/how-to-convert-unix-timestamp-into-daymonthdateyear-format-in-c) – Ptit Xav Nov 18 '21 at 13:28
  • DrakeJest, Please provide more details about your "api that returns epoch time". – chux - Reinstate Monica Nov 18 '21 at 14:26

1 Answers1

1

Assuming the epoch time is the UNIX epoch, i.e. seconds since 1970/01/01 00:00:00 UTC, you can use the localtime function. This function takes the address of a time_t containing epoch time and splits it into its component values, assuming the local timezone:

struct tm *tm;
time_t t = time(NULL);
tm = localtime(&t);

The returned structure is defined as follows:

       struct tm {
           int tm_sec;         /* seconds */
           int tm_min;         /* minutes */
           int tm_hour;        /* hours */
           int tm_mday;        /* day of the month */
           int tm_mon;         /* month */
           int tm_year;        /* year */
           int tm_wday;        /* day of the week */
           int tm_yday;        /* day in the year */
           int tm_isdst;       /* daylight saving time */
       };

Where tm_year is years since 1900, tm_mon is in the range 0-11, and tm_wday is in the range 0-6 (Sun-Sat).

If you want UTC time, you can use gmtime instead with the same parameters / return type.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Got it to work ! Thank you sir ! , This does accomodate leap years and seconds right>? – DrakeJest Nov 18 '21 at 13:59
  • Re "*This does accomodate leap years*", yes. Re "*and seconds right*", I believe epoch time is the number of seconds since the start of 1970 UTC not counting leap seconds. So there's no input that will give 60 for `tm_sec`. – ikegami Nov 18 '21 at 14:13
  • Detail: In C, "epoch time is the number of seconds since the start of 1970 UTC not counting leap seconds" is not so specified, that is some implementations doing (although common). `.tm_sec` range is "int tm_sec; // seconds after the minute -- [0, 60]"C17 § 7.27.1 4. Ignoring such implements a future bug in OP's code, yet without more details form OP, not much can be done but to live (and die) with assumptions. – chux - Reinstate Monica Nov 18 '21 at 14:29
  • @chux - Reinstate Monica, hmmm, that would mean there could be a 27 s difference in the meaning of current epoch times between systems. Thankfully, I've never encountered this. e.g. 1637245960 means 2021-11-18T14:32:40Z on all platforms I've encountered, but you're saying it could mean 2021-11-18T14:32:13Z on others. oof. That's a pretty massive drift. – ikegami Nov 18 '21 at 14:29
  • We just do not have details about OP's _epoch time_. Many possibilities exist. Your approach overall is good - just looking to plug those various time holes for pieces of medical equipment someone writes code for today that goes on the blink in [2038](https://en.wikipedia.org/wiki/Year_2038_problem) - just when I need it. – chux - Reinstate Monica Nov 18 '21 at 14:34
  • @chux - Reinstate Monica, Re "*We just do not have details about OP's epoch time.*", These comments are in response to a post saying "Assuming the epoch time is the UNIX epoch" – ikegami Nov 18 '21 at 14:35
  • My comments is a reply to the [comment](https://stackoverflow.com/questions/70019726/epoch-time-to-yy-mm-dd-dotw-hh-mm-ss-covertion-formula#comment123778560_70020777) that does not re-assert that far away placed assumption. IAC, the assumption is not needed. If _epoch time_ counted leap seconds, was of a different epoch than 1970, etc. , `localtime()` would still work to meet OP's goal given that function expects such, following code just needs to handle `.tm_sec == 60`. – chux - Reinstate Monica Nov 18 '21 at 14:40
  • @chux-ReinstateMonica Apologies its been a while, and i was blissfully not aware that there might be a problem since everything looks fine so far. So since then i have been switching the API for the epoch time. Current I am using [this api](http://worldtimeapi.org/) I am parsing the `unixtime` which will then be passed to the code that dbush made. So far it has been working great, The time i get is the same as on my windows pc and my smartphone. – DrakeJest Nov 25 '21 at 12:33