例181:获取详细日期时间并输出
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
struct tm *gm_date;
time_t seconds;
// 获取秒
time(&seconds);
// 将秒转为日期 - gmtime函数转换后的时间没有经过时区变换,是UTC时间
gm_date = gmtime(&seconds);
printf("Current date: %d-%d-%d\n", gm_date->tm_mon+1, gm_date->tm_mday, gm_date->tm_year);
printf("Current time: %02d:%02d\n", gm_date->tm_hour, gm_date->tm_min);
return 0;
}
例182:指定日期来输出
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
time_t seconds;
struct tm time_fields;
time_fields.tm_mday = 4;
time_fields.tm_mon = 7;
time_fields.tm_year = 94;
// 根据结构体中的属性值获取秒数
if (mktime(&time_fields) == -1)
printf("Error converting fields\n");
else
printf("Julian date for July 4, 1994 is %d\n", time_fields.tm_yday);
return 0;
}
例183:获取详细日期时间并输出(转换时区后)
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
struct tm *current_date;
time_t seconds;
// 获取秒
time(&seconds);
// 将秒转为日期 - localtime是时区转换后的结果
current_date = localtime(&seconds);
printf("Current date: %d-%d-%d\n", current_date->tm_mon+1, current_date->tm_mday, current_date->tm_year);
printf("Current time: %02d:%02d\n", current_date->tm_hour, current_date->tm_min);
return 0;
}
例184:计算1970年1月1日到指定时时间的秒数
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
time_t seconds;
struct tm time_fields;
time_fields.tm_mday = 4;
time_fields.tm_mon = 7;
time_fields.tm_year = 94;
time_fields.tm_hour = 0;
time_fields.tm_min = 0;
time_fields.tm_sec = 0;
seconds = mktime(&time_fields);
printf("The number of seconds between 7-4-94 and 1-1-70 is %ld\n",seconds);
return 0;
}
例185:设置时区并输出时区
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char const *argv[])
{
putenv("TZ=PST8PDT");
tzset();
printf("Current time zone is %s\n", tzname[0]);
if (tzname[1])
printf("Daylight savings zone is %s\n", tzname[1]);
else
printf("Daylight savings zone is not defined\n");
return 0;
}
例186:设置DOS日期
#include <stdio.h>
#include <dos.h>
int main(int argc, char const *argv[])
{
struct date desired_date;
desired_date.da_mon = 12;
desired_date.da_day = 8;
desired_date.da_year = 1993;
setdate(&desired_date);
return 0;
}
例187:设置DOS时间
#include <stdio.h>
#include <dos.h>
int main(int argc, char const *argv[])
{
struct time desired_time;
desired_time.ti_hour = 12;
desired_time.ti_min = 30;
settime(&desired_time);
return 0;
}
例188:设置时间加快1天
#include <time.h>
int main(int argc, char const *argv[])
{
time_t seconds;
time(&seconds);
seconds += (time_t) 60 * 60 * 24;
stime(&seconds);
return 0;
}
例189:输出当前系统日期
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
char date[9];
// 获取当前系统日期(不包括时间),函数以字符指针形式为返回
_strdate(date);
printf("The current date is %s\n", date);
return 0;
}
例190:strftime格式化时间:
#include <stdio.h>
#include <time.h>
/**
* strftime格式
%a 星期几的简写
%A 星期几的全称
%b 月份的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的前两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年份,使用基于周的年
%h 简写的月份名
%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u 每周的第几天,星期一为第一天 (值从1到7,星期一为1)
%U 第年的第几周,把星期日作为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
*/
int main(int argc, char const *argv[])
{
char buffer[128];
struct tm *datetime;
time_t current_time;
tzset();
time(¤t_time);
datetime = localtime(¤t_time);
strftime(buffer, sizeof(buffer), "%x %X", datetime);
printf("Using %%x %%X: %s\n", buffer);
strftime(buffer, sizeof(buffer), "%A %B %m, %Y", datetime);
printf("Using %%A %%B %%m %%Y: %s\n", buffer);
strftime(buffer, sizeof(buffer), "%I:%M%p", datetime);
printf("Using %%I:%%M%%p: %s\n", buffer);
return 0;
}
例191:输出系统时间
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
char time[9];
_strtime(time);
printf("The current time is %s\n", time);
return 0;
}
例192:计算时区时差
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
tzset();
printf("Difference between local and GMT is %d hours\n", timezone / 3600);
return 0;
}
例193:查看时区
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
tzset();
printf("Current time zone is %s\n", tzname[0]);
if (tzname[1])
printf("Daylight savings zone is %s\n", tzname[1]);
else
printf("Daylight savings zone is not defined\n");
return 0;
}
例194:延迟函数
#include <stdio.h>
#include <dos.h>
int main(int argc, char const *argv[])
{
printf("About to delay 5 seconds\n");
delay(5000);
printf("Done\n");
return 0;
}