public class TimeStampToDataTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);//起始年份
DateTime newDateTime = converted.AddMilliseconds(Double.Parse(value.ToString()));
return newDateTime.ToLocalTime().ToString("g");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
C#的时间戳有10位不带毫秒的Unix的时间戳,还有带毫秒的13位Unix的时间戳。但在转换为DataTime时都需要变成17位的Ticks。
public class DeviceTimeStampToDataTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);
//DateTime newDateTime = converted.AddSeconds(Double.Parse(value.ToString()));
//return newDateTime.ToLocalTime().ToString("g");
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(2001, 1, 1));
long lTime = long.Parse(long.Parse(value.ToString()) + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow).ToString("g");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}