- C 標準庫
- C 庫 - 首頁
- C 庫 - <assert.h>
- C 庫 - <complex.h>
- C 庫 - <ctype.h>
- C 庫 - <errno.h>
- C 庫 - <fenv.h>
- C 庫 - <float.h>
- C 庫 - <inttypes.h>
- C 庫 - <iso646.h>
- C 庫 - <limits.h>
- C 庫 - <locale.h>
- C 庫 - <math.h>
- C 庫 - <setjmp.h>
- C 庫 - <signal.h>
- C 庫 - <stdalign.h>
- C 庫 - <stdarg.h>
- C 庫 - <stdbool.h>
- C 庫 - <stddef.h>
- C 庫 - <stdio.h>
- C 庫 - <stdlib.h>
- C 庫 - <string.h>
- C 庫 - <tgmath.h>
- C 庫 - <time.h>
- C 庫 - <wctype.h>
- C 標準庫資源
- C 庫 - 快速指南
- C 庫 - 有用資源
- C 庫 - 討論
- C 程式設計資源
- C 程式設計 - 教程
- C - 有用資源
C 庫 - wcsftime() 函式
C 庫的 wcsftime() 函式根據指定的格式將日期和時間資訊轉換為寬字元字串。在列印日期時,它使用 wprintf() 函式。此函式用於使用寬字元進行格式化輸出。它允許使用者將一系列寬字元和值列印到標準輸出。
語法
以下是 C 庫 wcsftime() 函式的語法 -
wcsftime(buffer, int_value, L"The current Date: %A, %F %I:%M %p", timeinfo); and wprintf(L"%ls\n", buffer);
引數
此函式接受以下引數 -
1. buffer:指向輸出將儲存到的寬字元陣列的第一個元素的指標。
2. int_valuie:要寫入的最大寬字元數(緩衝區的大小)。
3. format:指向一個以 null 結尾的寬字元字串的指標,該字串指定轉換的格式,以下是日期和時間格式化中使用的常用轉換說明符列表 -
- %A:完整的工作日名稱(例如,“星期日/星期一/星期二等”)。
- %F:完整日期,格式為“YYYY-MM-DD”(例如,“2024-05-22”)。
- %H:小時(24 小時制),以十進位制數字表示(例如,“19”)。
- %M:分鐘,以十進位制數字表示(例如,“30”)。
- %I:%M %p:小時(12 小時制)和分鐘,後跟 AM/PM 指示符。
- %S:秒,以十進位制數字表示(例如,“45”)。
4. timeinfo:指向包含本地時間資訊的 struct tm 的指標。
返回型別
程式成功時,函式返回複製到字串中的寬字元總數,不包括終止 null 寬字元。如果輸出超過最大大小,則函式返回零。
示例 1
以下是使用 wcsftime() 函式顯示當前日期和時間並使用自定義格式的基本 C 庫程式。
#include <stdio.h>
#include <wchar.h>
#include <time.h>
int main() {
time_t demotime;
struct tm* timeinfo;
wchar_t buffer[80];
time(&demotime);
timeinfo = localtime(&demotime);
// Custom formatting of time
wcsftime(buffer, 80, L"The current Date: %A, %F %I:%M %p", timeinfo);
wprintf(L"%ls\n", buffer);
return 0;
}
輸出
執行上述程式碼後,我們將獲得以下輸出 -
The current Date: Wednesday, 2024-05-22 01:38 PM
示例 2
以下示例使用各種函式以 24 小時制格式化本地時間。
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <time.h>
int main() {
time_t rawtime;
struct tm* timeinfo;
wchar_t buffer[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
// Set the locale category
setlocale(LC_ALL, "ja_JP.utf8");
// Custom Formatting to 24-hour clock
wcsftime(buffer, 80, L"現在の日時: %A、%F %H:%M", timeinfo);
// Display the result
wprintf(L"%ls\n", buffer);
return 0;
}
輸出
執行程式碼後,我們將獲得以下輸出 -
?????: Wednesday?2024-05-22 13:42
示例 3
要獲取今天的日期,wcsftime() 格式設定轉換說明符的自定義設定並顯示結果。
#include <stdio.h>
#include <time.h>
#include <wchar.h>
int main(void) {
struct tm* timeinfo;
wchar_t dest[100];
time_t temp;
size_t r;
temp = time(NULL);
timeinfo = localtime(&temp);
r = wcsftime(dest, sizeof(dest), L" Today is %A, %b %d.\n Time: %I:%M %p", timeinfo);
printf("%zu is the placed characters in the string:\n\n%ls\n", r, dest);
return 0;
}
輸出
以上程式碼產生以下輸出 -
44 The placed characters in the string: Today is Wednesday, May 22. Time: 02:28 PM
廣告