Reputation: 95
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.
Upvotes: 3
Views: 10923
Reputation: 1
hrs=date +%H
#H >>
min=date +%M
#min >>
dim=echo "scale=2; $hrs * 60 + $min" | bc
#day in minutes >>
dis=echo "scale=2; $dim * 60 + $sec" | bc
#day in seconds >>
did=echo "scale=2; $dis / 240" | bc
#day in degree >>
The Planet revolution is One degree per 240 seconds.
I work to make a sumerian-time-sys . One sumerian hour is equivant with two current HRS or 30 revolution-degree. search for sexagesimal systems. Have fun!
Upvotes: 0
Reputation: 6522
There's probably a more concise way to do this, but here's what you need to do.
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;
}
Upvotes: 3
Reputation: 55619
First assume 12 o'clock is at 0 degrees. This makes sense because the degrees just increases when going around the clock once, and so does the hours (there's no wrap-around), so maybe we can just multiply the hours by a constant.
To have it reach 360 again at 12 o'clock, we need to multiply the hours by 360/12 = 30.
So now 3 o'clock is at 90 degrees and 12 o'clock is at 0.
Subtract 90 and we have 3 o'clock at 0 and 12 o'clock at -90.
Then make it negative and we have 3 o'clock at 0 and 12 o'clock at 90, as required.
There we go, now you just need to put it all together.
Upvotes: 1
Reputation: 401
You could check out these formula's on Wikipedia, they might help: http://en.wikipedia.org/wiki/Clock_angle_problem
But other than that, you could do something simple like, degrees/360 * 12. + offset.
For example, if we set that 360 degrees is 12 o'clock, and you had 90 degrees. You would get. 90/360 * 12 = 3. So that would be 3'o clock. Then you could add your offset, which in your case is 3 so it would be 6 o'clock.
Upvotes: 2