David Ghukasyan David Ghukasyan - 1 month ago 6
C# Question

Write getter for Seconds of Fast Days

I have a function called getDayTimeParameter(), which gets the day parameter from 0 to 1. I also have two functions which are getters of hour and minute. I need to show time as hh:mm:ss. But the problem is I have no idea how to write getter for seconds. Thanks in advance!

public float getDayTimeParameter()
{
return System.DateTime.Now.Minute / 60f + System.DateTime.Now.Second / 3600f + System.DateTime.Now.Millisecond / 3600000f;
}

public int getHour()
{
int dayMinute = Mathf.RoundToInt(24 * 60 * getDayTimeParameter());
return Mathf.FloorToInt(dayMinute / 60f);
}

public int getMinute()
{
int dayMinute = Mathf.RoundToInt(24 * 60 * getDayTimeParameter());
int hour = getHour();
return Mathf.FloorToInt((dayMinute / 60f - hour) * 60f);
}

public int getSecond()
{
// What to do in here?
}

public string getFullTimeString()
{
int hour = getHour();
int minute = getMinute();
int second = getSecond();

string minutesString;

if (minute < 10)
{
minutesString = "0" + minute;
}
else {
minutesString = "" + minute;
}

string hoursString;

if (hour < 10)
{
hoursString = "0" + hour;
}
else
{
hoursString = "" + hour;
}

string secondsString;

if (second < 10)
{
secondsString = "0" + second;
}
else
{
secondsString = "" + second;
}
return hoursString + ":" + minutesString + ":" + secondsString;
}

Answer

Given that your getDayTimeParameter function returns 0-1, and a full day equals 1

then the following should give you accurate Hours, Minutes and Seconds.

    public int getHour()
    {
        return Mathf.FloorToInt(getDayTimeParameter() * 24);
    }

    public int getMinute()
    {
        var hourSubtraction = getHour() * 60;
        return Mathf.FloorToInt((getDayTimeParameter() * 1440) - hourSubtraction);
    }

    public int getSecond()
    {
        var hourSubtraction = getHour() * 3600;
        var minuteSubtraction = getMinute() * 60;
        return Mathf.FloorToInt((getDayTimeParameter() * 86400) - hourSubtraction - minuteSubtraction);
    }