给出一个反向转换的示例
使用API,当前时间转MS-DOS时间- cls
-
- $code=@'
- using System;
- using System.Runtime.InteropServices;
- public static class WinApi{
- public struct FILETIME {
- public uint dwLowDateTime;
- public uint dwHighDateTime;
- };
- public struct SYSTEMTIME {
- public short wYear;
- public short wMonth;
- public short wDayOfWeek;
- public short wDay;
- public short wHour;
- public short wMinute;
- public short wSecond;
- public short wMilliseconds;
- };
- [DllImport("kernel32.dll")]
- public static extern bool DosDateTimeToFileTime(short wFatDate, short wFatTime, ref FILETIME lpFileTime);
- [DllImport("kernel32.dll")]
- public static extern bool FileTimeToDosDateTime(ref FILETIME lpFileTime, ref short wFatDate, ref short wFatTime);
- [DllImport("kernel32.dll")]
- public static extern bool FileTimeToSystemTime(ref FILETIME lpFileTime,ref SYSTEMTIME lpSystemTime);
- [DllImport("kernel32.dll")]
- public static extern bool SystemTimeToFileTime(ref SYSTEMTIME lpSystemTime, ref FILETIME lpFileTime);
- [DllImport("kernel32.dll")]
- public static extern void GetLocalTime(ref SYSTEMTIME lpSystemTime);
- }
- '@
- Add-Type -TypeDefinition $code
-
- $st_file_time = New-Object 'WinApi+FILETIME'
- $st_system_time = New-Object 'WinApi+SYSTEMTIME'
- $wFatDate = New-Object 'uint16'
- $wFatTime = New-Object 'uint16'
- [WinApi]::GetLocalTime([ref]$st_system_time)
- [WinApi]::SystemTimeToFileTime([ref]$st_system_time,[ref]$st_file_time)
- [WinApi]::FileTimeToDosDateTime([ref]$st_file_time,[ref]$wFatDate,[ref]$wFatTime)
-
- '当前时间 {0:d4}/{1:d2}/{2:d2} {3:d2}:{4:d2}:{5:d2}' -f $st_system_time.wYear,$st_system_time.wMonth,$st_system_time.wDay,$st_system_time.wHour,$st_system_time.wMinute,$st_system_time.wSecond
- 'MS-DOS wFatDate:{0:X4} wFatTime:{1:X4}' -f $wFatDate,$wFatTime
复制代码
|