Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 06 / Event / Time.cpp
diff options
context:
space:
mode:
Commitd3c133e918cb85933c961c31871d038cace6b638 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2022-03-29 20:00:49 +0300
Committer
 GitHub <noreply@github.com>
2022-03-29 20:00:49 +0300
Files200177f14d906fe71e7745aec9799c4c0b11171f /Sem. 06/Event/Time.cpp
Object-oriented_programming_FMI-d3c133e918cb85933c961c31871d038cace6b638.tar
Object-oriented_programming_FMI-d3c133e918cb85933c961c31871d038cace6b638.tar.gz
Object-oriented_programming_FMI-d3c133e918cb85933c961c31871d038cace6b638.zip
Parent4b875f477c0eaa55ee1ea81949af6c6010249888 (diff)
Add files via upload
1 files changed, 126 insertions, 0 deletions
-rw-r--r--Sem. 06/Event/Time.cpp126
diff --git a/Sem. 06/Event/Time.cpp b/Sem. 06/Event/Time.cpp
new file mode 100644
index 0000000..5c8fe44
--- /dev/null
+++ b/Sem. 06/Event/Time.cpp
@@ -0,0 +1,126 @@
+#include "Time.h"
+
+Time::Time(size_t hours, size_t mins, size_t seconds)
+{
+ setHours(hours);
+ setMins(mins);
+ setSeconds(seconds);
+}
+
+Time::Time() : Time(0, 0, 0)
+{}
+
+Time::Time(size_t seconds)
+{
+ hours = seconds / 3600;
+ seconds %= 3600;
+
+ mins = seconds / 60;
+ seconds %= 61;
+
+ this->seconds = seconds;
+}
+
+
+size_t Time::getSeconds() const
+{
+ return seconds;
+}
+size_t Time::getMins() const
+{
+ return mins;
+}
+size_t Time::getHours() const
+{
+ return hours;
+}
+
+void Time::setSeconds(size_t seconds)
+{
+ if (seconds > 59)
+ seconds = 59;
+ this->seconds = seconds;
+}
+void Time::setMins(size_t mins)
+{
+ if (mins > 59)
+ mins = 59;
+ this->mins = mins;
+}
+void Time::setHours(size_t hours)
+{
+ if (hours > 23)
+ hours = 23;
+ this->hours = hours;
+}
+
+void Time::tick()
+{
+ size_t seconds = convertToSeconds();
+ seconds++;
+ Time newObj(seconds);
+
+ *this = newObj; //копие
+}
+
+int Time::compare(const Time& other) const
+{
+ size_t mySeconds = convertToSeconds();
+ size_t otherSeconds = other.convertToSeconds();
+
+ if (mySeconds > otherSeconds)
+ return 1;
+ else if (mySeconds < otherSeconds)
+ return -1;
+ else
+ return 0;
+}
+Time Time::getDiff(const Time& other) const
+{
+ size_t mySeconds = convertToSeconds();
+ size_t otherSeconds = other.convertToSeconds();
+
+ size_t diff;
+
+ if (mySeconds > otherSeconds)
+ diff = mySeconds - otherSeconds;
+ else
+ diff = otherSeconds - mySeconds;
+
+ return Time(diff);
+}
+
+Time Time::getToMidnight() const
+{
+ Time midnight(23, 59, 59);
+ Time diff = getDiff(midnight);
+ diff.tick();
+ return diff;
+}
+bool Time::isDinnerTime() const
+{
+ Time lowerBound(20, 30, 0);
+ Time upperBound(22, 0, 0);
+ return compare(lowerBound) >= 0 && compare(upperBound) <= 0;
+}
+bool Time::isPartyTime() const
+{
+ Time lowerBound(23, 0, 0);
+ Time upperBound(6, 0, 0);
+ return compare(lowerBound) >= 0 || compare(upperBound) <= 0;
+}
+
+void Time::print(bool is12hoursclock) const
+{
+ if (hours < 10)
+ std::cout << 0;
+ std::cout << ((is12hoursclock && hours > 12) ? hours - 12 : hours) << ":";
+ if (mins < 10)
+ std::cout << 0;
+ std::cout << mins << ":";
+ if (seconds < 10)
+ std::cout << 0;
+ std::cout << seconds;
+ if (is12hoursclock)
+ std::cout << ((hours > 12) ? "PM" : "AM");
+} \ No newline at end of file