📄 time.cs
字号:
using System;
using System.Runtime.InteropServices;
namespace PInvokeLibrary
{
/// <summary>
/// Allows the user to get/set the system time.
/// </summary>
public class Time
{
/// <summary>
/// This structure represents a date and time using individual members for
/// the month, day, year, weekday, hour, minute, second, and millisecond.
/// </summary>
public struct SYSTEMTIME
{
/// <summary>
/// Specifies the current year.
/// </summary>
public ushort wYear;
/// <summary>
/// Specifies the current month; January = 1, February = 2, and so on.
/// </summary>
public ushort wMonth;
/// <summary>
/// Specifies the current day of the week; Sunday = 0, Monday = 1, and so on.
/// </summary>
public ushort wDayOfWeek;
/// <summary>
/// Specifies the current day of the month.
/// </summary>
public ushort wDay;
/// <summary>
/// Specifies the current hour.
/// </summary>
public ushort wHour;
/// <summary>
/// Specifies the current minute.
/// </summary>
public ushort wMinute;
/// <summary>
/// Specifies the current second.
/// </summary>
public ushort wSecond;
/// <summary>
/// Specifies the current millisecond.
/// </summary>
public ushort wMilliseconds;
}
/// <summary>
/// This function retrieves the current system date and time. The system time
/// is expressed in Coordinated Universal Time (UTC).
/// </summary>
/// <param name="lpSystemTime">[out] Pointer to a SYSTEMTIME structure to
/// receive the current system date and time.</param>
[DllImport("coredll.dll")]
public extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
/// <summary>
/// This function sets the current system time and date. The system time is
/// expressed in Coordinated Universal Time (UTC).
/// </summary>
/// <param name="lpSystemTime">[in] Pointer to a SYSTEMTIME structure that
/// contains the current system date and time.</param>
/// <returns></returns>
[DllImport("coredll.dll")]
public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
/// <summary>
/// Run a test of the Time class.
/// </summary>
/// <param name="showLine">Delegate called to show debug information</param>
public static void TestProc(MainTest.DisplayLineDelegate showLine)
{
DateTime dt = DateTime.UtcNow.ToLocalTime();
showLine(String.Format("Current Time: {0}:{1:00}:{2:00} {3}", dt.Hour > 12 ? dt.Hour - 12 : dt.Hour, dt.Minute, dt.Second, dt.Hour > 12 ? "PM" : "AM"));
SYSTEMTIME st = new SYSTEMTIME();
GetSystemTime(ref st);
showLine("Adding 1 hour...");
st.wHour = (ushort)(st.wHour + 1 % 24);
if (SetSystemTime(ref st) == 0)
showLine("FAILURE: SetSystemTime failed");
dt = DateTime.UtcNow.ToLocalTime();
showLine(String.Format("Current Time: {0}:{1:00}:{2:00} {3}", dt.Hour > 12 ? dt.Hour - 12 : dt.Hour, dt.Minute, dt.Second, dt.Hour > 12 ? "PM" : "AM"));
showLine("Setting time back...");
st.wHour = (ushort)(st.wHour - 1 % 24);
if (SetSystemTime(ref st) == 0)
showLine("FAILURE: SetSystemTime failed");
dt = DateTime.UtcNow.ToLocalTime();
showLine(String.Format("Current Time: {0}:{1:00}:{2:00} {3}", dt.Hour > 12 ? dt.Hour - 12 : dt.Hour, dt.Minute, dt.Second, dt.Hour > 12 ? "PM" : "AM"));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -