我的应用程序是在C#中,我试图创建一个方法,将传递的值转换为秒(时间),将来的格式不固定,由用户提供,因此它将是动态的。它可以是 hh:mm或mm:ss或hh:mm:ss ,它可以是任何有效的时间格式。我将从某个文件中获取的值始终是时间部分。格式将由用户根据条件/要求传递。它可以是任何有效的时间格式。
我试过
我根据格式将转换逻辑添加到秒的其他选项,我也不想为每种格式添加条件,然后使用parse方法。有没有其他通用方法来实现相同的目标?这样我可以传递值和格式,它应该转换为秒。我尝试过的其他事情远非通用,而是专注于任何一种格式,如下面链接中的那种格式
答案 0 :(得分:0)
好吧,歧义使问题无法在一般情况中实现,例如看看
10:20
是 10小时20分,所以答案是10 * 3600 + 20 * 60 == 37200
或 10分20秒等于10 * 60 + 20 = 620
秒。但是,如果您知道如何处理这种歧义,可以使用TryParseExact
:
private static double InSeconds(string value, params string[] formats) {
// if no formats provided, use default ones:
// try these formats in this particular order:
if ((null == formats) || (formats.Length <= 0))
formats = new string[] {
@"h\:m\:s",
@"h\:m",
@"m\:s" }
else
// In case of unescaped formats like "h:m" (see comments below)
// we automatically escape them
for (int i = 0; i < formats.Length; ++i)
if (formats[i] != null)
formats[i] = Regex.Replace(formats[i],
@"(?<!\\)[^A-Za-z\\]",
match => @"\" + match.Value);
TimeSpan result;
if (TimeSpan.TryParseExact(value, formats, CultureInfo.InvariantCulture, out result))
return result.TotalSeconds;
else
return double.NaN;
}
测试
// 23 hours 53 minutes == 85980 seconds
// Please, notice ambiguity: 23 minutes 53 seconds is also possible
// however the routine states that h:m should be tried first
Console.Write(InSeconds("23:53"));
// 47 minutes 53 seconds == 2873 seconds:
// hour:minutes tried first, fails and minutes:seconds succeeds
Console.Write(InSeconds("47:53"));
// 99:99 == NaN
// All three expected formats fail on such an input
Console.Write(InSeconds("99:99"));
// Custom format - 45240
Console.Write(InSeconds("12:34", @"hh\:mm"));
// Custom format - NAN: hh requires leading zeros (i.e. 01:34)
Console.Write(InSeconds("1:34", @"hh\:mm"));
// Custom formats: 1 day 12 hours 34 minutes - 131640
Console.Write(InSeconds("1.12:34", @"hh\:mm", @"d\.hh\:mm"));
// Custom format (unescaped, notice abscence of \) - 45240
Console.Write(InSeconds("12:34", @"hh:mm"));
答案 1 :(得分:0)
试试这个:
class TimeParser
{
Regex tripleReg = new Regex(@"([\d]{1,2}):([\d]{1,2}):([\d]{1,2})");
Regex doubleReg = new Regex(@"([\d]{1,2}):([\d]{1,2})");
public enum Format
{
HoursMinutesSeconds,
HoursMinutes,
MinutesSeconds
}
public TimeSpan Parse(string data)
{
if (tripleReg.IsMatch(data))
return Parse(data, Format.HoursMinutesSeconds);
else
{
var match = doubleReg.Match(data);
var segment = match.Groups[1];
var value = int.Parse(segment.Value);
if (value < 60)
return Parse(data, Format.MinutesSeconds);
else
return Parse(data, Format.HoursMinutes);
}
}
public TimeSpan Parse(string data, Format format)
{
TimeSpan result;
Match match;
var hours = 0;
var minutes = 0;
var seconds = 0;
switch (format)
{
case Format.HoursMinutesSeconds:
match = tripleReg.Match(data);
if (match.Success)
{
int.TryParse(match.Groups[1].Value, out hours);
int.TryParse(match.Groups[2].Value, out minutes);
int.TryParse(match.Groups[3].Value, out seconds);
}
break;
case Format.HoursMinutes:
match = doubleReg.Match(data);
if (match.Success)
{
int.TryParse(match.Groups[1].Value, out hours);
int.TryParse(match.Groups[2].Value, out minutes);
}
break;
case Format.MinutesSeconds:
match = doubleReg.Match(data);
if (match.Success)
{
int.TryParse(match.Groups[1].Value, out minutes);
int.TryParse(match.Groups[2].Value, out seconds);
}
break;
default:
break;
}
result = new TimeSpan(hours, minutes, seconds);
return result;
}
}
测试:
static void Main(string[] args)
{
var parser = new TimeParser();
Console.WriteLine(parser.Parse("60:34").TotalSeconds); //218040 seconds, hh:mm
Console.WriteLine(parser.Parse("55:10").TotalSeconds); //3310 seconds, mm:ss
Console.WriteLine(parser.Parse("3:33:34").TotalSeconds); //12814 seconds, hh:mm:ss
Console.WriteLine(parser.Parse("12:34", TimeParser.Format.HoursMinutes).TotalSeconds); //45240 seconds
Console.WriteLine(parser.Parse("55:10", TimeParser.Format.MinutesSeconds).TotalSeconds); //3310 seconds
Console.WriteLine(parser.Parse("3:33:34", TimeParser.Format.HoursMinutesSeconds).TotalSeconds); //12814 seconds
Console.ReadKey();
}
答案 2 :(得分:0)
我不知道这是否是最佳解决方案,或者在发布问题时是否使其更复杂。以下是我实施的内容及其满足我的要求
以这样的双重格式获取值&#34; 01:30&#34;我将其转换为Datetime对象
var DateObj= Convert.ToDateTime("MyValue");
DateObj.ToString("MyFormat");
var TempVariable=DateObj.TimeOfDay.TotalSeconds;
如果此解决方案有任何缺点,请评论或改进我的回答