01 Oct, 2009, Zeno wrote in the 1st comment:
I want to print the number of hours/minutes until 5pm the next day.

I was working on using mktime to do this, but then realized mktime asks for a specific month and day and I need to make it be every day. Then I figured I could use date() in mktime, but then realized I couldn't just do +1 on the day in case it's the end of the month.

Easiest way to do this?
01 Oct, 2009, David Haley wrote in the 2nd comment:
Why do you need to go to tomorrow's date in the first place?

Take the current time in minutes since midnight: say, 13:01 i.e. 781 minutes.
Take 5pm tomorrow in minutes since midnight today: 24h + 17h = 41h * 60 = 2460 minutes.
Subtract the numbers: 1679 minutes until 5pm tomorrow.
02 Oct, 2009, bbailey wrote in the 3rd comment:
Zeno said:
I want to print the number of hours/minutes until 5pm the next day.

I was working on using mktime to do this, but then realized mktime asks for a specific month and day and I need to make it be every day. Then I figured I could use date() in mktime, but then realized I couldn't just do +1 on the day in case it's the end of the month.

Easiest way to do this?

`#include <stdio.h>#include <sys/time.h>#include <time.h>int main( void ) {  struct timeval now;  struct tm *curr_time;  gettimeofday(&now, 0);  curr_time = localtime(&now.tv_sec);  printf("Roughly %d hours until 5pm tomorrow!\r\n", (17-curr_time->tm_hour)+24);  return 0;}`

It's probably worth noting that I took "5pm the next day" literally. The calculation's fairly simple – 5pm (17 hours) today minus the current hour, plus 24 hours to bump it to tomorrow. If you want it to just calculate time until the next 5pm (either today, or tomorrow if it's already occured), you can just add a little extra logic.

`/* a little extra (naive) logic */if (curr_time->tm_hour < 17) {  printf("Roughly %d hours until 5pm today!\r\n", (17-curr_time->tm_hour));} else if (curr_time->tm_hour == 17) {  printf("It's 5pm!\r\n");} else {  printf("Roughly %d hours until 5pm tomorrow!\r\n", (17-curr_time->tm_hour)+24);}`

You can of course gain better resolution by taking advantage of tm_min and tm_sec in struct tm. Hopefully this should be enough to get you started.
Random Picks

0.0/3