using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.IO;
|
using System.Reflection;
|
using System.Runtime.InteropServices;
|
|
namespace BatchService.Framework.Utility
|
{
|
public static class DateTimeUtil
|
{
|
[StructLayout(LayoutKind.Sequential)]
|
public struct SYSTEMTIME
|
{
|
public ushort wYear;
|
public ushort wMonth;
|
public ushort wDayOfWeek;
|
public ushort wDay;
|
public ushort wHour;
|
public ushort wMinute;
|
public ushort wSecond;
|
public ushort wMilliseconds;
|
|
public void FromDateTime(DateTime dateTime)
|
{
|
wYear = (ushort)dateTime.Year;
|
wMonth = (ushort)dateTime.Month;
|
wDayOfWeek = (ushort)dateTime.DayOfWeek;
|
wDay = (ushort)dateTime.Day;
|
wHour = (ushort)dateTime.Hour;
|
wMinute = (ushort)dateTime.Minute;
|
wSecond = (ushort)dateTime.Second;
|
wMilliseconds = (ushort)dateTime.Millisecond;
|
}
|
|
public DateTime ToDateTime()
|
{
|
return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond);
|
}
|
}
|
//设定,获取系统时间,SetSystemTime()默认设置的为UTC时间,比北京时间少了8个小时。
|
[DllImport("Kernel32.dll")]
|
public static extern bool SetSystemTime(ref SYSTEMTIME time);
|
[DllImport("Kernel32.dll")]
|
public static extern bool SetLocalTime(ref SYSTEMTIME time);
|
[DllImport("Kernel32.dll")]
|
public static extern void GetSystemTime(ref SYSTEMTIME time);
|
[DllImport("Kernel32.dll")]
|
public static extern void GetLocalTime(ref SYSTEMTIME time);
|
/// <summary>
|
/// 微信的CreateTime是当前与1970-01-01 00:00:00之间的秒数 /// </summary>
|
/// <param name=“dt”></param> /// <returns></returns>
|
public static string DateTimeToInt(this DateTime dt)
|
{
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
|
//intResult = (time- startTime).TotalMilliseconds;
|
long t = (dt.Ticks - startTime.Ticks) / 10000000; //现在是10位,除10000调整为13位
|
return t.ToString();
|
}
|
}
|
}
|