Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The guy's asking for the <em>simplest</em> way. All your ways are unnecessarily complex. Just use modular arithmetic:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;time.h&gt; #define TARGET_TIME 21*60 // 9:00 PM, in format HH * 60 + MM int main(int argc, char* argv[]) { struct tm* tm; time_t ts = time(NULL); int minutes_until_9pm; tm = localtime(&amp;ts); minutes_until_9pm = 24*60 - (tm-&gt;tm_min + tm-&gt;tm_hour * 60 + TARGET_TIME)%(24*60); //alternatively, the following is slightly faster //minutes_until_9pm = 24*60 - (tm-&gt;tm_min + tm-&gt;tm_hour * 60 + TARGET_TIME) + (tm-&gt;tm_min+tm-&gt;tm_hour*60+TARGET_TIME&gt;24*60?24*60:0); printf("%d minutes til (next)9pm\n", minutes_until_9pm); return 0; } </code></pre> <p>If you know your timezone , it all becomes even simpler:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;time.h&gt; #define TARGET_TIME 21*60 // 9:00 PM, in format HH * 60 + MM #define TIMEZONE (-6) // This is CST; replace with your own timezone int main(int argc, char* argv[]) { int minutes_until_9pm = 24*60 - (time(NULL)/60 + TARGET_TIME + (24+TIMEZONE)*60)%(24*60); printf("%d minutes til (next)9pm\n", minutes_until_9pm); return 0; } </code></pre> <p>What's going on here is that, modulo one day, we're getting number of minutes since midnight. Then we're adding 9:00 PM to get the the number of minutes since 9:00 PM. Then we're subtracting that from 24 hours to get the number of minutes until the next 9:00 PM.</p> <p>If you need higher accuracy (i.e. rounded to the nearest number of minutes, rather than simply number of minutes), simply subtract <code>(tm-&gt;tm_sec&gt;=30?0:1)</code> from <code>minutes_until_9pm</code>, or replace <code>time(NULL)/60</code> with <code>(time(NULL)+30)/60</code>, respectively.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload