Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add funcs #222

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 82 additions & 22 deletions extra-task-1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
#include <iostream>
using namespace std;
#include "extra-task-1.h";





double seconds_difference(double time_1, double time_2)
{

return time_2 - time_1;

// your implementation goes here...

/*
Expand All @@ -22,10 +33,12 @@ double seconds_difference(double time_1, double time_2)

double hours_difference(double time_1, double time_2)
{
return (time_2 - time_1) / 3600.0;

/*
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

Expand All @@ -40,8 +53,12 @@ double hours_difference(double time_1, double time_2)
*/
}


double to_float_hours(int hours, int minutes, int seconds)
{
assert(minutes >= 0 && minutes < 60);
assert(seconds >= 0 && seconds < 60);
return hours + minutes / 60 + seconds / 3600;
/*
Return the total number of hours in the specified number
of hours, minutes, and seconds.
Expand All @@ -59,8 +76,13 @@ double to_float_hours(int hours, int minutes, int seconds)
*/
}



double to_24_hour_clock(double hours)
{
assert(hours >= 0);

return hours % 24;
/*
hours is a number of hours since midnight. Return the
hour as seen on a 24-hour clock.
Expand All @@ -69,31 +91,32 @@ double to_24_hour_clock(double hours)

>>> 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.

*/
}


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

>>> get_hours(3800)
Expand All @@ -105,12 +128,38 @@ double to_24_hour_clock(double hours)
>>> get_seconds(3800)
20

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

int get_hours(int n)
{
return n / 3600;
}

int get_minutes(int n)
{
return (n % 3600) / 60;
}

int get_seconds(int t)
{
return t % 60;
}


double time_to_utc(int utc_offset, double time)
{
double utc_time = time - utc_offset;

if (utc_time < 0) {
utc_time += 24;
}
else if (utc_time >= 24) {
utc_time -= 24;
}

return utc_time;
/*
Return time at UTC+0, where utc_offset is the number of hours away from
UTC+0.
Expand All @@ -119,51 +168,62 @@ double time_to_utc(int utc_offset, double 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 time_from_utc(int utc_offset, double time)
{
double local_time = time + utc_offset;

if (local_time < 0) {
local_time += 24;
}
else if (local_time >= 24) {
local_time -= 24;
}

return local_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
*/
}
}
9 changes: 9 additions & 0 deletions extra-task-1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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 n);
int get_minutes(int n);
int get_seconds(int t);
double time_to_utc(int utc_offset, double time);
double time_from_utc(int utc_offset, double time);
13 changes: 13 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
using namespace std;
#include "extra-task-1.h";

void main()
{
assert(seconds_difference(3600, 1800) == -1800);
assert(hours_difference(1800.0, 3600.0) == 0.5);
assert(to_float_hours(0, 15, 0) == 0.25);
assert(to_24_hour_clock(25) == 1);
assert(time_to_utc(+0, 12.0) == 12.0);
assert(time_from_utc(+1, 12.0) == 13.0);
}