linux系統時間程式設計(6) 日曆時間tm轉字串strftime函式
<time.h> | |
---|---|
size_t strftime( char * str, size_t count, const char * format, const struct tm * time ); | (until C99) |
size_t strftime( char *restrict str, size_t count, const char *restrict format, const struct tm *restrict time ); | (since C99) |
count指出了寫到str的最大長度,不包含\0,所以使用者提供的str的長度必須大於等於count+1。
format用於指定輸出的格式,這個格式非常豐富,見下表:
%+下面的字母代表一段字串。
Conversion specifier | Explanation | Used fields |
---|---|---|
% | writes literal % . The full conversion specification must be %% . | |
n (C99) | writes newline character | |
t (C99) | writes horizontal tab character | |
Year | ||
Y | writes year as a decimal number, e.g. 2017 | **tm_year** |
EY (C99) | writes year in the alternative representation, e.g.平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale | **tm_year** |
y | writes last 2 digits of year as a decimal number (range [00,99] ) | **tm_year** |
Oy (C99) | writes last 2 digits of year using the alternative numeric system, e.g. 十一 instead of 11 in ja_JP locale | **tm_year** |
Ey (C99) | writes year as offset from locale’s alternative calendar period %EC (locale-dependent) | **tm_year** |
C (C99) | writes first 2 digits of year as a decimal number (range [00,99] ) | **tm_year** |
EC (C99) | writes name of the base year (period) in the locale’s alternative representation, e.g. 平成 (Heisei era) in ja_JP | **tm_year** |
G (C99) | writes ISO 8601 week-based year, i.e. the year that contains the specified week.In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:Includes January 4Includes first Thursday of the year | **tm_year** , **tm_wday** , **tm_yday** |
g (C99) | writes last 2 digits of ISO 8601 week-based year, i.e. the year that contains the specified week (range [00,99] ).In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:Includes January 4Includes first Thursday of the year | **tm_year** , **tm_wday** , **tm_yday** |
Month | ||
b | writes abbreviated month name, e.g. Oct (locale dependent) | **tm_mon** |
Ob (C23) | writes abbreviated month name in the locale’s alternative representation | **tm_mon** |
h (C99) | synonym of b | **tm_mon** |
B | writes full month name, e.g. October (locale dependent) | **tm_mon** |
OB (C23) | writes appropriate full month name in the locale’s alternative representation | **tm_mon** |
m | writes month as a decimal number (range [01,12] ) | **tm_mon** |
Om (C99) | writes month using the alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale | **tm_mon** |
Week | ||
U | writes week of the year as a decimal number (Sunday is the first day of the week) (range [00,53] ) | **tm_year** , **tm_wday** , **tm_yday** |
OU (C99) | writes week of the year, as by %U , using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale | **tm_year** , **tm_wday** , **tm_yday** |
W | writes week of the year as a decimal number (Monday is the first day of the week) (range [00,53] ) | **tm_year** , **tm_wday** , **tm_yday** |
OW (C99) | writes week of the year, as by %W , using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale | **tm_year** , **tm_wday** , **tm_yday** |
V (C99) | writes ISO 8601 week of the year (range [01,53] ).In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:Includes January 4Includes first Thursday of the year | **tm_year** , **tm_wday** , **tm_yday** |
OV (C99) | writes week of the year, as by %V , using the alternative numeric system, e.g. 五十二 instead of 52 in ja_JP locale | **tm_year** , **tm_wday** , **tm_yday** |
Day of the year/month | ||
j | writes day of the year as a decimal number (range [001,366] ) | **tm_yday** |
d | writes day of the month as a decimal number (range [01,31] ) | **tm_mday** |
Od (C99) | writes zero-based day of the month using the alternative numeric system, e.g 二十七 instead of 27 in ja_JP localeSingle character is preceded by a space. | **tm_mday** |
e (C99) | writes day of the month as a decimal number (range [1,31] ).Single digit is preceded by a space. | **tm_mday** |
Oe (C99) | writes one-based day of the month using the alternative numeric system, e.g. 二十七 instead of 27 in ja_JP localeSingle character is preceded by a space. | **tm_mday** |
Day of the week | ||
a | writes abbreviated weekday name, e.g. Fri (locale dependent) | **tm_wday** |
A | writes full weekday name, e.g. Friday (locale dependent) | **tm_wday** |
w | writes weekday as a decimal number, where Sunday is 0 (range [0-6] ) | **tm_wday** |
Ow (C99) | writes weekday, where Sunday is 0 , using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale | **tm_wday** |
u (C99) | writes weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range [1-7] ) | **tm_wday** |
Ou (C99) | writes weekday, where Monday is 1 , using the alternative numeric system, e.g. 二 instead of 2 in ja_JP locale | **tm_wday** |
Hour, minute, second | ||
H | writes hour as a decimal number, 24 hour clock (range [00-23] ) | **tm_hour** |
OH (C99) | writes hour from 24-hour clock using the alternative numeric system, e.g. 十八 instead of 18 in ja_JP locale | **tm_hour** |
I | writes hour as a decimal number, 12 hour clock (range [01,12] ) | **tm_hour** |
OI (C99) | writes hour from 12-hour clock using the alternative numeric system, e.g. 六 instead of 06 in ja_JP locale | **tm_hour** |
M | writes minute as a decimal number (range [00,59] ) | **tm_min** |
OM (C99) | writes minute using the alternative numeric system, e.g. 二十五 instead of 25 in ja_JP locale | **tm_min** |
S | writes second as a decimal number (range [00,60] ) | **tm_sec** |
OS (C99) | writes second using the alternative numeric system, e.g. 二十四 instead of 24 in ja_JP locale | **tm_sec** |
Other | ||
c | writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (locale dependent) | all |
Ec (C99) | writes alternative date and time string, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale | all |
x | writes localized date representation (locale dependent) | all |
Ex (C99) | writes alternative date representation, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale | all |
X | writes localized time representation, e.g. 18:40:20 or 6:40:20 PM (locale dependent) | all |
EX (C99) | writes alternative time representation (locale dependent) | all |
D (C99) | equivalent to "%m/%d/%y" | **tm_mon** , **tm_mday** , **tm_year** |
F (C99) | equivalent to “%Y-%m-%d” (the ISO 8601 date format) | **tm_mon** , **tm_mday** , **tm_year** |
r (C99) | writes localized 12-hour clock time (locale dependent) | **tm_hour** , **tm_min** , **tm_sec** |
R (C99) | equivalent to "%H:%M" | **tm_hour** , **tm_min** |
T (C99) | equivalent to “%H:%M:%S” (the ISO 8601 time format) | **tm_hour** , **tm_min** , **tm_sec** |
p | writes localized a.m. or p.m. (locale dependent) | **tm_hour** |
z (C99) | writes offset from UTC in the ISO 8601 format (e.g. -0430 ), or no characters if the time zone information is not available | **tm_isdst** |
Z | writes locale-dependent time zone name or abbreviation, or no characters if the time zone information is not available | **tm_isdst** |
返回值
如果產生的 C 字串小於count個字元(不包括空結束字元),則會返回複製到 str 中的字元總數(不包括空結束字元),否則返回零。
例子
#include <stdio.h>
#include <time.h>
#include <locale.h>
int main(void)
{
char buff[70];
struct tm my_time = { .tm_year=112, // = year 2012
.tm_mon=9, // = 10th month
.tm_mday=9, // = 9th day
.tm_hour=8, // = 8 hours
.tm_min=10, // = 10 minutes
.tm_sec=20 // = 20 secs
};
if (strftime(buff, sizeof buff, "%A %c", &my_time)) {
puts(buff);
} else {
puts("strftime failed");
}
setlocale(LC_TIME, "el_GR.utf8");
if (strftime(buff, sizeof buff, "%A %c", &my_time)) {
puts(buff);
} else {
puts("strftime failed");
}
}
Possible output:
Sunday Sun Oct 9 08:10:20 2012
Κυριακή Κυρ 09 Οκτ 2012 08:10:20 πμ EST
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm *info;
char buffer[80];
time( &rawtime );
info = localtime( &rawtime );
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
printf("格式化的日期 & 時間 : |%s|\n", buffer );
return(0);
}
讓我們編譯並執行上面的程式,這將產生以下結果:
格式化的日期 & 時間 : |2018-09-19 08:59:07|
相關文章
- linux系統時間程式設計(8) UTC秒數轉本地字串時間Linux程式設計字串
- linux系統時間程式設計(9) 計算程式片段執行時間clock函式Linux程式設計函式
- C時間函式strftime、struct timespec 和 struct timeval函式Struct
- Linux時間設定系統時間、硬體時間和時間服務Linux
- linux系統時間設定Linux
- Linux設定系統時間Linux
- mysql時間操作(時間差和時間戳和時間字串的互轉)MySql時間戳字串
- mysql 時間相關的函式 以及日期和字串互轉MySql函式字串
- 時間戳與時間字串的多時區轉換時間戳字串
- PHP 時間函式PHP函式
- 直播系統程式碼,linux date修改系統時間Linux
- Linux系統如何更改時間時區Linux
- Golang時間函式及測試函式執行時間案例Golang函式
- Linux系統時間同步方法。Linux
- linux時間子系統(三)Linux
- 時間函式:與時間相關那些事。。。函式
- Oracle計算時間函式(對時間的加減numtodsinterval、numtoyminterval)Oracle函式
- python時間戳和時間字串的各種轉換Python時間戳字串
- python之為函式執行設定超時時間(允許函式執行的最大時間)Python函式
- mysql時間與字串之間相互轉換MySql字串
- Clickhouse 時間日期函式函式
- win10系統如何設定時間日期顯示農曆Win10
- Linux系統自動更新時間Linux
- postgresql如何將字串轉為時間SQL字串
- python字串轉換為日期時間Python字串
- MySQL字串轉時間戳查詢MySql字串時間戳
- 使用ASM框架實現統計函式執行時間ASM框架函式
- PHP的時間日期與例項應用:日曆核心程式PHP
- 探索MySQL高階語句(數學函式、聚合函式、字串函式、日期時間函式)MySql函式字串
- SPL 的日期時間函式函式
- T-SQL——函式——時間操作函式SQL函式
- Java程式碼實現帶時區時間字串轉為LocalDateTime物件Java字串LDA物件
- 在C程式中按照你想要的格式輸出時間:strftime()C程式
- Linux 檢視程式啟動時間、執行時間Linux
- 【Python】Python 使用http時間同步設定系統時間原始碼PythonHTTP原始碼
- mysql5.7日誌時間與系統時間不一致MySql
- ORACLE中日期和時間函式彙總(轉載)Oracle函式
- 雲伺服器Linux系統設定時間同步設定伺服器Linux