英文:
Golang time.duration format for C# TimeSpan
问题
最近我学了一点 Golang,非常喜欢 time.duration 的格式。例如,"1d2h3s"。但是我找不到在 C# 的 TimeSpan 类中使用这种格式的方法。有什么想法吗?
英文:
I recently did a bit of Golang and really enjoyed the format for time.duration. For example, "1d2h3s". I could not find a way to use this format with the C# TimeSpan class. Any ideas?
答案1
得分: 0
在Golang中,似乎没有像你在代码中找到的那样的格式化实现方式,但是如果你喜欢那种方式,我写了一个自定义方法来实现:
public static class TimeSpanHelper
{
private static List<(string Abbreviation, TimeSpan InitialTimeSpan)> timeSpanInfos = new List<(string Abbreviation, TimeSpan InitialTimeSpan)>
{
("y", TimeSpan.FromDays(365)), // 年
("M", TimeSpan.FromDays(30)), // 月
("w", TimeSpan.FromDays(7)), // 周
("d", TimeSpan.FromDays(1)), // 天
("h", TimeSpan.FromHours(1)), // 小时
("m", TimeSpan.FromMinutes(1)), // 分钟
("s", TimeSpan.FromSeconds(1)), // 秒
("t", TimeSpan.FromTicks(1)) // 刻度
};
public static TimeSpan ParseDuration(string format)
{
var result = timeSpanInfos
.Where(timeSpanInfo => format.Contains(timeSpanInfo.Abbreviation))
.Select(timeSpanInfo => timeSpanInfo.InitialTimeSpan * int.Parse(new Regex(@$"(\d+){timeSpanInfo.Abbreviation}").Match(format).Groups[1].Value))
.Aggregate((accumulator, timeSpan) => accumulator + timeSpan);
return result;
}
}
你可以按照以下方式使用它:
var total = TimeSpanHelper.ParseDuration("1d2h3s");
请注意,还有内置的实现方式可以实现相同的结果:
TimeSpan
结构体有以下构造函数:
public TimeSpan(long ticks)
public TimeSpan(int hours, int minutes, int seconds)
public TimeSpan(int days, int hours, int minutes, int seconds)
public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
因此,我们可以按照以下方式将你的示例转换为 C# 代码:
var total = new TimeSpan(1, 2, 0, 3);
或者,我们可以使用 TimeSpan
结构体上的一些静态方法来为特定的时间段设置值:
public static TimeSpan FromDays(double value)
public static TimeSpan FromHours(double value)
public static TimeSpan FromMinutes(double value)
public static TimeSpan FromSeconds(double value)
public static TimeSpan FromMilliseconds(double value)
public static TimeSpan FromTicks(long value)
因此,我们也可以这样做:
var total = TimeSpan.FromDays(1) + TimeSpan.FromHours(2) + TimeSpan.FromSeconds(3);
英文:
There doesn't seem to exist a formatting implementation the way you found it on Golang but in case you prefer it that way I wrote a custom method to allow you that:
public static class TimeSpanHelper
{
private static List<(string Abbreviation, TimeSpan InitialTimeSpan)> timeSpanInfos = new List<(string Abbreviation, TimeSpan InitialTimeSpan)>
{
("y", TimeSpan.FromDays(365)), // Year
("M", TimeSpan.FromDays(30)), // Month
("w", TimeSpan.FromDays(7)), // Week
("d", TimeSpan.FromDays(1)), // Day
("h", TimeSpan.FromHours(1)), // Hour
("m", TimeSpan.FromMinutes(1)), // Minute
("s", TimeSpan.FromSeconds(1)), // Second
("t", TimeSpan.FromTicks(1)) // Tick
};
public static TimeSpan ParseDuration(string format)
{
var result = timeSpanInfos
.Where(timeSpanInfo => format.Contains(timeSpanInfo.Abbreviation))
.Select(timeSpanInfo => timeSpanInfo.InitialTimeSpan * int.Parse(new Regex(@$"(\d+){timeSpanInfo.Abbreviation}").Match(format).Groups[1].Value))
.Aggregate((accumulator, timeSpan) => accumulator + timeSpan);
return result;
}
}
You can use it in the following way:
var total = TimeSpanHelper.ParseDuration("1d2h3s");
Note that there are built in implementations that allow you to achieve the same result:
The TimeSpan
struct has following constructor headers:
public TimeSpan(long ticks)
public TimeSpan(int hours, int minutes, int seconds)
public TimeSpan(int days, int hours, int minutes, int seconds)
public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
Therefore we can C#ify your example in the following way:
var total = new TimeSpan(1, 2, 0, 3);
Alternatively we can use some static methods on the TimeSpan
struct that allow us to put in a value for a specific time period:
public static TimeSpan FromDays(double value)
public static TimeSpan FromHours(double value)
public static TimeSpan FromMinutes(double value)
public static TimeSpan FromSeconds(double value)
public static TimeSpan FromMilliseconds(double value)
public static TimeSpan FromTicks(long value)
So we could also do:
var total = TimeSpan.FromDays(1) + TimeSpan.FromHours(2) + TimeSpan.FromSeconds(3);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论