| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Core.Native |
| 4 | { |
| 5 | using System; |
| 6 | using System.Runtime.InteropServices; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Interop class for the date/time handling. |
| 10 | /// </summary> |
| 11 | internal static class DateTimeInterop |
| 12 | { |
| 13 | /// <summary> |
| 14 | /// Converts DateTime to MS-DOS date and time which cabinet uses. |
| 15 | /// </summary> |
| 16 | /// <param name="dateTime">DateTime</param> |
| 17 | /// <param name="cabDate">MS-DOS date</param> |
| 18 | /// <param name="cabTime">MS-DOS time</param> |
| 19 | public static void DateTimeToCabDateAndTime(DateTime dateTime, out ushort cabDate, out ushort cabTime) |
| 20 | { |
| 21 | // dateTime.ToLocalTime() does not match FileTimeToLocalFileTime() for some reason. |
| 22 | // so we need to call FileTimeToLocalFileTime() from kernel32.dll. |
| 23 | var filetime = dateTime.ToFileTime(); |
| 24 | var localTime = 0L; |
| 25 | FileTimeToLocalFileTime(ref filetime, ref localTime); |
| 26 | FileTimeToDosDateTime(ref localTime, out cabDate, out cabTime); |
| 27 | } |
| 28 | |
| 29 | /// <summary> |
| 30 | /// Converts file time to a local file time. |
| 31 | /// </summary> |
| 32 | /// <param name="fileTime">file time</param> |
| 33 | /// <param name="localTime">local file time</param> |
| 34 | /// <returns>true if successful, false otherwise</returns> |
| 35 | [DllImport("kernel32.dll", SetLastError = true)] |
| 36 | private static extern bool FileTimeToLocalFileTime(ref long fileTime, ref long localTime); |
| 37 | |
| 38 | /// <summary> |
| 39 | /// Converts file time to a MS-DOS time. |
| 40 | /// </summary> |
| 41 | /// <param name="fileTime">file time</param> |
| 42 | /// <param name="wFatDate">MS-DOS date</param> |
| 43 | /// <param name="wFatTime">MS-DOS time</param> |
| 44 | /// <returns>true if successful, false otherwise</returns> |
| 45 | [DllImport("kernel32.dll", SetLastError = true)] |
| 46 | private static extern bool FileTimeToDosDateTime(ref long fileTime, out ushort wFatDate, out ushort wFatTime); |
| 47 | } |
| 48 | } |