hello, i’m new to programming in i’m trying to solve this exercise in C, basically it’s the amount of passed hours between the start of a game and it’s end, if the game started at 16 and ended at 2 the result is a game with 10 hours(in different days) i know i can to it more manually, but i wanted to somehow use the <time.h> to learn how to use a header etc, can someone help me?, thank you all

  • tunetardis@lemmy.ca
    link
    fedilink
    arrow-up
    3
    ·
    1 year ago

    The thing is to take 2 time stamps: one at the beginning and one at the end of the game. Then you subtract the beginning value from the end to get the elapsed time.

    In terms of <time.h>, you probably want to call clock() to get these time stamps.

    clock_t beginTime = clock();
    

    And later:

    clock_t endTime = clock();
    double elapsed = (double)(endTime - beginTime) / CLOCKS_PER_SEC;
    

    That would give you the time in seconds, which you could make into hours by dividing it further by 3600.

    (Note: It’s been a long time since I’ve programmed straight C so I may be off on something here? In C++, you would want to use std::chrono::steady_clock::now() to get your time stamps, even though clock() still exists.)

    • Juniper@skein.city
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      This is correct, however it is important to note that the C standard allows arbitrary values at the beginning of the program. The manpage does a better job explaining it.

      Doing a bit of research, it looks like the POSIX time_t time(time_t *dest) function (man) is available on Windows (see here). So I would recommend that over clock_t clock(void) as it will operate more consistently across platforms.

      • tunetardis@lemmy.ca
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        The arbitrary values at the beginning of the program is the reason you want to measure a difference rather than an absolute time. Like you don’t want to simply go (double)clock() / CLOCKS_PER_SEC at the end of the program. That would only work if you knew for certain that the clock started at 0 on program launch. Some operating systems may ensure this. Others may only zero it when the CPU is booted, for example. So it’s just not safe to work with absolute times, but differences between time stamps should still behave consistently across platforms.

        Now, the problem with time() and friends is they represent the kind of time displayed by your computer. In most cases, this should work fine, but occasionally, the OS may adjust this time for a number of reasons. It may be jumping ahead or back due to daylight savings, for example, or adding/losing some seconds as it syncs the computer clock to an NTP server someplace. What you want to use to avoid such shenanigans in measuring time elapsed is a steady clock which ticks along regardless of what the OS decides to do. I think clock() is your best bet for that in plain C? (It also may have a higher resolution than time(), though that’s not super important if you’re only worried about counting hours.)