diff options
author | KT <tran0563@umn.edu> | 2021-10-04 21:25:55 -0500 |
---|---|---|
committer | KT <tran0563@umn.edu> | 2021-10-04 21:25:55 -0500 |
commit | 427e0e31a7ba3d947aac97b30d9397dad329078d (patch) | |
tree | 534bb19ea21c593d314ce32ae875012df0338ed1 /dev/a3-earthquake/date.cc | |
parent | publish a2 (diff) | |
download | csci4611-427e0e31a7ba3d947aac97b30d9397dad329078d.tar csci4611-427e0e31a7ba3d947aac97b30d9397dad329078d.tar.gz csci4611-427e0e31a7ba3d947aac97b30d9397dad329078d.tar.bz2 csci4611-427e0e31a7ba3d947aac97b30d9397dad329078d.tar.lz csci4611-427e0e31a7ba3d947aac97b30d9397dad329078d.tar.xz csci4611-427e0e31a7ba3d947aac97b30d9397dad329078d.tar.zst csci4611-427e0e31a7ba3d947aac97b30d9397dad329078d.zip |
Publish a3
Diffstat (limited to 'dev/a3-earthquake/date.cc')
-rw-r--r-- | dev/a3-earthquake/date.cc | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/dev/a3-earthquake/date.cc b/dev/a3-earthquake/date.cc new file mode 100644 index 0000000..620cc74 --- /dev/null +++ b/dev/a3-earthquake/date.cc @@ -0,0 +1,127 @@ +/** CSci-4611 Assignment 3: Earthquake + */ + +#include <ctime> +#include "date.h" + +using namespace std; + +const int SECONDS_PER_4_YEARS=126230400; + +Date::Date() { + year_ = 0; + month_ = 0; + day_ = 0; + hour_ = 0; + minute_ = 0; + second_ = 0; + ctime_ = 0; + fouryears_ = 0; +} + +// for visual studio, disable warning about localtime being unsafe +#pragma warning(push) +#pragma warning(disable: 4996) + +Date::Date(double seconds) { + double tweakedSeconds = seconds; + fouryears_ = 0; + while (tweakedSeconds < 0) { + tweakedSeconds += SECONDS_PER_4_YEARS; + fouryears_++; + } + ctime_ = int(tweakedSeconds); + tm *t; + t = localtime(&ctime_); + year_ = t->tm_year + 1900 - 4 * fouryears_; + month_ = t->tm_mon + 1; + day_ = t->tm_mday; + hour_ = t->tm_hour; + minute_ = t->tm_min; + second_ = t->tm_sec + (tweakedSeconds - ctime_); +} + +#pragma warning(pop) + +Date::Date(int m, int d, int y) { + month_ = m; + day_ = d; + year_ = y; + hour_ = 0; + minute_ = 0; + second_ = 0; + fouryears_ = 0; + tm t; + while (year_ + fouryears_*4 < 1970) { + fouryears_++; + } + t.tm_year = year_ - 1900 + fouryears_ * 4; + t.tm_mon = month_ - 1; + t.tm_mday = day_; + t.tm_hour = 0; + t.tm_min = 0; + t.tm_sec = 0; + ctime_ = mktime(&t); +} + +Date::Date(int m, int d, int y, int hr, int min, double sec) { + month_ = m; + day_ = d; + year_ = y; + hour_ = hr; + minute_ = min; + second_ = sec; + fouryears_ = 0; + tm t; + while (year_ + fouryears_*4 < 1970) { + fouryears_++; + } + t.tm_year = year_ - 1900 + fouryears_ * 4; + t.tm_mon = month_ - 1; + t.tm_mday = day_; + t.tm_hour = hour_; + t.tm_min = minute_; + t.tm_sec = int(second_); + ctime_ = mktime(&t); +} + +bool Date::operator<(const Date& other) { + return ToSeconds() < other.ToSeconds(); +} + +bool Date::operator>(const Date& other) { + return ToSeconds() > other.ToSeconds(); +} + +double Date::ToSeconds() const { + return ctime_ + second_ - int(second_) - double(fouryears_) * SECONDS_PER_4_YEARS; +} + +double Date::SecondsUntil(const Date& other) const { + return ToSeconds() - other.ToSeconds(); +} + + +int Date::year() { + return year_; +} + +int Date::month() { + return month_; +} + +int Date::day() { + return day_; +} + +int Date::hour() { + return hour_; +} + +int Date::minute() { + return minute_; +} + +double Date::second() { + return second_; +} |