Taking digital clock time and converting to a value between 0 and 1?

Hello,

I have a mechanic in my project where the player can set a digital alarm clock to a time, go to sleep, and wake up at that time, allowing time to pass and for stamina to restore and such. A day/night cycle occurs between a value of 0 and 1, where 0 is 12AM, 0.5 is 12PM, 1 is a new day at 12AM, and everything in between.

Right now as day and night cycles occur, the digital clock takes that value and converts that into an AM/PM time. I’m trying to figure out how to make it go the opposite way, to take a user-inputed time and make that a value between 0 and 1 so that I can set the current time to that.

This is the code for converting from the value to the time, where controller.currentTime is the value between 0 and 1.

public class ClockDigital : MonoBehaviour
{

    public Text clockText;
    public Text ampmText;
    public double minute, hour;
    DayAndNightControl controller;

    private void Start()
    {
        controller = GameObject.Find("Day and Night Controller").GetComponent<DayAndNightControl>();
    }

    void Update()
    {
        CalculateTime();
    }

    void CalculateTime()
    {
        float currHour = 24 * controller.currentTime;
        float currMin = 60 * (currHour - Mathf.Floor(currHour));

        minute = Mathf.Floor(Mathf.Repeat(currMin, 60));
        hour = Mathf.Repeat(Mathf.Floor(currHour), 12);

Then I use a couple if statements to create 0’s for minutes and hours less than nine for the clock text, which has a generic form of clockText.text = hour + “:” + minute. But I figure I’m looking at these last 4 lines to reverse the equation…

TL;DR : Your needed formula is : time = ( hour x 3600 + minutes x 60 + seconds ) / 86400.

Glad that you found a workaround.

To go a bit further, here is what you would do to calculate your time value from hour/minute/second.

Since your time value goes from 0 to 1 from midnight to the next day’s midnight, you can deduce that it translates to:

  • 0 to 24 when counting hours
  • 0 to 1440 when counting minutes (24x60)
  • 0 to 86400 when counting seconds (24x60x60)

Now all you have to do is to convert your time information in the correct unit, and divide by the maximum mentioned earlier:

  • 13:37:42 is a total of 13x60x60 + 37x60 + 42 = 49062 seconds. Divide by 86400 and it corresponds to 0.567847222 in your time cycle
  • 09:56 is a total of 9x60 + 56 = 596 minutes. Divide by 1440 and it corresponds to 0.4138889 in your cycle.
  • To avoid managing multiple units, simply convert everytime to the same one. 09:56 = 09:56:00, to calculate in seconds. You can event go further in precision if you need to.

I found a workaround, and though it’s not ideal, it is functional.

I made a slider that has a min value of 0 and max value of 1 and, like the digital clock, wired it up to preview a user inputed alarm time to move with that slider. Then it was as simple as cleaning up with this code:

   public void MoveTime()
    {
        if (timeSlider.value < controller.currentTime)
        {
            controller.currentDay++;
            timeSlider.value = controller.currentTime;
        }
        else
        {
            controller.currentTime = timeSlider.value;
        }
    }