Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #282 from jmelosegui/GetSetSystemTime
Browse files Browse the repository at this point in the history
Adding functions to get and set the system time
  • Loading branch information
AArnott committed Sep 3, 2016
2 parents b597a86 + f1a48e0 commit 8d3da2b
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
167 changes: 167 additions & 0 deletions src/Kernel32.Shared/Kernel32+SYSTEMTIME.cs
@@ -0,0 +1,167 @@
// Copyright (c) to owners found in https://github.com/AArnott/pinvoke/blob/master/COPYRIGHT.md. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.

namespace PInvoke
{
using System.Runtime.InteropServices;

/// <content>
/// Contains the <see cref="SYSTEMTIME"/> nested type.
/// </content>
public partial class Kernel32
{
/// <summary>
/// Specifies a date and time, using individual members for the month, day, year, weekday, hour, minute, second, and millisecond.
/// The time is either in coordinated universal time (UTC) or local time, depending on the function that is being called.
/// </summary>
/// <remarks>
/// It is not recommended that you add and subtract values from the <see cref="SYSTEMTIME"/> structure to obtain relative times.
/// Instead, you should
/// Convert the <see cref="SYSTEMTIME"/> structure to a <see cref="FILETIME"/> structure.
/// <list type="bullet">
/// <item>
/// <desccription>Copy the resulting <see cref="FILETIME"/> structure to a ULARGE_INTEGER structure.</desccription>
/// </item>
/// <item>
/// <desccription>Use normal 64-bit arithmetic on the ULARGE_INTEGER value.</desccription>
/// </item>
/// <item>
/// <description>The system can periodically refresh the time by synchronizing with a time source.</description>
/// </item>
/// </list>
/// Because the system time can be adjusted either forward or backward, do not compare system time readings to determine elapsed time.
/// Instead, use one of the methods described in Windows Time.
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
/// <summary>
/// The year. The valid values for this member are 1601 through 30827.
/// </summary>
public short wYear;

/// <summary>
/// The month. This member can be one of the following values.
/// <list type="table">
/// <listheader>
/// <term>Value</term>
/// <term>Meaning</term>
/// </listheader>
/// <item>
/// <term>1</term>
/// <term>January</term>
/// </item>
/// <item>
/// <term>2</term>
/// <term>February</term>
/// </item>
/// <item>
/// <term>3</term>
/// <term>March</term>
/// </item>
/// <item>
/// <term>4</term>
/// <term>April</term>
/// </item>
/// <item>
/// <term>5</term>
/// <term>May</term>
/// </item>
/// <item>
/// <term>6</term>
/// <term>June</term>
/// </item>
/// <item>
/// <term>7</term>
/// <term>July</term>
/// </item>
/// <item>
/// <term>8</term>
/// <term>August</term>
/// </item>
/// <item>
/// <term>9</term>
/// <term>September</term>
/// </item>
/// <item>
/// <term>10</term>
/// <term>October</term>
/// </item>
/// <item>
/// <term>11</term>
/// <term>November</term>
/// </item>
/// <item>
/// <term>12</term>
/// <term>December</term>
/// </item>
/// </list>
/// </summary>
public short wMonth;

/// <summary>
/// The day of the week. This member can be one of the following values.
/// <list type="table">
/// <listheader>
/// <term>Value</term>
/// <term>Meaning</term>
/// </listheader>
/// <item>
/// <term>0</term>
/// <term>Sunday</term>
/// </item>
/// <item>
/// <term>1</term>
/// <term>Monday</term>
/// </item>
/// <item>
/// <term>2</term>
/// <term>Tuesday</term>
/// </item>
/// <item>
/// <term>3</term>
/// <term>Wednesday</term>
/// </item>
/// <item>
/// <term>4</term>
/// <term>Thursday</term>
/// </item>
/// <item>
/// <term>5</term>
/// <term>Friday</term>
/// </item>
/// <item>
/// <term>6</term>
/// <term>Saturday</term>
/// </item>
/// </list>
/// </summary>
public short wDayOfWeek;

/// <summary>
/// The day of the month. The valid values for this member are 1 through 31.
/// </summary>
public short wDay;

/// <summary>
/// The hour. The valid values for this member are 0 through 23.
/// </summary>
public short wHour;

/// <summary>
/// The minute. The valid values for this member are 0 through 59.
/// </summary>
public short wMinute;

/// <summary>
/// The second. The valid values for this member are 0 through 59.
/// </summary>
public short wSecond;

/// <summary>
/// The millisecond. The valid values for this member are 0 through 999.
/// </summary>
public short wMilliseconds;
}
}
}
1 change: 1 addition & 0 deletions src/Kernel32.Shared/Kernel32.Shared.projitems
Expand Up @@ -26,6 +26,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Kernel32+SECURITY_ATTRIBUTES.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32+SECURITY_DESCRIPTOR.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32+SECURITY_IMPERSONATION_LEVEL.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32+SYSTEMTIME.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32+WaitForSingleObjectResult.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32+WIN32_FIND_DATA.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Kernel32.cs">
Expand Down
30 changes: 30 additions & 0 deletions src/Kernel32.Shared/Kernel32.cs
Expand Up @@ -42,6 +42,7 @@ public static partial class Kernel32
private const string api_ms_win_core_namedpipe_l1_2_0 = ApiSets.api_ms_win_core_namedpipe_l1_2_0;
private const string api_ms_win_core_libraryloader_l1_1_1 = ApiSets.api_ms_win_core_libraryloader_l1_1_1;
private const string api_ms_win_core_sysinfo_l1_2_1 = ApiSets.api_ms_win_core_sysinfo_l1_2_1;
private const string api_ms_win_core_sysinfo_l1_2_0 = ApiSets.api_ms_win_core_sysinfo_l1_2_0;
#else
private const string api_ms_win_core_localization_l1_2_0 = nameof(Kernel32);
private const string api_ms_win_core_processthreads_l1_1_1 = nameof(Kernel32);
Expand All @@ -55,6 +56,7 @@ public static partial class Kernel32
private const string api_ms_win_core_namedpipe_l1_2_0 = nameof(Kernel32);
private const string api_ms_win_core_libraryloader_l1_1_1 = nameof(Kernel32);
private const string api_ms_win_core_sysinfo_l1_2_1 = nameof(Kernel32);
private const string api_ms_win_core_sysinfo_l1_2_0 = nameof(Kernel32);
#endif
#pragma warning restore SA1303 // Const field names must begin with upper-case letter

