在达到0,xx之前达到最高可能时间值的毫秒数

时间:2017-03-08 18:16:11

标签: c#

我想在达到0,x值之前将转换器从毫秒写入最高可能的时间值。

让我用例子说明这一点。

我们假设您directDebit 1500ms会产生1,5secs,因为其最高可能的数字值而不是会产生0,x

所以不同的例子是

10ms = 10,0ms
100ms = 100,0ms
1000ms = 1,0sec
10000ms = 10,0sec
100000ms = 1,6min
1000000ms = 16,0min
10000000ms = 2,7hours

(该方法应该或多或少是无穷无尽的,所以从数小时到数天,到数周,到数月,到数年,到数十年等等......)

这是否有.net方法?

4 个答案:

答案 0 :(得分:1)

虽然不是最终解决方案,但也许TimeSpan可以帮助您实现所需的目标。 但需要注意的是,TimeSpan最多只支持TotalDays

var timespan = TimeSpan.FromMilliseconds(1500);
var seconds = timespan.TotalSeconds; // equals: 1.5

答案 1 :(得分:1)

如下所示

public static string ConversionMethod(UInt64 ms)
{
    // change output format as needed
    string format = "######.###";

    var cutoffs = new List<UInt64>() {
        1000, // second
        60000, // minute
        3600000, // hour
        86400000, // day
        604800000, // week = day * 7
        2592000000, // month = day * 30
        31536000000, // year = day * 365
        315360000000, // decade = year * 10
        3153600000000, // century = decade * 10 (100 years)
        31536000000000, // millenia = century * 10 (1000 years)
        31536000000000000 // megayear = year * 100000
        // 18446744073709551615 // UInt64 MaxValue
        // 31536000000000000000 // gigayear = year * 100000000
    };

    var postfix = new List<String>() {
        "second",
        "minute",
        "hour",
        "day",
        "week",
        "month",
        "year",
        "decade",
        "century",
        "millenia",
        "megayear"
    };

    // The above are listed from smallest to largest for easy reading,
    // but the comparisons need to be made from largest to 
    // smallest (in the loop below)
    cutoffs.Reverse();
    postfix.Reverse();

    int count = 0;

    foreach (var cutoff in cutoffs)
    {
        if (ms > cutoff)
        {
            return ((decimal)((decimal)ms / (decimal)cutoff)).ToString(format) + " " + postfix[count];
        }

        count++;
    }

    return ms + " ms";
}

分数的转换有点脏,可能想要清理它。此外,您还必须决定如何处理闰年(和闰秒)等。

答案 2 :(得分:0)

似乎TimeSpan课程是最接近你需要的课程,但显然它并不完全是你想要的。我对它的看法是这样的:

public static void main (String[] args)

答案 3 :(得分:-1)

以下是使用DateTime和Gregorian日历(即闰年,日历月)的数年,数月的解决方案。然后它使用已提交的TimeSpan解决方案。

static string ToMostNonZeroTime(long ms) {
    const int hundretsNanosecondsInMillisecond = 10000;
    long ticks = (long)ms * hundretsNanosecondsInMillisecond;
    var dt = new DateTime(ticks); 

    if((dt.Year - 1) > 0) { // starts with 1
        double daysToYear = (dt.DayOfYear - 1) * 1.0 / (DateTime.IsLeapYear(dt.Year) ? 366 : 365);
        daysToYear +=  dt.Year - 1;
        return $"{daysToYear:0.0} years";
    }
    if((dt.Month - 1) > 0) {
        double daysToMonth = (dt.Day - 1) * 1.0 / DateTime.DaysInMonth(dt.Year, dt.Month);
        daysToMonth +=  dt.Day - 1;
        return $"{daysToMonth:0.0} months";
    }
    // can use TimeSpan then:
    var ts = TimeSpan.FromMilliseconds(ms);
    if(ts.TotalDays >= 1)
        return $"{ts.TotalDays:0.0} days";
    if(ts.TotalHours >= 1)
        return $"{ts.TotalHours:0.0} hours";
    if(ts.TotalMinutes >= 1)
        return $"{ts.TotalMinutes:0.0} minutes";
    if(ts.TotalSeconds >= 1)
        return $"{ts.TotalSeconds:0.0} seconds";
    return $"{ms} milliseconds";
}

打印

100ms: 100 milliseconds
1000ms: 1.0 seconds
10000ms: 10.0 seconds
100000ms: 1.7 minutes
1000000ms: 16.7 minutes
10000000ms: 2.8 hours
100000000ms: 1.2 days
1000000000ms: 11.6 days
20000000000ms: 19.6 months
200000000000ms: 6.3 years

查看https://ideone.com/QZHOM4