Skip to content
Open
200 changes: 49 additions & 151 deletions extra-task-1.cpp
Original file line number Diff line number Diff line change
@@ -1,169 +1,67 @@
#include <iostream>
#include "extra-task-1.h"
// Calculates the difference in seconds between two timestamps
double seconds_difference(double time_1, double time_2)
{
// your implementation goes here...

/*
Return the number of seconds later that a time in seconds
time_2 is than a time in seconds time_1.

>>> seconds_difference(1800.0, 3600.0)
1800.0

>>> seconds_difference(3600.0, 1800.0)
-1800.0

>>> seconds_difference(1800.0, 2160.0)
360.0

>>> seconds_difference(1800.0, 1800.0)
0.0
*/
return time_2 - time_1;
}

// Calculates the time difference in hours between two timestamps
double hours_difference(double time_1, double time_2)
{
/*
Return the number of hours later that a time in seconds
time_2 is than a time in seconds time_1.

>>> hours_difference(1800.0, 3600.0)
0.5

>>> hours_difference(3600.0, 1800.0)
-0.5

>>> hours_difference(1800.0, 2160.0)
0.1

>>> hours_difference(1800.0, 1800.0)
0.0
*/
return seconds_difference(time_1, time_2) / 3600.0;
}

// Returns the total amount of time in hours
double to_float_hours(int hours, int minutes, int seconds)
{
/*
Return the total number of hours in the specified number
of hours, minutes, and seconds.

Precondition: 0 <= minutes < 60 and 0 <= seconds < 60

>>> to_float_hours(0, 15, 0)
0.25

>>> to_float_hours(2, 45, 9)
2.7525

>>> to_float_hours(1, 0, 36)
1.01
*/
assert(0 <= minutes && minutes < 60 && 0 <= seconds && seconds < 60);
return hours + (minutes / 60.0) + (seconds / 3600.0);
}

// Translates "time from midnight" to "time on the clock"
double to_24_hour_clock(double hours)
{
/*
hours is a number of hours since midnight. Return the
hour as seen on a 24-hour clock.

Precondition: hours >= 0

>>> to_24_hour_clock(24)
0

>>> to_24_hour_clock(48)
0

>>> to_24_hour_clock(25)
1

>>> to_24_hour_clock(4)
4

>>> to_24_hour_clock(28.5)
4.5

You may wish to inspect various function in <cmath> to work
with integer and fractional part of a hours separately.

*/
assert(hours >= 0);
return fmod(hours, 24);
}

/*
Implement three functions
* get_hours
* get_minutes
* get_seconds
They are used to determine the hours part, minutes part and seconds part
of a time in seconds. E.g.:

>>> get_hours(3800)
1

>>> get_minutes(3800)
3

>>> get_seconds(3800)
20

In other words, if 3800 seconds have elapsed since midnight,
it is currently 01:03:20 (hh:mm:ss).
*/

// Returns the whole number of hours
int get_hours(int seconds)
{
assert(seconds >= 0);
return seconds / 3600 % 24;
}
// Returns an integer number of minutes
int get_minutes(int seconds)
{
assert(seconds >= 0);
return seconds % 3600 / 60;
}
// Returns an integer number of seconds
int get_seconds(int seconds)
{
assert(seconds >= 0);
return seconds % 60;
}
// Convenes the time
double time_to_utc(int utc_offset, double time)
{
/*
Return time at UTC+0, where utc_offset is the number of hours away from
UTC+0.
You may be interested in:
https://en.wikipedia.org/wiki/Coordinated_Universal_Time

>>> time_to_utc(+0, 12.0)
12.0

>>> time_to_utc(+1, 12.0)
11.0

>>> time_to_utc(-1, 12.0)
13.0

>>> time_to_utc(-11, 18.0)
5.0

>>> time_to_utc(-1, 0.0)
1.0

>>> time_to_utc(-1, 23.0)
0.0
*/
double utc_time = time - utc_offset;
if (utc_time >= 24.0)
utc_time -= 24.0;
else if (utc_time < 0.0)
utc_time += 24.0;
return utc_time;
}

