Coder Social home page Coder Social logo

turbo1337gs / gstime.hpp Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 6 KB

This header file defines a C++ class called GStime that represents a time in hours, minutes, and seconds. It provides various member functions and overloaded operators to perform operations on time objects.

License: MIT License

C++ 100.00%

gstime.hpp's Introduction

GStime.hpp

This header file defines a C++ class called GStime that represents a time in hours, minutes, and seconds. It provides various member functions and overloaded operators to perform operations on time objects.

Class Description

The GStime class has the following private member variables:

  • hours: represents the number of hours
  • minutes: represents the number of minutes
  • seconds: represents the number of seconds

The class provides the following public member functions:

  • GStime(): default constructor that initializes the time to 0 hours, 0 minutes, and 0 seconds.
  • GStime(int h, int m, int s): constructor that allows initializing the time with custom values.
  • void setTime(int h, int m, int s): sets the time to the specified values.
  • int getHours() const: returns the number of hours.
  • int getMinutes() const: returns the number of minutes.
  • int getSeconds() const: returns the number of seconds.
  • void addSeconds(int s): adds the specified number of seconds to the time.
  • void addMinutes(int m): adds the specified number of minutes to the time.
  • void addHours(int h): adds the specified number of hours to the time.
  • void subtractSeconds(int s): subtracts the specified number of seconds from the time.
  • void subtractMinutes(int m): subtracts the specified number of minutes from the time.
  • void subtractHours(int h): subtracts the specified number of hours from the time.
  • GStime operator+(const GStime& other) const: overloads the + operator to add two time objects.
  • GStime operator-(const GStime& other) const: overloads the - operator to subtract two time objects.
  • GStime& operator+=(const GStime& other): overloads the += operator to add another time object to the current time object.
  • GStime& operator-=(const GStime& other): overloads the -= operator to subtract another time object from the current time object.
  • GStime operator+(int seconds) const: overloads the + operator to add a specified number of seconds to the time object.
  • GStime operator-(int seconds) const: overloads the - operator to subtract a specified number of seconds from the time object.
  • GStime& operator++(): overloads the prefix ++ operator to increment the time object by 1 second.
  • GStime operator++(int): overloads the postfix ++ operator to increment the time object by 1 second and return the previous value.
  • GStime& operator--(): overloads the prefix -- operator to decrement the time object by 1 second.
  • GStime operator--(int): overloads the postfix -- operator to decrement the time object by 1 second and return the previous value.
  • bool operator==(const GStime& other) const: overloads the == operator to compare two time objects for equality.
  • bool operator!=(const GStime& other) const: overloads the != operator to compare two time objects for inequality.
  • bool operator<(const GStime& other) const: overloads the < operator to compare two time objects.
  • bool operator>(const GStime& other) const: overloads the > operator to compare two time objects.
  • bool operator<=(const GStime& other) const: overloads the <= operator to compare two time objects.
  • bool operator>=(const GStime& other) const: overloads the >= operator to compare two time objects.
  • int toSeconds() const: converts the time object to the total number of seconds.
  • static GStime fromSeconds(int seconds): creates a new time object from the specified number of seconds.
  • int getTotalMinutes() const: returns the total number of minutes represented by the time object.
  • int getTotalHours() const: returns the total number of hours represented by the time object.
  • GStime getTimeDifference(const GStime& other) const: returns the time difference between the current time object and another time object.
  • GStime to12HourFormat() const: converts the time object to the 12-hour format.
  • std::string toAMPMFormat() const: returns the time object in the format "HH:MM:SS AM/PM".
  • std::string toString() const: returns the time object in the format "HH:MM:SS".
  • std::string toFullString() const: returns the time object in the format "HH:MM:SS AM/PM".

The class also provides several static member functions for common time operations, such as calculating the sum of multiple time objects, adding or subtracting time from a given time object, getting the difference between two time objects in seconds, minutes, or hours, and checking if a time is valid or empty.

Operator Overloading

The GStime class overloads the following operators:

  • +, -, +=, -=, ++, -- for performing arithmetic operations on time objects.
  • ==, !=, <, >, <=, >= for comparing time objects.
  • << for outputting the time object to an output stream.

Additional Functions

The header file also defines several global functions that operate on time objects:

  • GStime operator+(int seconds, const GStime& time): overloads the + operator to add a specified number of seconds to a time object.
  • GStime operator-(int seconds, const GStime& time): overloads the - operator to subtract a specified number of seconds from a time object.
  • GStime operator*(const GStime& time, int factor): overloads the * operator to multiply a time object by a factor.
  • GStime operator*(int factor, const GStime& time): overloads the * operator to multiply a time object by a factor.
  • GStime operator/(const GStime& time, int divisor): overloads the / operator to divide a time object by a divisor.
  • GStime operator%(const GStime& time, int divisor): overloads the % operator to get the remainder of dividing a time object by a divisor.
  • bool operator<(const GStime& time, int seconds): overloads the < operator to compare a time object with a specified number of seconds.
  • bool operator>(const GStime& time, int seconds): overloads the > operator to compare a time object with a specified number of seconds.
  • bool operator<=(const GStime& time, int seconds): overloads the <= operator to compare a time object with a specified number of seconds.
  • bool operator>=(const GStime& time, int seconds): overloads the >= operator to compare a time object with a specified number of seconds.
  • bool operator==(const GStime& time, int seconds): overloads the == operator to compare a time object with a specified number of seconds.
  • bool operator!=(const GStime& time, int seconds): overloads the != operator to compare a time object with a specified number of seconds.

Example Usage

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

int main() {
    GStime time1(1, 30, 45);
    GStime time2(0, 45, 30);
    GStime time3 = time1 + time2;

    std::cout << "Time 1: " << time1 << std::endl;
    std::cout << "Time 2: " << time2 << std::endl;
    std::cout << "Time 3: " << time3 << std::endl;

    if (time1 == time2) {
        std::cout << "Time 1 is equal to Time 2" << std::endl;
    } else {
        std::cout << "Time 1 is not equal to Time 2" << std::endl;
    }

    time3 += GStime(0, 0, 15);
    std::cout << "Time 3 after adding 15 seconds: " << time3 << std::endl;

    time3 += GStime(0, 20, 0); 
    std::cout << "Time 3 after adding 20 minutes: " << time3 << std::endl;

    time3 += GStime(2, 0, 0); 
    std::cout << "Time 3 after adding 2 hours: " << time3 << std::endl;

    GStime time4 = time3 - GStime(0, 40, 0); 
    std::cout << "Time 4 after subtracting 40 minutes from Time 3: " << time4 << std::endl;

    GStime time5 = GStime(0, 0, 10) + time2; 
    std::cout << "Time 5 after adding 10 seconds to Time 2: " << time5 << std::endl;

    GStime timeArray[] = { time1, time2, time3, time4, time5 };
    GStime totalTime = GStime::sumTimes(timeArray, sizeof(timeArray) / sizeof(GStime));
    std::cout << "Total time: " << totalTime << std::endl;

    return 0;
}

Output

Time 1: 1h 30m 45s
Time 2: 0h 45m 30s
Time 3: 2h 16m 15s
Time 1 is not equal to Time 2
Time 3 after adding 15 seconds: 2h 16m 30s
Time 3 after adding 20 minutes: 2h 36m 30s
Time 3 after adding 2 hours: 4h 36m 30s
Time 4 after subtracting 40 minutes from Time 3: 3h 56m 30s
Time 5 after adding 10 seconds to Time 2: 0h 45m 40s
Total time: 9h 55m 50s

gstime.hpp's People

Contributors

turbo1337gs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.