C#:实现格式正确的时间字符串的最佳方法? [英] C#: Best way to achieve a nicely formatted time string?

查看:181
本文介绍了C#:实现格式正确的时间字符串的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写此问题,因为我正在寻求最佳方法.我的程序中有很多这样的程序,我想创建一个方法来将包含秒计时器的Int32转换为格式正确的字符串.

I am writing this question as I am asking for the best way to do this. I have many of these in my program and I want to make a method to convert a Int32 which contains a seconds of a timer into a nicely formatted string.

例如,如果我的计时器int是让我们说一个随机数,例如16429 会是:

So for example, if my timer int was at lets say a random number like 16429 it would be:

4 hours, 32 minutes and 9 seconds

如果是600,则为:

10 minutes

如果是60,应该是

1 minute

如果是172801,它将是

If it was 172801, it would be

2 days and 1 second

如果是32,则为

32 seconds

我希望每个单词(如分钟",第二"和天")末尾的"s"仅在不等于1的情况下放入S,因此实际上并不需要发音正确.我也只希望添加几天和几小时以及其他需要的东西,因此,如果计时器以秒为单位低于1天,那么它只会显示几小时,几分钟和几秒钟,或者需要什么.

I want the "s" 's at the end of each word like "minute", "second" and "day" to only put S if it isn't equal to 1, therefor it isn't really needed to be pronounced correctly. I also only want days and hours and other things added if they are needed, so if the timer is below 1 day in seconds, it only shows hours, minutes and seconds, or what is needed.

实现这样的目标的最佳方法是什么?我在下面有此功能,但它非常凌乱,只能持续几分钟和几秒钟,而不是几小时或几天:

What is the best way to achieve something like this? I have this function below, but it is very messy and only goes up to minutes and seconds, not hours or days:

public static string GetConvertedTime(int timer)
{
    int Minutes = timer / 60;
    int Seconds = timer - Minutes * 60;

    if (timer < 60)
    {
        string secs = (Seconds != 1) ? "s" : "";
        return "" + timer + " second" + secs;
    }

    else
    {
        if (Seconds < 1)
        {
            string mins = (Minutes != 1) ? "s" : "";
            return "" + Minutes + " minute" + mins;
        }
        else
        {
            string mins = (Minutes != 1) ? "s" : "";
            string secs = (Seconds != 1) ? "s" : "";
            return "" + Minutes + " minute" + mins + " and " + Seconds + " second" + secs;
        }
    }
}

执行此操作的最佳方法到底是什么?

What exactly is the best way to do this?

推荐答案

最佳方法"非常主观. DRY通常是最好的,考虑到本地化和非正规复数形式通常是最好的,使其简短且活泼通常是最好的:

"Best way" is awfully subjective. DRY is usually best, considering localization and non-regular pluralization forms is usually best, keeping it short and snappy is usually best:

public static string FriendlyDuration(int seconds) {
    int[] divisors = { 60 * 60 * 24, 60 * 60, 60 , 1};
    string[] language = { ", ", " and ", "days", "day", "hours", "hour", "minutes", "minute", "seconds", "second"};
    var parts = new List<string>();
    for (int part = 0; part < divisors.Length; ++part) {
        int count = seconds / divisors[part];
        if (count == 0) continue;
        string unit = language[part*2 + (count == 1 ? 3 : 2)];
        parts.Add(string.Format("{0} {1}", count, unit));
        seconds -= count * divisors[part];
    }
    var result = string.Join(language[0], parts.ToArray());
    if (parts.Count > 1) {
        var ix = result.LastIndexOf(language[0]);
        result = result.Substring(0, ix) + language[1] + result.Substring(ix + language[0].Length);
    }
    return result;
}

这篇关于C#:实现格式正确的时间字符串的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