HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Calculate Age in Seconds

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
secondsagecalculate

Problem

I am a high school math and science teacher. I am working with my students on the concepts of algorithms, unit conversions, and functions. In class, to work on all these concepts, we just worked out the process of how to calculate how old someone is in seconds. I thought this was a particularly interesting algorithm to feed to a computer, so I spent the next couple of days working on this in my spare time.

I have not sanitized my user input or done any internal error checking, and the code assumes they were born at exactly 00:00:00 on their day of birth. Laziness on my party, I guess. I am most concerned with "Is there a better way to calculate my days offset?" This is either the number of days until their birthday this year or since their birthday this year. There is a specific function or three for this, as you can see below in the code.

Additionally, are there any bits of particularly cringy (my term) code?

I had to google how to get current date and time ("c++ date time", first link). So I totally just stole that bit of code.

Thank you very much. I tried to comment clearly, but occasionally I might have been less commenty than desirable.

```
/*
* For use by absolutely anyone for absolutely any reason.
*/

/*
* File: main.cpp
* Author: Wayman Bell III
*
* Created on March 31, 2016
*/

#include
#include

using namespace std;

int getTheYear();
int getTheMonth();
int getTheDay();
int getTheHour();
int getTheMinute();
int getTheSecond();
int countLeapYears(int, int);
int welcome();
int calcAgeInSeconds(int, int, int);
int calcOffset(int, int);
int calcDaysRemainingThisYear(int, int);
int calcDaysSinceBDay(int, int);

int main(int argc, char** argv)
{

welcome();
return 0;
}

int getTheYear( ) //Retrieve current year
{
// current date/time based on current system
time_t now = time(0);

tm *ltm = localtime(&now);

return (1900+ltm->tm_year); // print various components of tm structure.
/*cout tm_year tm_mon); // print

Solution

Non-atomic time problem

I just wanted to point out one thing. You currently have a few places where you do this:

int curYear  =   getTheYear();
int curMon   =   getTheMonth();
int curDay   =   getTheDay();
int curHour  =   getTheHour();
int curMin   =   getTheMinute();
int curSec   =   getTheSecond();


Each of these functions gets the current time using time(0) and then extracts the particular aspect of the time. However, this is unsafe because if you happen to run this code at a time where a wraparound occurs, you could get inconsistent results for your 6 variables.

For example, consider what happens if you ran the code at 12/31/2015 23:59:59, and while you were getting the 6 variables, the time incremented by one second. You could end up thinking the date was one of these 5 possibilities, all of which are wrong by varying degrees:
1/ 1/2015 00:00:00 (Off by one year )
12/ 1/2015 00:00:00 (Off by one month )
12/31/2015 00:00:00 (Off by one day )
12/31/2015 23:00:00 (Off by one hour )
12/31/2015 23:59:00 (Off by one minute)


To fix this, you should read the time once, and then pass the time into your functions:

static inline int getTheYear(const tm *ltm)
{
    return 1900 + ltm->tm_year;
}

static inline int getTheMonth(const tm *ltm)
{
    return 1 + ltm->tm_mon;
}

// ...

{
    time_t now      = time(0);
    tm    *ltm      = localtime(&now);
    int    curYear  = getTheYear(ltm);
    int    curMon   = getTheMonth(ltm);
    // ...
}

Code Snippets

int curYear  =   getTheYear();
int curMon   =   getTheMonth();
int curDay   =   getTheDay();
int curHour  =   getTheHour();
int curMin   =   getTheMinute();
int curSec   =   getTheSecond();
static inline int getTheYear(const tm *ltm)
{
    return 1900 + ltm->tm_year;
}

static inline int getTheMonth(const tm *ltm)
{
    return 1 + ltm->tm_mon;
}

// ...

{
    time_t now      = time(0);
    tm    *ltm      = localtime(&now);
    int    curYear  = getTheYear(ltm);
    int    curMon   = getTheMonth(ltm);
    // ...
}

Context

StackExchange Code Review Q#124898, answer score: 8

Revisions (0)

No revisions yet.