内容目录
ISO8601 规定了时间格式。
在 Go 语言中,Duration 就实现了这种时间格式。
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t)
}
输出:
2022-11-19 02:59:28.201938781 +0000 UTC m=+0.000051897
在 C# 中,可以通过引用 Iso8601DurationHelper
来实现 Duration。
var duration = Duration.Parse("2022-11-19 02:59:28.201938781 +0000 UTC m=+0.000051897");
为了方便将 Duration 与 TimeSpan 之间转换,加上一个扩展:
public static class DurationExtensions
{
/// <summary>
/// Converts the <see cref="Duration"/> into a <see cref="TimeSpan"/>
/// </summary>
/// <param name="duration">The <see cref="Duration"/> to convert</param>
/// <returns>The converted <see cref="TimeSpan"/></returns>
public static TimeSpan ToTimeSpan(this Duration duration)
{
int days = (int)duration.Days;
days += (int)duration.Weeks * 7;
days += (int)duration.Months * 30;
days += (int)duration.Years * 365;
return new TimeSpan(days, (int)duration.Hours, (int)duration.Minutes, (int)duration.Seconds);
}
}
文章评论