Expand Down Expand Up @@ -599,6 +601,34 @@ public static partial class Kernel32
out int pBytesReturned,
OVERLAPPED* lpOverlapped);

/// <summary>
/// Retrieves the current system date and time. The system time is expressed in Coordinated Universal Time (UTC).
/// To retrieve the current system date and time in local time, use the GetLocalTime function.
/// </summary>
/// <param name="lpSystemTime">
/// A pointer to a SYSTEMTIME structure to receive the current system date and time.
/// The lpSystemTime parameter must not be NULL. Using NULL will result in an access violation.
/// </param>
[DllImport(api_ms_win_core_sysinfo_l1_2_0)]
public static extern unsafe void GetSystemTime([Friendly(FriendlyFlags.Out)] SYSTEMTIME* lpSystemTime);

/// <summary>
/// Sets the current system time and date. The system time is expressed in Coordinated Universal Time (UTC).
/// </summary>
/// <param name="lpSystemTime">
/// A pointer to a <see cref="SYSTEMTIME"/> structure that contains the new system date and time.
/// The wDayOfWeek member of the <see cref="SYSTEMTIME"/> structure is ignored.</param>
/// <returns>
/// If the function succeeds, the return value is a nonzero value.
/// <para>
/// If the function fails, the return value is zero. To get extended error information, call
/// <see cref="GetLastError" />.
/// </para>
/// </returns>
[DllImport(api_ms_win_core_sysinfo_l1_2_0, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern unsafe bool SetSystemTime([Friendly(FriendlyFlags.In)] SYSTEMTIME* lpSystemTime);

/// <summary>
/// Closes a file search handle opened by the FindFirstFile, FindFirstFileEx, FindFirstFileNameW,
/// FindFirstFileNameTransactedW, FindFirstFileTransacted, FindFirstStreamTransactedW, or FindFirstStreamW functions.
Expand Down
5 changes: 5 additions & 0 deletions src/Windows.Core.Shared/ApiSets.cs
Expand Up @@ -59,6 +59,11 @@ public static class ApiSets
/// </summary>
public const string api_ms_win_core_sysinfo_l1_2_1 = "api-ms-win-core-sysinfo-l1-2-1.dll";

/// <summary>
/// The "api-ms-win-core-sysinfo-l1-2-0.dll" constant.
/// </summary>
public const string api_ms_win_core_sysinfo_l1_2_0 = "api-ms-win-core-sysinfo-l1-2-0.dll";

/// <summary>
/// The "api-ms-win-core-handle-l1-1-0.dll" constant.
/// </summary>
Expand Down

0 comments on commit 8d3da2b

Please sign in to comment.