<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Windows API 中的日期和時間 > 原文: [http://zetcode.com/gui/winapi/datetime/](http://zetcode.com/gui/winapi/datetime/) 在 Windows API 教程的這一部分中,我們將使用日期和時間。 ZetCode 的[文章](http://zetcode.com/articles/cdatetime/)處理 ANSI C 中的日期和時間。 `SYSTEMTIME`結構用于 Windows API 中的日期和時間。 時間可以是協調世界時(UTC)或本地時間。 它具有以下成員: ```c WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; ``` `SYSTEMTIME`結構用`GetSystemTime()`函數或`GetLocalTime()`函數填充。 然后,我們可以訪問結構的成員以獲取當前日期或時間。 `FILETIME`結構包含一個 64 位的值,該值表示自 1601 年 1 月 1 日(UTC)起 100 納秒間隔的數量。 使用此值,我們可以計算 Windows API 紀元或日期時間差。 ```c DWORD dwLowDateTime; DWORD dwHighDateTime; ``` `FILETIME`結構具有兩個成員:`dwLowDateTime`是文件時間的低階部分,`dwHighDateTime`是文件時間的高階部分。 為了從兩個成員中獲得單個值,我們利用了`LARGE_INTEGER`并集。 `FileTimeToSystemTime()`和`SystemTimeToFileTime()`函數用于在兩個結構之間進行轉換。 ## 當地時間 本地時間定義為用戶所在時區中的當前時間。 `localtime.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { SYSTEMTIME lt = {0}; GetLocalTime(&lt); wprintf(L"The local time is: %02d:%02d:%02d\n", lt.wHour, lt.wMinute, lt.wSecond); return 0; } ``` 程序將打印本地時間。 ```c SYSTEMTIME lt = {0}; ``` 我們聲明`SYSTEMTIME`結構。 通過調用特定的時間函數來填充此結構的成員。 ```c GetLocalTime(&lt); ``` `GetLocalTime()`檢索當前的本地日期和時間。 它用當前日期&時間值填充`SYSTEMTIME`結構的成員。 ```c wprintf(L"The local time is: %02d:%02d:%02d\n", lt.wHour, lt.wMinute, lt.wSecond); ``` 我們以`hh:mm:ss`格式打印當前本地時間。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\LocalTime>LocalTime.exe The local time is: 20:20:07 ``` 這是示例輸出。 ## UTC 時間 我們的星球是一個球體。 它繞其軸旋轉。 地球向東旋轉。 因此,太陽在不同位置的不同時間升起。 地球大約每 24 小時旋轉一次。 因此,世界被劃分為 24 個時區。 在每個時區,都有一個不同的本地時間。 夏令時通常會進一步修改此本地時間。 實際需要一個全球時間。 全球時間可以避免時區和夏令時的混淆。 UTC(世界標準時間)被選為主要時間標準。 UTC 用于航空,天氣預報,飛行計劃,空中交通管制通關和地圖。 與當地時間不同,UTC 不會隨季節變化而變化。 Windows API 具有`GetSystemTime()`函數以獲取 UTC 時間。 `utctime.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { SYSTEMTIME st = {0}; GetSystemTime(&st); wprintf(L"The UTC time is: %02d:%02d:%02d\n", st.wHour, st.wMinute, st.wSecond); return 0; } ``` 在示例中,我們計算 UTC 時間。 ```c SYSTEMTIME st = {0}; ``` UTC 時間將存儲在`SYSTEMTIME`結構中。 ```c GetSystemTime(&st); ``` 我們使用`GetSystemTime()`函數檢索 UTC 時間。 ```c wprintf(L"The UTC time is: %02d:%02d:%02d\n", st.wHour, st.wMinute, st.wSecond); ``` UTC 時間以`hh:mm:ss`格式打印到控制臺。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\UtcTime>UtcTime.exe The UTC time is: 19:25:20 ``` 這是 UTC 時間的輸出。 ## 算術 不建議對`SYSTEMTIME`結構中的值進行算術運算以獲得相對時間。 相反,我們將`SYSTEMTIME`結構轉換為`FILETIME`結構,將所得的`FILETIME`結構復制到`ULARGE_INTEGER`結構,并對`ULARGE_INTEGER`值使用常規的 64 位算術。 最后,我們將`FILETIME`結構轉換回`SYSTEMTIME`結構。 `arithmetic.c` ```c #include <windows.h> #include <wchar.h> #define NSECS 60*60*3 int wmain(void) { SYSTEMTIME st = {0}; FILETIME ft = {0}; GetLocalTime(&st); wprintf(L"%02d/%02d/%04d %02d:%02d:%02d\n", st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond); SystemTimeToFileTime(&st, &ft); ULARGE_INTEGER u = {0}; memcpy(&u, &ft, sizeof(u)); u.QuadPart += NSECS * 10000000LLU; memcpy(&ft, &u, sizeof(ft)); FileTimeToSystemTime(&ft, &st); wprintf(L"%02d/%02d/%04d %02d:%02d:%02d\n", st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond); return 0; } ``` 在示例中,我們將三個小時添加到當前本地時間值。 ```c #define NSECS 60*60*3 ``` 三個小時以秒表示。 ```c GetLocalTime(&st); ``` 使用`GetLocalTime()`函數,我們可以獲取當前本地時間。 ```c SystemTimeToFileTime(&st, &ft); ``` 我們調用`SystemTimeToFileTime()`函數將`SYSTEMTIME`結構轉換為`FILETIME`結構。 ```c ULARGE_INTEGER u = {0}; ``` `ULARGE_INTEGER`結構已創建。 ```c memcpy(&u, &ft, sizeof(u)); u.QuadPart += NSECS * 10000000LLU; memcpy(&ft, &u, sizeof(ft)); ``` 我們向`ULARGE_INTEGER`結構的`QuadPart`成員添加了三個小時。 成員以 100 納秒刻度表示; 因此,我們將`NSECS`乘以`10000000LLU`。 ```c FileTimeToSystemTime(&ft, &st); ``` 我們將`FILETIME`結構轉換回`SYSTEMTIME`結構。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\Arithmetic>Arithmetic.exe 01/02/2016 13:28:13 01/02/2016 16:28:13 ``` 這是`Arithmetic.exe`的示例輸出。 已將三個小時正確地添加到當前本地時間。 ## 日期 `GetLocalTime()`函數還用于確定當前日期。 `today.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { SYSTEMTIME st = {0}; GetLocalTime(&st); wprintf(L"Today is: %d-%02d-%02d\n", st.wYear, st.wMonth, st.wDay); return 0; } ``` 上面的程序打印今天的日期。 ```c SYSTEMTIME st = {0}; ``` 我們聲明一個`SYSTEMTIME`結構。 ```c GetLocalTime(&st); ``` 我們用當前的本地時間和日期值填充`SYSTEMTIME`成員。 ```c wprintf(L"Today is: %d-%02d-%02d\n", st.wYear, st.wMonth, st.wDay); ``` 當前日期將打印到控制臺。 我們選擇了公歷大端日期格式。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\Today>Today.exe Today is: 2016-01-30 ``` 這是`Today.exe`程序的輸出。 ## 格式化日期 `GetDateFormatEx()`函數將日期格式化為指定語言環境的日期字符串。 `date_format.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { PDWORD cChars = NULL; HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE); if (std == INVALID_HANDLE_VALUE) { wprintf(L"Cannot retrieve standard output handle %d\n", GetLastError()); return 1; } SYSTEMTIME lt = {0}; GetLocalTime(&lt); wchar_t buf[128] = {0}; int r = GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, DATE_LONGDATE, &lt, NULL, buf, sizeof(buf)/sizeof(buf[0]), NULL); if (r == 0) { wprintf(L"GetDateFormatEx function failed %d\n", GetLastError()); CloseHandle(std); return 1; } WriteConsoleW(std, buf, wcslen(buf), cChars, NULL); r = CloseHandle(std); if (r == 0) { wprintf(L"Cannot close console handle %d\n", GetLastError()); return 1; } CloseHandle(std); return 0; } ``` 程序以本地化格式打印當前本地時間。 ```c SYSTEMTIME lt = {0}; GetLocalTime(&lt); ``` 檢索當地時間。 ```c int r = GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, DATE_LONGDATE, &lt, NULL, buf, sizeof(buf)/sizeof(buf[0]), NULL); ``` `GetDateFormatEx()`以區域和語言選項中指定的默認語言環境格式化日期。 日期以長日期格式打印。 ```c WriteConsoleW(std, buf, wcslen(buf), cChars, NULL); ``` 日期將打印到控制臺。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\DateFormat>DateFormat.exe 1\. februára 2016 ``` 程序以斯洛伐克語打印日期。 ## 確定閏年 閏年是包含額外一天的年份。 日歷中額外一天的原因是天文日歷年與日歷年之間的差異。 日歷年正好是 365 天,而天文學年(地球繞太陽公轉的時間)是 365.25 天。 相差 6 個小時,這意味著在四年的時間里,我們一天中都沒有。 因為我們希望日歷與季節同步,所以每四年將 2 月增加一天。 (有例外。)在公歷中,February 年的 2 月有 29 天,而不是通常的 28 天。該年持續 366 天,而不是通常的 365 天。 `leapyear.c` ```c #include <windows.h> #include <stdbool.h> #include <wchar.h> bool isLeapYear(int); int wmain(void) { // Assume year >= 1582 in the Gregorian calendar. int years[] = { 2000, 2002, 2004, 2008, 2012, 2016, 2020, 1900, 1800, 1600 }; int size = sizeof(years) / sizeof(int); for (int i=0; i<size; i++) { if (isLeapYear(years[i])) { wprintf(L"%ld is a leap year\n", years[i]); } else { wprintf(L"%ld is not a leap year\n", years[i]); } } return 0; } bool isLeapYear(int year) { if (year % 4 != 0) { return false; } else if (year % 400 == 0) { return true; } else if (year % 100 == 0) { return false; } else { return true; } } ``` 我們有很多年。 我們檢查所有年份是否為閏年。 沒有內置函數可以檢查閏年。 我們創建了一個自定義的`isLeapYear()`函數。 ```c // Assume year >= 1582 in the Gregorian calendar. int years[] = { 2000, 2002, 2004, 2008, 2012, 2016, 2020, 1900, 1800, 1600 }; ``` 這是我們要檢查的年份。 年份必須在公歷中。 ```c for (int i=0; i<size; i++) { if (isLeapYear(years[i])) { wprintf(L"%ld is a leap year\n", years[i]); } else { wprintf(L"%ld is not a leap year\n", years[i]); } } ``` 使用 for 循環,我們遍歷數組。 我們使用`isLeapYear()`函數檢查年份是否為閏年。 ```c bool isLeapYear(int year) { if (year % 4 != 0) { return false; } else if (year % 400 == 0) { return true; } else if (year % 100 == 0) { return false; } else { return true; } } ``` 這是確定閏年的功能。 年是 4 的整數倍。年份是 100 的整數倍,則不是閏年,除非它也是 400 的整數倍,在這種情況下,它也是閏年。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\LeapYear>LeapYear.exe 2000 is a leap year 2002 is not a leap year 2004 is a leap year 2008 is a leap year 2012 is a leap year 2016 is a leap year 2020 is a leap year 1900 is not a leap year 1800 is not a leap year 1600 is a leap year ``` `LeapYear.exe`程序的輸出。 ## 正常運行時間 `GetTickCount()`函數可用于獲取計算機的正常運行時間。 它檢索自系統啟動以來經過的毫秒數。 ```c DWORD WINAPI GetTickCount(void); ``` 該函數返回`DWORD`值,因此返回的最大天數為 49.7。 為了克服這個限制,我們可以使用`GetTickCount64()`。 從 Windows Vista 開始,該函數可用。 `uptime.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { DWORD tc = GetTickCount(); short seconds = tc / 1000 % 60; short minutes = tc / 1000 / 60 % 60; short hours = tc / 1000 / 60 / 60 % 24; short days = tc / 1000 / 60 / 60 / 24 % 7; short weeks = tc / 1000 / 60 / 60 / 24 / 7 % 52; wprintf(L"Computer has been running for: "); if (weeks > 0 && weeks != 1) { wprintf(L"%hi weeks ", weeks); } else if (weeks == 1) { wprintf(L"1 week "); } if (days > 0 && days != 1) { wprintf(L"%hi days ", days); } else if (days == 1) { wprintf(L"1 day "); } if (hours > 0 && hours != 1) { wprintf(L"%hi hours ", hours); } else if (hours == 1) { wprintf(L"1 hour "); } if (minutes > 0 && minutes != 1) { wprintf(L"%hi minutes ", minutes); } else if (minutes == 1) { wprintf(L"1 minute "); } wprintf(L"and %hi seconds\n", seconds); return 0; } ``` 該程序將打印計算機的正常運行時間。 我們使用`GetTickCount()`函數。 如果計算機運行的時間少于 49.71 天或 4294967296 ms,它將正常工作。 之后,`DWORD`值溢出。 ```c DWORD tc = GetTickCount(); ``` 我們得到計算機正在運行的毫秒數。 `DWORD`變量可以存儲的最大數量為`ULONG_MAX`。 ```c short seconds = tc / 1000 % 60; short minutes = tc / 1000 / 60 % 60; short hours = tc / 1000 / 60 / 60 % 24; short days = tc / 1000 / 60 / 60 / 24 % 7; short weeks = tc / 1000 / 60 / 60 / 24 / 7 % 52; ``` 我們計算秒,分鐘,小時,天和周。 ```c if (weeks > 0 && weeks != 1) { wprintf(L"%hi weeks ", weeks); } else if (weeks == 1) { wprintf(L"1 week "); } ``` 如果計算機正在運行一個或多個星期,則可以在控制臺上打印`week`變量或`"1 week"`字符串。 ```c C:\winapi\examples2\datetime\Uptime>Uptime.exe Computer has been running for: 3 hours 31 minutes and 7 seconds ``` 樣本輸出。 ## 星期幾 `SYSTEMTIME`結構的`wDayOfWeek`成員存儲星期幾。 值是`1..7`,其中 1 是星期日,2 星期一,... 7 星期六。 `dayofweek.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { SYSTEMTIME st = {0}; wchar_t *dn[] = { L"Sunday", L"Monday", L"Tuesday", L"Wednesday", L"Thursday", L"Friday", L"Saturday" }; GetLocalTime(&st); wprintf(L"Today is %ls\n", dn[st.wDayOfWeek]); return 0; } ``` 該代碼將星期幾顯示在控制臺上。 ```c wchar_t *dn[] = { L"Sunday", L"Monday", L"Tuesday", L"Wednesday", L"Thursday", L"Friday", L"Saturday" }; ``` 我們將日期名稱存儲在字符串數組中。 ```c GetLocalTime(&st); wprintf(L"Today is %ls\n", dn[st.wDayOfWeek]); ``` 這些行檢索并打印星期幾。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\DayOfWeek>DayOfWeek.exe Today is Sunday ``` 這是輸出。 ## 紀元 紀元是選擇作為特定紀元起源的時間瞬間。 例如,在西方基督教國家,時期從耶穌出生(據信出生)的第 0 天開始。 另一個例子是法國共和黨日歷,使用了十二年。 這個時期是 1792 年 9 月 22 日宣布的共和紀元的開始,即宣布成立第一共和國并廢除君主制的那一天。 電腦也有自己的紀元。 最受歡迎的時間之一是 Unix 時間。 Unix 紀元是 1970 年 1 月 1 日 UTC 時間 00:00:00(或`1970-01-01T00:00:00Z ISO8601`)。 計算機中的日期和時間是根據自該計算機或平臺的定義時期以來經過的秒數或時鐘滴答數確定的。 Windows 操作系統有幾個時期。 Microsoft Excel,MS SQL Server 或 FAT32 文件系統具有不同的時間紀元。 Windows API 紀元是 UTC 1601 年 1 月 1 日 00:00:00。 選擇此日期的原因是接受公歷 400 周年。 周年紀念日是在 Windows NT 設計之初。 `FILETIME`結構包含一個 64 位的值,該值表示自 1601 年 1 月 1 日(UTC)起 100 納秒間隔的數量。 `windows_epoch.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { FILETIME ft = {0}; GetSystemTimeAsFileTime(&ft); LARGE_INTEGER li = {0}; li.LowPart = ft.dwLowDateTime; li.HighPart = ft.dwHighDateTime; long long int hns = li.QuadPart; wprintf(L"%lli hundreds of nanoseconds have elapsed " "since Windows API epoch\n", hns); return 0; } ``` 該代碼示例計算從 Windows API 紀元到現在為止經過的 100 納秒間隔的數量。 ```c FILETIME ft = {0}; ``` 我們聲明`FILETIME`結構。 它有兩個成員。 `dwLowDateTime`保留文件時間的低位部分。 `dwHighDateTime`保留文件時間的高位部分。 ```c LARGE_INTEGER li = {0}; ``` `LARGE_INTEGER`是一個聯合,可幫助我們將`FILETIME`結構的成員轉換為 100 納秒的間隔。 ```c li.LowPart = ft.dwLowDateTime; li.HighPart = ft.dwHighDateTime; ``` `FILETIME`結構的值將復制到大整數聯合成員。 ```c long long int hns = li.QuadPart; ``` `QuadPart`成員存儲從`LowPart`和`HighPart`成員確定的數百納秒數。 它是一個巨大的數字,存儲為 64 位整數。 ```c wprintf(L"%lli hundreds of nanoseconds have elapsed " "since Windows API epoch\n", hns); ``` 該值將打印到控制臺。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\WindowsEpoch>WindowsEpoch.exe 130987330019489987 hundreds of nanoseconds have elapsed since Windows API epoch ``` 這是示例輸出。 以下示例將 Windows API 時間轉換為 Unix 時間。 `unix_time.c` ```c #include <windows.h> #include <wchar.h> #define WINDOWS_TICKS_PER_SEC 10000000 #define EPOCH_DIFFERENCE 11644473600LL long long WindowsTicksToUnixSeconds(long long); int wmain(void) { FILETIME ft = {0}; GetSystemTimeAsFileTime(&ft); LARGE_INTEGER li = {0}; li.LowPart = ft.dwLowDateTime; li.HighPart = ft.dwHighDateTime; long long int hns = li.QuadPart; wprintf(L"Windows API time: %lli\n", hns); long long int utm = WindowsTicksToUnixSeconds(hns); wprintf(L"Unix time: %lli\n", utm); return 0; } long long int WindowsTicksToUnixSeconds(long long windowsTicks) { return (windowsTicks / WINDOWS_TICKS_PER_SEC - EPOCH_DIFFERENCE); } ``` 該示例顯示 Windows API 時間和 Unix 控制臺時間。 ```c #define EPOCH_DIFFERENCE 11644473600LL ``` 兩個時期之間的差是`11644473600LL`。 請注意,閏秒是 1972 年引入的,因此我們沒有將它們考慮在內。 ```c long long int WindowsTicksToUnixSeconds(long long windowsTicks) { return (windowsTicks / WINDOWS_TICKS_PER_SEC - EPOCH_DIFFERENCE); } ``` 該函數將 Windows 刻度轉換為 Unix 時間秒。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\UnixTime>UnixTime.exe Windows API time: 130987431026414297 Unix time: 1454269502 ``` 這是`UnixTime.exe`示例的輸出。 ## 直到 XMas 的天數 Windows API 沒有任何函數來計算兩天之間的差異。 我們必須自己做數學。 `days_to_xmas.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { FILETIME ft1 = {0}; FILETIME ft2 = {0}; SYSTEMTIME st = {0}; LARGE_INTEGER li1 = {0}; LARGE_INTEGER li2 = {0}; st.wYear = 2016; st.wMonth = 12; st.wDay = 25; int r = SystemTimeToFileTime(&st, &ft1); if (r == 0) { wprintf(L"Failed to convert system time to file time\n (%d)", GetLastError()); return 1; } GetSystemTimeAsFileTime(&ft2); li1.LowPart = ft1.dwLowDateTime; li1.HighPart = ft1.dwHighDateTime; li2.LowPart = ft2.dwLowDateTime; li2.HighPart = ft2.dwHighDateTime; long long int dif = li1.QuadPart - li2.QuadPart; int days2xmas = dif / 10000000L / 60 / 60 / 24; if (days2xmas == 1) { wprintf(L"There is one day until Christmas\n", days2xmas); } else if (days2xmas == 0) { wprintf(L"Today is Chritmas\n"); } else { wprintf(L"There are %d days until Christmas\n", days2xmas); } return 0; } ``` 該代碼示例計算直到圣誕節的天數。 ```c FILETIME ft1 = {0}; FILETIME ft2 = {0}; SYSTEMTIME st = {0}; LARGE_INTEGER li1 = {0}; LARGE_INTEGER li2 = {0}; ``` 我們需要`FILETIME`,`SYSTEMTIME`結構和`LARGE_INTEGER`并集來進行計算。 ```c st.wYear = 2016; st.wMonth = 12; st.wDay = 25; ``` `SYSTEMTIME`結構填充了圣誕節的值。 ```c int r = SystemTimeToFileTime(&st, &ft1); ``` 圣誕節的系統時間將轉換為文件時間。 ```c GetSystemTimeAsFileTime(&ft2); ``` 我們使用`GetSystemTimeAsFileTime()`函數將當前日期作為文件時間。 ```c li1.LowPart = ft1.dwLowDateTime; li1.HighPart = ft1.dwHighDateTime; li2.LowPart = ft2.dwLowDateTime; li2.HighPart = ft2.dwHighDateTime; ``` 我們用文件時間的低位和高位部分填充兩個并集。 ```c long long int dif = li1.QuadPart - li2.QuadPart; ``` 計算兩個日期之間的差。 ```c int days2xmas = dif / 10000000L / 60 / 60 / 24; ``` 差異以 100 納秒表示。 此值轉換為天。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\DaysToXmas>DaysToXmas.exe There are 328 days until Christmas ``` 在 2016 年 1 月 31 日,我們獲得了此輸出。 ## 比較時間 `CompareFileTime()`函數可用于比較兩個文件時間。 當指定的第一次時間較早時,該函數返回 -1。 當兩次相等時,它返回 0。 當第一次晚于第二個文件時間時,它將返回 1。 `compare_time.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { SYSTEMTIME st1 = {0}; SYSTEMTIME st2 = {0}; FILETIME ft1 = {0}; FILETIME ft2 = {0}; st1.wYear = 2015; st1.wMonth = 4; st1.wDay = 12; st2.wYear = 2015; st2.wMonth = 5; st2.wDay = 12; int r1 = SystemTimeToFileTime(&st1, &ft1); if (r1 == 0) { wprintf(L"Failed to convert system time to file time\n (%d)", GetLastError()); return 1; } int r2 = SystemTimeToFileTime(&st2, &ft2); if (r2 == 0) { wprintf(L"Failed to convert system time to file time\n (%d)", GetLastError()); return 1; } short ct = CompareFileTime(&ft1, &ft2); if (ct == -1) { wprintf(L"4/12/2015 comes before 5/12/2015\n"); } else if (ct == 0) { wprintf(L"4/12/2015 is equal to 5/12/2015\n"); } else if (ct == 1) { wprintf(L"4/12/2015 comes after 5/12/2015\n"); } return 0; } ``` 我們有兩個時間值。 我們使用`CompareFileTime()`來確定哪個時間更早。 ```c st1.wYear = 2015; st1.wMonth = 4; st1.wDay = 12; st2.wYear = 2015; st2.wMonth = 5; st2.wDay = 12; ``` 兩次定義。 ```c int r1 = SystemTimeToFileTime(&st1, &ft1); if (r1 == 0) { wprintf(L"Failed to convert system time to file time\n (%d)", GetLastError()); return 1; } int r2 = SystemTimeToFileTime(&st2, &ft2); if (r2 == 0) { wprintf(L"Failed to convert system time to file time\n (%d)", GetLastError()); return 1; } ``` 使用`SystemTimeToFileTime()`函數調用將系統時間轉換為文件時間。 ```c short ct = CompareFileTime(&ft1, &ft2); ``` 將兩個文件時間與`CompareFileTime()`函數進行比較。 ```c if (ct == -1) { wprintf(L"4/12/2015 comes before 5/12/2015\n"); } else if (ct == 0) { wprintf(L"4/12/2015 is equal to 5/12/2015\n"); } else if (ct == 1) { wprintf(L"4/12/2015 comes after 5/12/2015\n"); } ``` 根據返回的值,我們將消息打印到控制臺。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\CompareTime>CompareTime.exe 4/12/2015 comes before 5/12/2015 ``` 這是`CompareTime.exe`程序的輸出。 ## 時區 時區是一個使用相同標準時間的區域。 世界上有 24 個時區。 ```c UTC = local time + bias ``` 偏差是 UTC 時間與本地時間之間的差異(以分鐘為單位)。 ### 檢索時區 `GetTimeZoneInformation()`用于獲取時區信息。 信息存儲在`TIME_ZONE_INFORMATION`結構中。 `get_time_zone.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { TIME_ZONE_INFORMATION tzi = {0}; int r = GetTimeZoneInformation(&tzi); if (r == TIME_ZONE_ID_INVALID) { wprintf(L"Failed to get time zone %d", GetLastError()); return 1; } wprintf(L"Time zone: %ls\n", tzi.StandardName); wprintf(L"The bias is: %ld minutes\n", tzi.Bias); return 0; } ``` 該示例顯示用戶的時區。 ```c TIME_ZONE_INFORMATION tzi = {0}; ``` `TIME_ZONE_INFORMATION`結構存儲時區的設置。 ```c int r = GetTimeZoneInformation(&tzi); ``` `GetTimeZoneInformation()`函數檢索當前時區設置。 ```c wprintf(L"Time zone: %ls\n", tzi.StandardName); ``` `TIME_ZONE_INFORMATION`結構的`StandardName`成員存儲我們時區的名稱。 ```c wprintf(L"The bias is: %ld minutes\n", tzi.Bias); ``` 我們打印偏差值。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\GetTimeZone>GetTimeZone.exe Time zone: Central Europe Standard Time The bias is: -60 minutes ``` 我們的時區是中歐標準時間(CEST),偏差是 -60 分鐘。 ### 將當地時間轉換為世界時間 `TzSpecificLocalTimeToSystemTime()`函數將本地時間轉換為 UTC 時間。 該函數考慮了夏令時(DST)對于要轉換的當地時間是否有效。 `localtime_to_universaltime.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { SYSTEMTIME lt = {0}; GetLocalTime(&lt); TIME_ZONE_INFORMATION tzi = {0}; GetTimeZoneInformation(&tzi); SYSTEMTIME utm = {0}; int r = TzSpecificLocalTimeToSystemTime(&tzi, &lt, &utm); if (r == 0) { wprintf(L"Failed to convert local time to system time %d\n)", GetLastError()); return 1; } wprintf(L"Date: %d/%d/%d\n", lt.wMonth, lt.wDay, lt.wYear); wprintf(L"The local time is: %02d:%02d:%02d\n", lt.wHour, lt.wMinute, lt.wSecond); wprintf(L"The universal time is: %02d:%02d:%02d\n", utm.wHour, utm.wMinute, utm.wSecond); return 0; } ``` 該示例將本地時間轉換為世界時間。 ```c SYSTEMTIME lt = {0}; GetLocalTime(&lt); ``` 當前的本地時間通過`GetLocalTime()`函數獲取。 ```c TIME_ZONE_INFORMATION tzi = {0}; GetTimeZoneInformation(&tzi); ``` 時區設置由`GetTimeZoneInformation()`函數確定。 ```c int r = TzSpecificLocalTimeToSystemTime(&tzi, &lt, &utm); ``` `TzSpecificLocalTimeToSystemTime()`將夏令時考慮在內,將當地時間轉換為世界時間。 ```c wprintf(L"The local time is: %02d:%02d:%02d\n", lt.wHour, lt.wMinute, lt.wSecond); ``` 將本地時間打印到控制臺。 ```c wprintf(L"The universal time is: %02d:%02d:%02d\n", utm.wHour, utm.wMinute, utm.wSecond); ``` 世界時間打印到控制臺。 ```c C:\Users\Jano\Documents\Pelles C Projects\timedate\LocalTimeToUniversalTime>LocalTimeToUniversalTime.exe Date: 2/1/2016 The local time is: 11:39:48 The universal time is: 10:39:48 ``` 在 2016 年 2 月 1 日的 CEST 時區,我們得到了上述輸出。 在 Windows API 教程的這一部分中,我們使用日期&時間。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看