104 lines
3.2 KiB
C#
104 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Data;
|
|
using static ICSharpCode.SharpZipLib.Zip.ZipEntryFactory;
|
|
|
|
namespace CapMachine.Wpf.Converts
|
|
{
|
|
/// <summary>
|
|
/// 秒到字符串类型的展示
|
|
/// </summary>
|
|
public class SecToStrConvert : IValueConverter
|
|
{
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value == null)
|
|
return null;
|
|
|
|
return SecToString((int)value);
|
|
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value == null)
|
|
return null;
|
|
|
|
return StringToSec((string)value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 秒时间到 00:00:00字符串
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private string SecToString(int TotalSec)
|
|
{
|
|
//int hour = TotalSec / 3600;
|
|
//int minute = (TotalSec - hour * 3600) / 60;
|
|
//int second = TotalSec % 60;
|
|
|
|
//return string.Format("{0}:{1}:{2}", hour, minute, second);
|
|
//////Console.WriteLine(result);
|
|
|
|
//TimeSpan TimeInfo = TimeSpan.FromSeconds(TotalSec);
|
|
//return TimeInfo.ToString();
|
|
|
|
return ConvertSecToTimeStr(TotalSec);
|
|
}
|
|
|
|
private string ConvertSecToTimeStr(int totalSeconds)
|
|
{
|
|
int hours = totalSeconds / 3600;
|
|
int remainingSeconds = totalSeconds % 3600;
|
|
int minutes = remainingSeconds / 60;
|
|
int seconds = remainingSeconds % 60;
|
|
|
|
return $"{hours}:{minutes:D2}:{seconds:D2}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 00:00:00到秒时间
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private int StringToSec(string timeString)
|
|
{
|
|
try
|
|
{
|
|
//TimeSpan TimeInfo = TimeSpan.Parse(timeString);
|
|
//return (int)TimeInfo.TotalSeconds;
|
|
return ConvertTimeStrToSecs(timeString);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("时间格式输入不正确");
|
|
return (int)0;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public int ConvertTimeStrToSecs(string timeString)
|
|
{
|
|
string[] parts = timeString.Split(':');
|
|
if (parts.Length != 3) return 0;
|
|
//throw new FormatException("Invalid time format. Use 'hh:mm:ss'");
|
|
|
|
if (!int.TryParse(parts[0], out int hours) || hours < 0) return 0;
|
|
//throw new ArgumentException("小時必須為非負整數");
|
|
|
|
if (!int.TryParse(parts[1], out int minutes) || minutes < 0 || minutes >= 60) return 0;
|
|
//throw new ArgumentException("分鐘必須為 0-59 的整數");
|
|
|
|
if (!int.TryParse(parts[2], out int seconds) || seconds < 0 || seconds >= 60) return 0;
|
|
//throw new ArgumentException("秒必須為 0-59 的整數");
|
|
|
|
return hours * 3600 + minutes * 60 + seconds;
|
|
}
|
|
}
|
|
}
|