In order to calculate the current time of day (or nearly anything involving time, really), you can utilize the ctime library's function time(nullptr). The value returned by this function represents the number of seconds elapsed since [the first] midnight ...of January 1, 1970 — a date known as the Unix epoch ...in Greenwich, England — GMT (Greenwich Mean Time).
It will return this value as the data type time_t (which is also defined in the ctime library). time_t is simply a standardized name for one of the integral types, but it is safer to use it than to choose the integral type you think is appropriate for such a quantity, since different compilers/library implementors may decide on different integral types to have it represent. That is, time_t may represent an int on one system but a long on another. (On still another system, they may have chosen long long — looking forward to the day 32 bits runs out of space.)
The argument value nullptr should always be passed to the time function. The count of seconds does not account for Daylight Savings Time. The count of seconds does include leap days. However, it seems highly doubtful that it includes leap seconds. (Maybe you should do some research about it...*shrug* I would count just like a biography report for extra credit...)
Note how we let the compiler build the composite constants instead of doing the computations by hand and potentially entering the wrong value (like entering 360 instead of the correct 3600 for seconds_per_hour; trust me, it can happen!).
Also note how the last constant is long instead of short. It takes a little thought to realize that this is necessary, but you don't need the exact figure to do it, really. Start by noting that 6*6*2=>72. Then attach the three zeros (since those were the tens digits): 72000. This is going to be outside the range of a short — even unsigned short! The type-cast is [sadly] necessary since the compiler will do purely short integer arithmetic unless one of the factors is 'turned into' a long integer.
Not easily. But, then again, it's not that tricky, either.
If we make ourselves a couple of constants:
const short seconds_per_minute = 60, minutes_per_hour = 60, seconds_per_hour = seconds_per_minute * minutes_per_hour, hours_per_day = 24; const long seconds_per_day = static_cast<long>(seconds_per_hour) * hours_per_day;
Utilizing these constants, we can manipulate a time_t value from time() to make it more useful:
long sec_today = time(nullptr) % seconds_per_day; short hour_now = sec_today / seconds_per_hour, minute_now = sec_today % seconds_per_hour / seconds_per_minute, second_now = sec_today % seconds_per_hour % seconds_per_minute;
Well, having obtained the time of day so simply, how can we get today's date? That's a whole other ball of monkeys! (*grin*)
To begin, we need to find the number of whole days that have passed since the epoch. This is just the opposite of our sec_today calculation above:
long days_since_epoch = time(nullptr) / seconds_per_day;
Next we'll need to break this large number of days into years, months, and days. It seems we're set up for another easy round of division and modulo, but the groupings are not regular enough to lend themselves to easy calculation. Notice, for instance, that every month is a different number of days — and they don't even alternate properly!
The closest thing to a regular pattern we have is that every four years have 365*4+1 days, but that's not much to go on.
I suppose we could use a loop to remove the days for a whole year deciding as we go which years are leap and therefore need an extra day removed. Something like:
year = 1970;
days = days_since_epoch + 1; // current day isn't complete but counts!
while ( days >= days_per_year )
{
days -= days_per_year;
if ( is_leap( year ) )
{
days--;
}
year++;
}
We'll just have to figure out what it means to be a leap year and incorporate that into our nested if condition.
Once those days that represent whole years are removed, we just need to remove the ones that represent whole months. This is more difficult since the months are so irregular. We'll need a set of branches each tuned to the days for a different month to remove the months in order. (It is important to remove the months from January forward because we only want to remove the days for those months which have actually passed.) Note also that these branches will not be cascaded but sequenced:
month = 1; // you'd think 0, but you aren't ever gonna // reach that December if since your days // are less than a whole year... if ( days >= Jan_days ) { days -= Jan_days; month++; } if ( days >= Feb_days ) { days -= Feb_days; month++; } if ( days > 0 && is_leap( year ) ) { days--; } if ( days >= Mar_days ) { days -= Mar_days; month++; } // . . .
Note that we don't need to check for the leap day this year until we've successfully passed the standard days of February but definitely before we dive into March.
I'll leave it for you to fill in the rest of the code (including the is_leap function) and the actual definitions for all those *_days constants. (I'd suggest using an enumeration as mentioned in the style guidelines.)
What about figuring out what day of the week it is? Not as horrible, but still not awesomely easy...
With a little searching ...and a little reading ...and a little calculation we find that the Unix epoch was a Thursday. Recalling from the research/reading we did that there are seven (7) days in a week so that every seven days from the epoch would also have been a Thursday (Jan 8, 1970; Jan 15, 1970; etc.). And recalling our earlier work with converting seconds to other units, we realize that this would simply be a matter of converting days to weeks and days — i.e. 25 days would be 3 weeks and 4 extra days. So...
Note that the idea of a group of constants mentioned on the style guidelines could come in handy here...
Okay, then, what about that whole GMT and/or DST thing you mentioned? Can we at least take care of that?! *sigh* Mostly...
First let's figure out what GMT vs. CST is by doing a little calculation. Then we can make an attempt to figure out Daylight Savings Time ...before giving up and just making the one hour adjustment by hand twice each year. (*grmbl* *$^#%@!* government mucking about with time! Bad enough when they mess with morality or money...*phbbt* *shrug* *sigh* *grin*)