// �onverts time from UTC+0
double time_from_utc(int utc_offset, double time)
{
/*
Return UTC time in time zone utc_offset.

>>> time_from_utc(+0, 12.0)
12.0

>>> time_from_utc(+1, 12.0)
13.0

>>> time_from_utc(-1, 12.0)
11.0

>>> time_from_utc(+6, 6.0)
12.0

>>> time_from_utc(-7, 6.0)
23.0

>>> time_from_utc(-1, 0.0)
23.0

>>> time_from_utc(-1, 23.0)
22.0

>>> time_from_utc(+1, 23.0)
0.0
*/
double local_time = time + utc_offset;
if (local_time >= 24.0)
local_time -= 24.0;
else if (local_time < 0.0)
local_time += 24.0;
return local_time;
}
// Approximate double comparison
bool are_equal(double a, double b)
{
return fabs(a - b) < DBL_EPSILON;
}
26 changes: 26 additions & 0 deletions extra-task-1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <iostream>
#include "cassert"
#include <cmath>
using namespace std;
// A function that calculates the difference in seconds between two timestamps
double seconds_difference(double, double);
// A function that calculates the difference in hours between two time stamps
double hours_difference(double, double);
// A function that returns the total amount of time in hours
double to_float_hours(int , int, int);
// A function that converts "time from midnight" to "time on the clock"
double to_24_hour_clock(double);
// A function that receives a whole number of hours
int get_hours(int);
// A function that gets an integer number of minutes
int get_minutes(int);
// A function that gets an integer number of seconds
int get_seconds(int);
// A function that convents time
double time_to_utc(int, double);
// A function that converts time from UTC+0
double time_from_utc(int, double);
// A function approximate double comparison
bool are_equal(double, double);

49 changes: 49 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include "extra-task-1.h"
int main()
{
// Checking the seconds_difference()
assert(are_equal(seconds_difference(1800.0, 3600.0), 1800.0));
assert(are_equal(seconds_difference(3600.0, 1800.0), -1800.0));
assert(are_equal(seconds_difference(1800.0, 2160.0), 360.0));
assert(are_equal(seconds_difference(1800.0, 1800.0), 0.0));
// Checking the hours_difference()
assert(are_equal(hours_difference(1800.0, 3600.0), 0.5));
assert(are_equal(hours_difference(3600.0, 1800.0), -0.5));
assert(are_equal(hours_difference(1800.0, 2160.0), 0.1));
assert(are_equal(hours_difference(1800.0, 1800.0), 0.0));
// Checking the to_float_hours()
assert(are_equal(to_float_hours(0, 15, 0), 0.25));
assert(are_equal(to_float_hours(2, 45, 9), 2.7525));
assert(are_equal(to_float_hours(1, 0, 36), 1.01));
// Checking to_24_hour_clock()
assert(are_equal(to_24_hour_clock(24), 0));
assert(are_equal(to_24_hour_clock(48), 0));
assert(are_equal(to_24_hour_clock(25), 1));
assert(are_equal(to_24_hour_clock(4), 4));
assert(are_equal(to_24_hour_clock(28.5), 4.5));
// Checking get_hours()
assert(get_hours(3800) == 1);
// Checking get_minutes()
assert(get_minutes(3800) == 3);
// Checking get_seconds()
assert(get_seconds(3800) == 20);
// Checking time_to_utc()
assert(are_equal(time_to_utc(+0, 12.0), 12.0));
assert(are_equal(time_to_utc(+1, 12.0), 11.0));
assert(are_equal(time_to_utc(-1, 12.0), 13.0));
assert(are_equal(time_to_utc(-11, 18.0), 5.0));
assert(are_equal(time_to_utc(-1, 0.0), 1.0));
assert(are_equal(time_to_utc(-1, 23.0), 0.0));
// Checking time_from_utc()
assert(are_equal(time_from_utc(+0, 12.0), 12.0));
assert(are_equal(time_from_utc(+1, 12.0), 13.0));
assert(are_equal(time_from_utc(-1, 12.0), 11.0));
assert(are_equal(time_from_utc(+6, 6.0), 12.0));
assert(are_equal(time_from_utc(-7, 6.0), 23.0));
assert(are_equal(time_from_utc(-1, 0.0), 23.0));
assert(are_equal(time_from_utc(-1, 23.0), 22.0));
assert(are_equal(time_from_utc(+1, 23.0), 0.0));
}