Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#ifndef TIME_HPP
#define TIME_HPP

class Time
{
private:
    int hour;
    int minute;
    int second;

public:
    explicit Time(int = 0, int = 0, int = 0);
    ~Time();

    // Setters
    void setTime(int, int, int);
    void setHour(int);
    void setMinute(int);
    void setSecond(int);

    // Getters
    int getHour() const;
    int getMinute() const;
    int getSecond() const;

    void printStandard() const;
    void printUniversal() const;
    void tick();
};

#endif

#include <ctime>
#include <iomanip>
#include <iostream>
#include "Time.hpp"

Time::Time(int hr, int min, int sec)
{
    setTime(hr, min, sec);
}

Time::~Time() {}

void Time::setTime(int h, int m, int s)
{
    setHour(h);
    setMinute(m);
    setSecond(s);
}

void Time::setHour(int h)
{
    if (h >= 0 && h < 24)
        hour = h;
    else
        throw std::invalid_argument("hour must be 0-23");
}

void Time::setMinute(int m)
{
    if (m >= 0 && m < 60)
        minute = m;
    else
        throw std::invalid_argument("minute must be 0-59");
}

void Time::setSecond(int s)
{
    if (s >= 0 && s < 60)
        second = s;
    else
        throw std::invalid_argument("second must be 0-59");
}

int Time::getHour() const { return hour; }
int Time::getMinute() const { return minute; }
int Time::getSecond() const { return second; }

void Time::printStandard() const
{
    std::cout << ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12)
              << ":" << std::setfill('0') << std::setw(2) << getMinute() << ":"
              << std::setw(2) << getSecond() << (hour < 12 ? " AM" : " PM");
}

void Time::printUniversal() const
{
    std::cout << std::setfill('0') << std::setw(2) << getHour() << ":"
              << std::setw(2) << getMinute() << ":" << std::setw(2)
              << getSecond();
}
<pre>void Time::tick()
{
    if (getSecond() < 59)
        setSecond(getSecond() + 1);
    else
    {
        setSecond(0);
        if (getMinute() < 59)
            setMinute(getMinute() + 1);
        else
        {
            setMinute(0);
            if (getHour() < 23)
                setHour(getHour() + 1);
            else
            {
                setHour(0);
                setMinute(0);
                setSecond(0);

         }
        }
    }
};

#include <iostream>
#include "Time.hpp"

int main(int argc, char const *argv[])
{
    Time t1(23, 59, 58);

    std::cout << "t1: ";

    t1.printUniversal();

    std::cout << std::endl;

    for (int i = 0; i < 100; ++i)
    {
        t1.tick();

        if (i % 10 == 0)
        {
            t1.printUniversal();

            std::cout << " ";

            t1.printStandard();

            std::cout << std::endl;
        }
    }

    return 0;
}

What I have tried:

<pre>void Time::tick()
{
    if (getSecond() < 59)
        setSecond(getSecond() + 1);
    else
    {
        setSecond(0);
        if (getMinute() < 59)
            setMinute(getMinute() + 1);
        else
        {
            setMinute(0);
            if (getHour() < 23)
                setHour(getHour() + 1);
            else
            {
                setHour(0);
                setMinute(0);
                setSecond(0);

         }
        }
    }
};

#include <iostream>
#include "Time.hpp"

int main(int argc, char const *argv[])
{
    Time t1(23, 59, 58);

    std::cout << "t1: ";

    t1.printUniversal();

    std::cout << std::endl;

    for (int i = 0; i < 100; ++i)
    {
        t1.tick();

        if (i % 10 == 0)
        {
            t1.printUniversal();

            std::cout << " ";

            t1.printStandard();

            std::cout << std::endl;
        }
    }

    return 0;
}
Posted
Updated 17-Mar-22 3:11am

It simply increments current seconds, if such increment crosses the minute boundary, resets current seconds and then increments current minutes, if such increments crosses the hour boundary...
 
Share this answer
 
Comments
Greg Utas 17-Mar-22 9:12am    
5.
It appears that tick is called to advance the time by 1 second. If the current clock time shows 58 seconds or less, you just increment the number of seconds. But if it's at 59 seconds, it has to wrap around to 0, and you then have to go to the next minute, and possibly the next hour, and possibly the next day (which isn't tracked, but which just wraps around to 00:00:00).
 
Share this answer
 
Comments
CPallini 17-Mar-22 9:34am    
5. :-)
Greg Utas 17-Mar-22 9:57am    
You type faster than I do. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900