Search code examples
mathtimegeometryclockangle

Converting angle of circle to time in minutes


I have been trying to develop a clock control with a help of a circle. So if the circle is at 360 degree its 3'O clock, if its 90 degree then it will be 12'o clock. I can't figure out what formula can be used to find the time in hh:mm format if I have the angle of the circle.

eg.

9'o clock is 180 degree. 3'o clock is 360 degree 12'o clock is 90 degree and so on.


Solution

  • There's probably a more concise way to do this, but here's what you need to do.

    1. Calculate the decimal value of the time, using 3 - (1/30) * (angle mod 360)
    2. If the result is less than zero, add 12.
    3. Take the integer portion to use as the hours, replacing a value of zero with 12.
    4. Take the remainder (the decimal portion of the calculation from step 1) and convert to minutes by multiplying by 60.
    5. Format your hours and minutes, padding with leading zeros if less than 10, and combine them with the colon.

    An example implementation in java:

    public static String convertAngleToTimeString(float angle) {
        String time = "";
        float decimalValue = 3.0f - (1.0f/30.0f) * (angle % 360);
        if (decimalValue < 0)
            decimalValue += 12.0f;
    
        int hours = (int)decimalValue;
        if (hours == 0)
            hours = 12;
        time += (hours < 10 ? "0" + hours: hours) + ":";
        int minutes = (int)(decimalValue * 60) % 60; 
        time += minutes < 10 ? "0" + minutes: minutes;
        return time;
    }