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.

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:

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?

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

/* 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.

0.0/3

Votes:0I 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?