Files
OSS.Common/OS.Common/Extention/DateTimeExtention.cs
T
2017-01-12 17:09:00 +08:00

87 lines
2.6 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#region Copyright (C) 2014 OS系列开源项目
/*
   文件功能描述:验证属性attribute
   创建人:王超
创建人Email:1985088337@qq.com
创建日期:2014.08.25
   修改描述:
*/
#endregion
using System;
namespace OS.Common.Extention
{
/// <summary>
/// 时间秒数转化
/// </summary>
public static class DateTimeExtention
{
/// <summary>
/// 获取距离 1970-01-01(格林威治时间)的秒数
/// </summary>
/// <param name="localTime"></param>
/// <returns></returns>
public static long ToUtcSeconds(this DateTime localTime)
{
return (long)(localTime.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
}
/// <summary>
/// 距离 1970-01-01(格林威治时间)的秒数转换为当前时间
/// </summary>
/// <param name="seconds"></param>
/// <returns></returns>
public static DateTime FromUtcSeconds(this long seconds)
{
return new DateTime(1970, 1, 1).AddSeconds(seconds).ToLocalTime();
}
/// <summary>
/// 获取距离 1970-01-01(格林威治时间)的秒数
/// </summary>
/// <param name="localTime"></param>
/// <returns></returns>
public static long ToUtcMilliSeconds(this DateTime localTime)
{
return (long)(localTime.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds;
}
/// <summary>
/// 距离 1970-01-01(格林威治时间)的秒数转换为当前时间
/// </summary>
/// <param name="seconds"></param>
/// <returns></returns>
public static DateTime FromUtcMilliSeconds(this long seconds)
{
return new DateTime(1970, 1, 1).AddMilliseconds(seconds).ToLocalTime();
}
/// <summary>
/// 获取距离 1970-01-01(本地/北京时间)的秒数
/// </summary>
/// <param name="localTime"></param>
/// <returns></returns>
public static long ToLocalSeconds(this DateTime localTime)
{
return (long)(localTime - new DateTime(1970, 1, 1)).TotalSeconds;
}
/// <summary>
/// 距离 1970-01-01(本地/北京时间)的秒数转换为当前时间
/// </summary>
/// <param name="seconds"></param>
/// <returns></returns>
public static DateTime FromLocalSeconds(this long seconds)
{
return new DateTime(1970, 1, 1).AddSeconds(seconds);
}
}
}