Initialize Time class using the current time from the time and localtime functions - C++ Class

C++ examples for Class:Constructor

Description

Initialize Time class using the current time from the time and localtime functions

Demo Code

                                                                                                                                            
#include <ctime>
#include <iomanip>
#include <iostream>
#include <stdexcept>
class Time {/*from  w w  w  . j  ava  2s .  c om*/
 private:
    int hour;
    int minute;
    int second;
                                                                                                                                            
 public:
    Time();
    Time(int, int, int);
                                                                                                                                            
    // SETTERS
    void setTime(int, int, int);
    void setHour(int);
    void setMinute(int);
    void setSecond(int);
                                                                                                                                            
    // GETTERS
    int getHour();
    int getMinute();
    int getSecond();
                                                                                                                                            
    void printUniversal();
    void printStandard();
};
                                                                                                                                            
Time::Time() {
    time_t currentTime;
    struct tm *localTime;
                                                                                                                                            
    time(&currentTime);  // get current time
                                                                                                                                            
    localTime = localtime(&currentTime);  // convert to local time
                                                                                                                                            
    hour = localTime->tm_hour;
    minute = localTime->tm_min;
    second = localTime->tm_sec;
}
Time::Time(int hour, int minute, int second) { setTime(hour, minute, second); }
// SETTERS
// set new Time value using universal time
void Time::setTime(int h, int m, int s) {
    setHour(h);
    setMinute(m);
    setSecond(s);
}
// set hours
void Time::setHour(int h) {
    if (h >= 0 && h < 24) {
        hour = h;
    } else {
        throw std::invalid_argument("hour must be 0-23");
    }
}
// set minutes
void Time::setMinute(int m) {
    if (m >= 0 && m < 60) {
        minute = m;
    } else {
        throw std::invalid_argument("minute must be 0-59");
    }
}
// set seconds
void Time::setSecond(int s) {
    if (s >= 0 && s < 60) {
        second = s;
    } else {
        throw std::invalid_argument("second must be 0-59");
    }
}
// GETTERS
int Time::getHour() { return hour; }
int Time::getMinute() { return minute; }
int Time::getSecond() { return second; }
// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal() {
    std::cout << std::setfill('0') << std::setw(2) << getHour() << ":"
              << std::setw(2) << getMinute() << ":" << std::setw(2)
              << getSecond();
}
// print Time in standard-time format (H:MM:SS AM PM)
void Time::printStandard() {
    std::cout << ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12)
              << ":" << std::setfill('0') << std::setw(2) << getMinute() << ":"
              << std::setw(2) << getSecond() << (hour < 12 ? " AM" : " PM");
}
#include <iostream>
                                                                                                                                            
int main(int argc, const char *argv[]) {
    Time time1;
    Time time2(12, 12, 12);
                                                                                                                                            
    std::cout << "time1: ";
    time1.printUniversal();
    std::cout << std::endl;
    time1.printStandard();
                                                                                                                                            
    std::cout << "\n\ntime2: ";
    time2.printUniversal();
    std::cout << std::endl;
    time2.printStandard();
    std::cout << std::endl;
                                                                                                                                            
    return 0;
}

Result


Related Tutorials