Dave's Brain

Browse - Programming Tips - When is tomorrow?

Date: 1998jan20
Language: C/C++

Q.  When is tomorrow?

A.  Sometimes C/C++ is so nice. Say you want to find
tomorrow's date. Do you need to make a table holding
the number of days in each month - and what about leap years?
Nope, this fairly simple code does the trick:

#include <time.h> #include <stdio.h> #define SECONDS_PER_DAY (60 * 60 * 24) // Returns the date of tomorrow void Tomorrow(int &year, int &month, int &day) { time_t now; struct tm *here; now = time(NULL); // Get the current time here = localtime(&now); here->tm_hour = 12; // Set to noon to handle daylight savings - important here->tm_min = here->tm_sec = 0; now = mktime(here) + SECONDS_PER_DAY; // The key line here = localtime(&now); year = here->tm_year + 1900; month = here->tm_mon + 1; day = here->tm_mday; } main() { int year, month, day; Tomorrow(year, month, day); printf("Tomorrow is %04d.%02d.%02d\n", year, month, day); }

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.