Ai
2 Star 0 Fork 0

lemonzhang/控制器软件

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
TimerHelper.cs 3.31 KB
Copy Edit Raw Blame History
lemonzhang authored 2021-05-04 21:29 +08:00 . 控制器软件-方案一
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ControllerClient
{
class TimerHelper
{
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static long GetTimeStamp()
{
//long ts = ConvertDateTimeToInt(time);
//long timer = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long timer = Convert.ToInt64(ts.TotalMilliseconds);
return timer;
}
/// <summary>
/// 将c# DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time">时间</param>
/// <returns>long</returns>
public static long ConvertDateTimeToInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return t;
}
/// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
public static DateTime ConvertStringToDateTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
/// <summary>
/// 时间戳转为C#格式时间10位
/// </summary>
/// <param name="timeStamp">Unix时间戳格式</param>
/// <returns>C#格式时间</returns>
public static DateTime GetDateTimeFrom1970Ticks(long curSeconds)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
return dtStart.AddSeconds(curSeconds);
}
/// <summary>
/// 验证时间戳
/// </summary>
/// <param name="time"></param>
/// <param name="interval">差值(分钟)</param>
/// <returns></returns>
public static bool IsTime(long time, double interval)
{
DateTime dt = GetDateTimeFrom1970Ticks(time);
//取现在时间
DateTime dt1 = DateTime.Now.AddMinutes(interval);
DateTime dt2 = DateTime.Now.AddMinutes(interval * -1);
if (dt > dt2 && dt < dt1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 判断时间戳是否正确(验证前8位)
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static DateTime ConvertLongToDateTime(long d)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(d + "0000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime dtResult = dtStart.Add(toNow);
return dtResult;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lemonzhang4/ControllerClient.git
git@gitee.com:lemonzhang4/ControllerClient.git
lemonzhang4
ControllerClient
控制器软件
master

Search