Displaying the time

From: Gactimus (gactimus_at_xrs.net)
Date: 11/24/04


Date: Wed, 24 Nov 2004 04:50:31 GMT

This program displays the current time on my computer's clock as well as
the number of seconds left in the day. The only problem is that all
seconds, minutes, and hours less than ten do not display a zero before the
number. Can anyone help me fix this? Code is provided below.

#ifndef TIME_H
#define TIME_H
/**
A class that describes a time of day
(between 00:00:00 and 23:59:59)
*/
class Time
{
public:
/**
Constructs a time of day.
@param hour the hours
@param min the minutes
@param sec the seconds
*/
Time(int hour, int min, int sec);
/**
Constructs a Time object that is set to
the time at which the constructor executes.
*/
Time();
/**
Gets the hours of this time.
@return the hours
*/
int get_hours() const;
/**
Gets the minutes of this time.
@return the minutes
*/
int get_minutes() const;
/**
Gets the seconds of this time.
@return the seconds
*/
int get_seconds() const;
/**
Computes the seconds between this time and another.
@param t the other time
@return the number of seconds between this time and t
*/
int seconds_from(Time t) const;
/**
Adds a number of seconds to this time.
@param s the number of seconds to add
*/
void add_seconds(int s);
private:
int time_in_secs;
};
#endif

//time.cpp
#include <iostream>
using namespace std;
#include "time.h"

int main()
{
        Time now;
        cout << now.get_hours() << ":" << now.get_minutes() << ":" << now.get_seconds() << "\n";
        Time day_end(23, 59, 59);
        long seconds_left = day_end.seconds_from(now);
        cout << "There are " << seconds_left << " seconds left in this day.\n";
        return 0;
}

//outtatime.cpp
#include <ctime>
#include <cassert>
//using namespace std;
#include "usftime.h"
/**
Computes the correct remainder for negative dividend
@param a - an integer
@param n - an integer > 0
@return the mathematically correct remainder r such that
a - r is divisible by n and 0 <= r and r < n
*/

int remainder(int a, int n)
{
        if (a >= 0)
        return a % n;
        else
        return n - 1 - (-a - 1) % n;
}

Time::Time(int hour, int min, int sec)
{
        assert(0 <= hour);
        assert(hour < 24);
        assert(0 <= min);
        assert(min < 60);
        assert(0 <= sec);
        assert(sec < 60);
        time_in_secs = 60L * 60 * hour + 60 * min + sec;
}

Time::Time()
{
        time_t now = time(0);
        struct tm& t = *localtime(&now);
        time_in_secs = 60L * 60 * t.tm_hour + 60 * t.tm_min + t.tm_sec;
}

int Time::get_hours() const
{
        return time_in_secs / (60 * 60);
}

int Time::get_minutes() const
{
        return (time_in_secs / 60) % 60;
}

int Time::get_seconds() const
{
        return time_in_secs % 60;
}

int Time::seconds_from(Time t) const
{
        return time_in_secs - t.time_in_secs;
}

void Time::add_seconds(int s)
{
        const int SECONDS_PER_DAY = 60 * 60 * 24;
        time_in_secs = remainder(time_in_secs + s, SECONDS_PER_DAY);
}



Relevant Pages

  • Re: Funky function
    ... >> It makes it possible to call this member function on a const object ... >> object may or may not be const). ... int Data; ... in the next line you try to call a function foo on MyData. ...
    (comp.lang.cpp)
  • Re: Difficulty with nested template classes (newbie)
    ... the huge post but I am including first the errors, then the template ... Reference initialized with 'const double', ... matrix& lookup (int row, int col); ... // iterator operator++ ...
    (comp.lang.cpp)
  • Re: accessor member functions and const
    ... > reference the returned value v. ... int& val ... const int& valconst ... so one cannot modify the object via the reference. ...
    (alt.comp.lang.learn.c-cpp)
  • [2.6.6-BK] NTFS 2.1.11 Really final cleanups.
    ... throughout the NTFS code to ntfschar since uchar_t is already defined by POSIX ... +BOOL find_attr(const ATTR_TYPES type, const ntfschar *name, const u32 name_len, ... register int rc; ...
    (Linux-Kernel)
  • Re: The proof that I was referring to is on the website
    ... You construct a "halt analyzer"... ... int WillHaltPO(char const * machine, ... and now we see that Naughty doesn't halt. ...
    (comp.theory)