C语言学习:获取日历时间
时间戳表示从1970年1月1日到现在的秒数。
1 #include <io_utils.h> 2 #include <time_utils.h> 3 #include <time.h> 4 5 int main() { 6 time_t current_time; 7 time(¤t_time); 8 PRINT_LLONG(current_time); 9 struct tm *calendar_time = localtime(¤t_time); 10 PRINT_INT(calendar_time->tm_year); 11 PRINT_INT(calendar_time->tm_mon); 12 PRINT_INT(calendar_time->tm_mday); 13 PRINT_INT(calendar_time->tm_hour); 14 PRINT_INT(calendar_time->tm_min); 15 PRINT_INT(calendar_time->tm_sec); 16 17 calendar_time->tm_sec = 70; 18 19 time_t current_time2 = mktime(calendar_time); 20 PRINT_LLONG(current_time2); 21 PRINT_INT(calendar_time->tm_year); 22 PRINT_INT(calendar_time->tm_mon); 23 PRINT_INT(calendar_time->tm_mday); 24 PRINT_INT(calendar_time->tm_hour); 25 PRINT_INT(calendar_time->tm_min); 26 PRINT_INT(calendar_time->tm_sec); 27 struct tm *gmt = gmtime(¤t_time); 28 29 PRINT_INT(gmt->tm_year); 30 PRINT_INT(gmt->tm_mon); 31 PRINT_INT(gmt->tm_mday); 32 PRINT_INT(gmt->tm_hour); 33 PRINT_INT(gmt->tm_min); 34 PRINT_INT(gmt->tm_sec); 35 return 0; 36 }
三个方法
localtime: 获取本地日历时间
mktime: 将calendar类型的时间进行格式化,转换成一个time_t类型的时间戳。
gmttime: 获取格林威治日历时间