#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep 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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Can you give an example of input that gives you a wrong output, and what you expect the output to be?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    >>> time_to_utc(-1, 12.0)
    13.0
    >>> time_to_utc(-11, 18.0)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep 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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Have you noticed a mathematical relationship between the answers you're getting and the answers you should be getting?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep 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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep 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
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Got it, you have to add mod % 24 at the end of the statement to make it correct

IMN logo majestic logo threadwatch logo seochat tools logo