Displaying the time
From: Gactimus (gactimus_at_xrs.net)
Date: 11/24/04
- Next message: Daniel Moree: "Re: Displaying the time"
- Previous message: ~ VividHazE ~: "3ds max 4 Plugin Development with C++"
- Next in thread: Daniel Moree: "Re: Displaying the time"
- Reply: Daniel Moree: "Re: Displaying the time"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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);
}
- Next message: Daniel Moree: "Re: Displaying the time"
- Previous message: ~ VividHazE ~: "3ds max 4 Plugin Development with C++"
- Next in thread: Daniel Moree: "Re: Displaying the time"
- Reply: Daniel Moree: "Re: Displaying the time"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|