经典编程900例(c语言)(第十六篇)

本文提供了多个C语言示例程序,展示如何进行日期和时间的获取、格式化、时区设置及时间差计算等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例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(&current_time);
    datetime = localtime(&current_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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值