解决人性化时间方法的问题?

时间:2018-08-18 17:40:42

标签: c# .net

由于已经没有任何东西了,除了大型Nuget软件包中只有0.5%的功能被使用的那些,或者不是100%人性化的格式之外,我决定编写自己的。

一切正常,直到timeSpan的总秒数达到3720(我认为)

我注意到应该说“ 1小时2分钟”时说“ 1小时1分60秒”。我知道这段代码变得有些复杂,而且我不确定为什么会出错。

有人可以帮我解决这个问题吗?

inline

我知道有些人可能会问SQUARE("Hello");是什么,所以我也包括在内。

public static string GetHumanizedTime(DateTime dateTime)
{
    var timeSpan = DateTime.Now - dateTime;

    var hours = timeSpan.TotalHours.RoundDown();
    var minutes = timeSpan.TotalMinutes.RoundDown();
    var seconds = timeSpan.TotalSeconds.RoundDown();

    var secondsAfterMinutes = seconds - minutes * 60;
    var secondsAfterHours = seconds - hours * 3600;

    var minutesAfterSeconds = minutes - hours * 60;
    var minutesAfterHours = minutes - hours * 60;

    if (seconds < 60)
    {
        return $"{seconds} second{(seconds != 1 ? "s" : "")}";
    }

    if (seconds >= 60 && minutes < 60)
    {
        if (secondsAfterMinutes < 1)
        {
            return $"{minutes} minute{(minutes != 1 ? "s" : "")}";
        }

        return $"{minutes} minute{(minutes != 1 ? "s" : "")} and {secondsAfterMinutes} second{(secondsAfterMinutes != 1 ? "s" : "")}";
    }

    if (minutes >= 60 && hours < 24)
    {
        if (minutesAfterHours > 0)
        {
            if (secondsAfterHours > 0)
            {
                return $"{hours} hour{(hours != 1 ? "s" : "")}, {minutesAfterSeconds} minute{(minutesAfterSeconds != 1 ? "s" : "")} and {secondsAfterHours} second{(secondsAfterHours != 1 ? "s" : "")}";
            }
            else
            {
                return $"{hours} hour{(hours != 1 ? "s" : "")}, and {minutesAfterSeconds} minute{(minutesAfterSeconds != 1 ? "s" : "")}";
            }
        }

        if (secondsAfterHours > 0)
        {
            return $"{hours} hour{(hours != 1 ? "s" : "")}, and {secondsAfterHours} second{(secondsAfterHours != 1 ? "s" : "")}";
        }

        return $"{hours} hour{(hours != 1 ? "s" : "")}";
    }

    if (hours >= 24)
    {
        return "TODO days";
    }

    return "TODO";
}

2 个答案:

答案 0 :(得分:0)

我建议您使用现有的库来解决此问题,例如@jeroenh建议。

但这是您的代码存在的问题:您的secondsAfterHours变量仅记录一个小时后经过了多少秒。因此上升到60,然后继续增加。这是在此行上打印的内容:

return $"{hours} hour{(hours != 1 ? "s" : "")}, {minutesAfterSeconds} minute{(minutesAfterSeconds != 1 ? "s" : "")} and {secondsAfterHours} second{(secondsAfterHours != 1 ? "s" : "")}";

因此,您只需将行更改为

即可解决问题
return $"{hours} hour{(hours != 1 ? "s" : "")}, {minutesAfterSeconds} minute{(minutesAfterSeconds != 1 ? "s" : "")} and {secondsAfterMinutes} second{(secondsAfterMinutes != 1 ? "s" : "")}";

您的固定代码是

public static string GetHumanizedTime(DateTime dateTime)
{
    var timeSpan = DateTime.Now - dateTime;

    var hours = timeSpan.TotalHours.RoundDown();
    var minutes = timeSpan.TotalMinutes.RoundDown();
    var seconds = timeSpan.TotalSeconds.RoundDown();

    var secondsAfterMinutes = seconds - minutes * 60;
    var secondsAfterHours = seconds - hours * 3600;

    var minutesAfterSeconds = minutes - hours * 60;
    var minutesAfterHours = minutes - hours * 60;

    if (seconds < 60)
    {
        return $"{seconds} second{(seconds != 1 ? "s" : "")}";
    }

    if (seconds >= 60 && minutes < 60)
    {
        if (secondsAfterMinutes < 1)
        {
            return $"{minutes} minute{(minutes != 1 ? "s" : "")}";
        }

        return $"{minutes} minute{(minutes != 1 ? "s" : "")} and {secondsAfterMinutes} second{(secondsAfterMinutes != 1 ? "s" : "")}";
    }

    if (minutes >= 60 && hours < 24)
    {
        if (minutesAfterHours > 0)
        {
            if (secondsAfterMinutes > 0)
            {
                return $"{hours} hour{(hours != 1 ? "s" : "")}, {minutesAfterSeconds} minute{(minutesAfterSeconds != 1 ? "s" : "")} and {secondsAfterMinutes} second{(secondsAfterMinutes != 1 ? "s" : "")}";
            }
            else
            {
                return $"{hours} hour{(hours != 1 ? "s" : "")}, and {minutesAfterSeconds} minute{(minutesAfterSeconds != 1 ? "s" : "")}";
            }
        }

        if (secondsAfterHours > 0)
        {
            return $"{hours} hour{(hours != 1 ? "s" : "")}, and {secondsAfterHours} second{(secondsAfterHours != 1 ? "s" : "")}";
        }

        return $"{hours} hour{(hours != 1 ? "s" : "")}";
    }

    if (hours >= 24)
    {
        return "TODO days";
    }

    return "TODO";
}

此代码可正确打印:

1 hour, and 59 seconds
1 hour, and 1 minute
1 hour, 1 minute and 1 second

答案 1 :(得分:0)

我不认为@Hele的答案是正确的,因为它仅使用数分钟内的秒数。我决定发布修订程序,因为我注意到secondsAfterHours变量没有考虑分钟数。

更改此行

var secondsAfterHours = seconds - hours * 3600;

此行可解决此问题。

var secondsAfterHours = timeInSeconds - minutesAfterHours * 60 - timeInHours * 3600;