Skip to content

Commit

Permalink
Minor improvements to SystemTime struct (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkl authored Mar 5, 2020
1 parent 270eeb5 commit 42e3665
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 65 deletions.
6 changes: 4 additions & 2 deletions src/Microsoft.Dism/DismDriverPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal struct DismDriverPackage_
/// <summary>
/// The manufacturer's build date of the driver.
/// </summary>
public SYSTEMTIME Date;
public SystemTime Date;

/// <summary>
/// The major version number of the driver.
Expand Down Expand Up @@ -128,6 +128,8 @@ internal DismDriverPackage(DismApi.DismDriverPackage_ driverPackage)
{
_driverPackage = driverPackage;

Date = _driverPackage.Date;

// Copy data from the struct
Version = new Version((int)driverPackage.MajorVersion, (int)driverPackage.MinorVersion, (int)driverPackage.Build, (int)driverPackage.Revision);
}
Expand Down Expand Up @@ -160,7 +162,7 @@ internal DismDriverPackage(DismApi.DismDriverPackage_ driverPackage)
/// <summary>
/// Gets the manufacturer's build date of the driver.
/// </summary>
public DateTime Date => _driverPackage.Date;
public DateTime Date { get; }

/// <summary>
/// Gets the driver signature status.
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.Dism/DismPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal struct DismPackage_
/// <summary>
/// The date and time that the package was installed. This field is local time relative to the servicing host computer.
/// </summary>
public SYSTEMTIME InstallTime;
public SystemTime InstallTime;
}
}

Expand All @@ -61,12 +61,14 @@ public sealed class DismPackage : IEquatable<DismPackage>
internal DismPackage(DismApi.DismPackage_ package)
{
_package = package;

InstallTime = _package.InstallTime;
}

/// <summary>
/// Gets the date and time the package was installed.
/// </summary>
public DateTime InstallTime => _package.InstallTime;
public DateTime InstallTime { get; }

/// <summary>
/// Gets the package name.
Expand Down
16 changes: 10 additions & 6 deletions src/Microsoft.Dism/DismPackageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal struct DismPackageInfo_
/// <summary>
/// The date and time that the package was installed. This field is local time, based on the servicing host computer.
/// </summary>
public SYSTEMTIME InstallTime;
public SystemTime InstallTime;

/// <summary>
/// TRUE if the package is applicable to the image, otherwise FALSE.
Expand All @@ -81,7 +81,7 @@ internal struct DismPackageInfo_
/// <summary>
/// The date and time that the package was created. This field is local time, based on the time zone of the computer that created the package.
/// </summary>
public SYSTEMTIME CreationTime;
public SystemTime CreationTime;

/// <summary>
/// The display name of the package.
Expand All @@ -106,7 +106,7 @@ internal struct DismPackageInfo_
/// <summary>
/// The date and time when this package was last updated. This field is local time, based on the servicing host computer.
/// </summary>
public SYSTEMTIME LastUpdateTime;
public SystemTime LastUpdateTime;

/// <summary>
/// The product name for this package.
Expand Down Expand Up @@ -179,6 +179,10 @@ internal DismPackageInfo(DismApi.DismPackageInfo_ packageInfo)
{
_packageInfo = packageInfo;

CreationTime = _packageInfo.CreationTime;
InstallTime = _packageInfo.InstallTime;
LastUpdateTime = _packageInfo.LastUpdateTime;

CustomProperties = new DismCustomPropertyCollection(_packageInfo.CustomProperty, _packageInfo.CustomPropertyCount);

Features = new DismFeatureCollection(_packageInfo.Feature, _packageInfo.FeatureCount);
Expand All @@ -202,7 +206,7 @@ internal DismPackageInfo(DismApi.DismPackageInfo_ packageInfo)
/// <summary>
/// Gets the date and time that the package was created. This field is local time, based on the time zone of the computer that created the package.
/// </summary>
public DateTime CreationTime => _packageInfo.CreationTime;
public DateTime CreationTime { get; }

/// <summary>
/// Gets an array of DismCustomProperty Structure objects representing the custom properties of the package.
Expand Down Expand Up @@ -242,12 +246,12 @@ internal DismPackageInfo(DismApi.DismPackageInfo_ packageInfo)
/// <summary>
/// Gets the date and time that the package was installed.
/// </summary>
public DateTime InstallTime => _packageInfo.InstallTime;
public DateTime InstallTime { get; }

/// <summary>
/// Gets the date and time when this package was last updated. This field is local time, based on the servicing host computer.
/// </summary>
public DateTime LastUpdateTime => _packageInfo.LastUpdateTime;
public DateTime LastUpdateTime { get; }

/// <summary>
/// Gets the name of the package.
Expand Down
12 changes: 8 additions & 4 deletions src/Microsoft.Dism/DismWimCustomizedInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ internal struct DismWimCustomizedInfo_
/// <summary>
/// The time that the image file was created.
/// </summary>
public SYSTEMTIME CreatedTime;
public SystemTime CreatedTime;

/// <summary>
/// The time that the image file was last modified.
/// </summary>
public SYSTEMTIME ModifiedTime;
public SystemTime ModifiedTime;
}
}

