diff --git a/extra-task-1.cpp b/extra-task-1.cpp index 6c8c1612..efe02d97 100644 --- a/extra-task-1.cpp +++ b/extra-task-1.cpp @@ -1,7 +1,8 @@ +#include "extra-task-1.h" double seconds_difference(double time_1, double time_2) { // your implementation goes here... - + return time_2 - time_1; /* Return the number of seconds later that a time in seconds time_2 is than a time in seconds time_1. @@ -22,6 +23,7 @@ double seconds_difference(double time_1, double time_2) double hours_difference(double time_1, double time_2) { + return (time_2 - time_1) / 3600; /* Return the number of hours later that a time in seconds time_2 is than a time in seconds time_1. @@ -42,6 +44,7 @@ double hours_difference(double time_1, double time_2) double to_float_hours(int hours, int minutes, int seconds) { + return hours + minutes / 60.0 + seconds / 3600.0; /* Return the total number of hours in the specified number of hours, minutes, and seconds. @@ -61,6 +64,8 @@ double to_float_hours(int hours, int minutes, int seconds) double to_24_hour_clock(double hours) { + while (hours >= 24) hours -= 24; + return hours; /* hours is a number of hours since midnight. Return the hour as seen on a 24-hour clock. @@ -108,9 +113,21 @@ double to_24_hour_clock(double hours) In other words, if 3800 seconds have elapsed since midnight, it is currently 01:03:20 (hh:mm:ss). */ - +int get_hours(int seconds) { + return seconds / 3600; + } +int get_minutes(int seconds) { + return seconds % 3600 / 60; +} +int get_seconds(int seconds) { + return seconds % 60; +} double time_to_utc(int utc_offset, double time) { + double a = time - utc_offset; + while (a >= 24) a -= 24; + while (a <= 0) a += 24; + return a; /* Return time at UTC+0, where utc_offset is the number of hours away from UTC+0. @@ -139,6 +156,11 @@ double time_to_utc(int utc_offset, double time) double time_from_utc(int utc_offset, double time) { + double a = time + utc_offset; + while (a >= 24) a -= 24; + while (a <= 0) a += 24; + return a; + /* Return UTC time in time zone utc_offset. diff --git a/extra-task-1.h b/extra-task-1.h new file mode 100644 index 00000000..b4bd51a6 --- /dev/null +++ b/extra-task-1.h @@ -0,0 +1,19 @@ +#pragma once +#include +double seconds_difference(double time_1, double time_2); + +double hours_difference(double time_1, double time_2); + +double to_float_hours(int hours, int minutes, int seconds); + +double to_24_hour_clock(double hours); + +int get_hours(int seconds); + +int get_minutes(int seconds); + +int get_seconds(int seconds); + +double time_to_utc(int utc_offset, double time); + +double time_from_utc(int utc_offset, double time); diff --git a/main.cpp b/main.cpp new file mode 100644 index 00000000..571479cf --- /dev/null +++ b/main.cpp @@ -0,0 +1,32 @@ +#include "assert.h" +#include "extra-task-1.h" +int main() { + //проверка поиска разницы секунд + assert(seconds_difference(1000.5,300.3) == -700.2); + assert(seconds_difference(300.5, 500) == 199.5); + //проверка поиска разницы часов + assert(hours_difference(1800.0, 3600.0) == 0.5); + assert(hours_difference(3600.0, 1800.0) == -0.5); + //перевод секунд, минут и часов в часы + assert(to_float_hours(0, 15, 0) == 0.25); + assert(to_float_hours(2, 45, 9) == 2.7525); + //перевод в часы в течение дня + assert(to_24_hour_clock(24) == 0); + assert(to_24_hour_clock(25) == 1); + assert(to_24_hour_clock(28.5) == 4.5); + //поиск количества часов в секундах + assert(get_hours(3800) == 1); + assert(get_hours(9000) == 2); + //поиск количества минут после вычета часов в секундах + assert(get_minutes(3800) == 3); + assert(get_minutes(9000) == 30); + //поиск количества секунд после вычета часов и минут в секундах + assert(get_seconds(3800) == 20); + assert(get_seconds(9000) == 0); + //Перевод в всемирное координированное время + assert(time_to_utc(+1, 12.3) == 11.3); + assert(time_to_utc(-11, 18.5) == 5.5); + //Перевод из всемирное координированное время + assert(time_from_utc(-7, 6.7) == 23.7); + assert(time_from_utc(-1, 23.82) == 22.82); +} \ No newline at end of file