Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 31st, 2013, 04:50 AM
 HauntJemimah
Contributing User

Join Date: Aug 2013
Posts: 194
Time spent in forums: 1 Day 5 h 48 m 34 sec
Reputation Power: 1
Need help with Utc_onset function

Hey guys,

So I'm working on a function that converts any time that's relative to the UTC or Universal Time Clock that will take in two variables, aka the positive or negative hours away from UTC 0, as well as the time in that particular time zone.

Here is the code I have thus far. Notice how I'm referring to another function called to_24_hours which will take in any time that's given in hours and convert it to hours on a 24 hour clock.

It works okay until it hits 12:00 and is just wrong afterwards.

Here it is:

Code:
```def to_24_hour_clock(hours):
""" (number) -> number

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

return hours % 24```

Code:
```def time_to_utc(utc_offset, time):
""" (number, float) -> float

Return time at UTC+0, where utc_offset is the number of hours away from
UTC+0.

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

newhours = to_24_hour_clock(time)

print (newhours)

# 12 AM, 12:00, 1 hours away from utc
# (1, 12.0)
# new time should be 11:00 aka you subtracted an hour

newhours = newhours - utc_offset
print (newhours)```

#2
August 31st, 2013, 11:42 PM
Contributing User

Join Date: Jul 2010
Posts: 140
Time spent in forums: 1 Day 8 h 1 m 46 sec
Reputation Power: 4
Can you give an example of input that gives you a wrong output, and what you expect the output to be?

#3
September 1st, 2013, 06:01 PM
 HauntJemimah
Contributing User

Join Date: Aug 2013
Posts: 194
Time spent in forums: 1 Day 5 h 48 m 34 sec
Reputation Power: 1
>>> time_to_utc(-1, 12.0)
13.0
>>> time_to_utc(-11, 18.0)

#4
September 1st, 2013, 06:03 PM
 HauntJemimah
Contributing User

Join Date: Aug 2013
Posts: 194
Time spent in forums: 1 Day 5 h 48 m 34 sec
Reputation Power: 1
The real answers that are to be expected are already included in the information section within the function, so the two above have to be correct for example.

The first two involving 12 PM totally works with the code I have so far, but every other variation gives me just wrong numbers.

#5
September 1st, 2013, 10:48 PM
Contributing User

Join Date: Jul 2010
Posts: 140
Time spent in forums: 1 Day 8 h 1 m 46 sec
Reputation Power: 4
Have you noticed a mathematical relationship between the answers you're getting and the answers you should be getting?

#6
September 2nd, 2013, 04:49 PM
 HauntJemimah
Contributing User

Join Date: Aug 2013
Posts: 194
Time spent in forums: 1 Day 5 h 48 m 34 sec
Reputation Power: 1
They're all answers from 1 to 24, and are positive.

I know this is supposed to be completely simple too, that's the major issue I'm having is how am I NOT seeing this...

You think it would be just adding the UTC_offset to the time you have.

The only other consideration I had was when it was a negative distance away from UTC 0, then it would be the opposite.

#7
September 2nd, 2013, 04:51 PM
 HauntJemimah
Contributing User

Join Date: Aug 2013
Posts: 194
Time spent in forums: 1 Day 5 h 48 m 34 sec
Reputation Power: 1
I tried to account for that for maybe making the offset negative...

The only other way I could think of how to do this, would be to set up if statements, but its not legal yet in the course to do so

#8
September 2nd, 2013, 05:30 PM
 HauntJemimah
Contributing User

Join Date: Aug 2013
Posts: 194
Time spent in forums: 1 Day 5 h 48 m 34 sec
Reputation Power: 1
Got it, you have to add mod % 24 at the end of the statement to make it correct

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Need help with Utc_onset function