Expand All @@ -76,12 +76,16 @@ internal DismWimCustomizedInfo(IntPtr wimCustomizedInfoPtr)
internal DismWimCustomizedInfo(DismApi.DismWimCustomizedInfo_ wimCustomizedInfo)
{
_wimCustomizedInfo = wimCustomizedInfo;

CreatedTime = _wimCustomizedInfo.CreatedTime;

ModifiedTime = _wimCustomizedInfo.ModifiedTime;
}

/// <summary>
/// Gets the time that the image file was created.
/// </summary>
public DateTime CreatedTime => _wimCustomizedInfo.CreatedTime;
public DateTime CreatedTime { get; }

/// <summary>
/// Gets the number of directories in the image.
Expand All @@ -96,7 +100,7 @@ internal DismWimCustomizedInfo(DismApi.DismWimCustomizedInfo_ wimCustomizedInfo)
/// <summary>
/// Gets the time that the image file was last modified.
/// </summary>
public DateTime ModifiedTime => _wimCustomizedInfo.ModifiedTime;
public DateTime ModifiedTime { get; }

/// <summary>
/// Gets the size of the customized info.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

using WORD = System.UInt16;

// ReSharper disable InconsistentNaming
namespace Microsoft.Dism
{
#pragma warning disable SA1307 // Accessible fields must begin with upper-case letter

/// <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>
Expand All @@ -33,99 +30,79 @@ namespace Microsoft.Dism
/// WORD wSecond;
/// WORD wMilliseconds;
/// } SYSTEMTIME, *PSYSTEMTIME;]]>
[StructLayout(LayoutKind.Sequential)]
#pragma warning disable SA1649 // File name must match first type name
internal struct SYSTEMTIME
#pragma warning restore SA1649 // File name must match first type name
[StructLayout(LayoutKind.Sequential, Pack = 2)]
internal struct SystemTime
{
/// <summary>
/// The year. The valid values for this member are 1601 through 30827.
/// </summary>
public WORD wYear;
public WORD Year;

/// <summary>
/// The month. January = 1 and December = 12
/// </summary>
public WORD wMonth;
public WORD Month;

/// <summary>
/// The day of the week. Sunday = 0 and Saturday = 6
/// </summary>
public WORD wDayOfWeek;
public WORD DayOfWeek;

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

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

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

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

/// <summary>
/// The millisecond. The valid values for this member are 0 through 999.
/// </summary>
public WORD wMilliseconds;
public WORD Milliseconds;

/// <summary>
/// Initializes a new instance of the <see cref="SYSTEMTIME" /> struct.
/// Initializes a new instance of the SYSTEMTIME class.
/// Converts a <see cref="System.DateTime" /> to a <see cref="SystemTime" />.
/// </summary>
/// <param name="dateTime">An existing DateTime object to copy data from.</param>
public SYSTEMTIME(DateTime dateTime)
/// <param name="dateTime">The time to convert.</param>
public static implicit operator SystemTime(DateTime dateTime)
{
DateTime utc = dateTime.ToUniversalTime();

wYear = (ushort)utc.Year;
wMonth = (ushort)utc.Month;
wDay = (ushort)utc.Day;
wDayOfWeek = (ushort)utc.DayOfWeek;
wHour = (ushort)utc.Hour;
wMinute = (ushort)utc.Minute;
wSecond = (ushort)utc.Second;
wMilliseconds = (ushort)utc.Millisecond;
}

/// <summary>
/// Converts a <see cref="System.DateTime" /> to a <see cref="SYSTEMTIME" />.
/// </summary>
/// <param name="dateTime">The time to convert.</param>
public static implicit operator SYSTEMTIME(DateTime dateTime)
{
return new SYSTEMTIME(dateTime);
return new SystemTime
{
Year = (WORD)utc.Year,
Month = (WORD)utc.Month,
Day = (WORD)utc.Day,
DayOfWeek = (WORD)utc.DayOfWeek,
Hour = (WORD)utc.Hour,
Minute = (WORD)utc.Minute,
Second = (WORD)utc.Second,
Milliseconds = (WORD)utc.Millisecond,
};
}

/// <summary>
/// Converts a <see cref="SYSTEMTIME" /> to a <see cref="System.DateTime" />
/// Converts a <see cref="SystemTime" /> to a <see cref="System.DateTime" />
/// </summary>
/// <param name="systemTime">The time to convert.</param>
public static implicit operator DateTime(SYSTEMTIME systemTime)
public static implicit operator DateTime(SystemTime systemTime)
{
return systemTime.ToDateTime();
}

/// <summary>
/// Returns the SYSTEMTIME as a <see cref="System.DateTime" /> value.
/// </summary>
/// <returns>A <see cref="System.DateTime" /> value.</returns>
public DateTime ToDateTime()
{
return wYear == 0
return systemTime.Year == 0
? DateTime.MinValue
: new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, DateTimeKind.Utc);
: new DateTime(systemTime.Year, systemTime.Month, systemTime.Day, systemTime.Hour, systemTime.Minute, systemTime.Second, DateTimeKind.Utc);
}

#pragma warning restore SA1307 // Accessible fields must begin with upper-case letter
}
}

0 comments on commit 42e3665

Please sign in to comment.