20210515 first

C语言获取系统时间

 	在学习I/O文件操作时遇到了关于时间函数的问题,在这里进行记录,有误之处
 	请指出!第一次发博客,多多担待嘿嘿
  • 包含头文件 : #include <time.h>
  • 创建 time_t类型 变量 ,此类型为types.h中定义的long int 别名
  • 创建 struct tm 结构体指针 ,该结构体包含时间信息,例如:秒 分 时 日 月 年….
struct tm
{
  int tm_sec;			/* Seconds.	[0-60] (1 leap second) 获取当前秒 */
  int tm_min;			/* Minutes.	[0-59] 获取当前分 */
  int tm_hour;			/* Hours.	[0-23] 获取当前时 这里获取西方的时间 刚好相差8小时 */
  int tm_mday;			/* Day.		[1-31] 获取当前日数 */
  int tm_mon;			/* Month.	[0-11] 获取当前月数*/
  int tm_year;			/* Year	- 1900.        获取当前年数 从1900开始*/
  int tm_wday;			/* Day of week.	[0-6]  星期-取值区间 0为星期天 1为星期一*/
  int tm_yday;			/* Days in year.[0-365]	从今年1月1日开始至今的天数,范围0-365*/
  int tm_isdst;			/* DST.		[-1/0/1] 夏令时标记 夏令时->-1 非->0 不确定-> -1 */

# ifdef	__USE_MISC
  long int tm_gmtoff;		/* Seconds east of UTC.  */
  const char *tm_zone;		/* Timezone abbreviation.  */
# else
  long int __tm_gmtoff;		/* Seconds east of UTC.  */
  const char *__tm_zone;	/* Timezone abbreviation.  */
# endif
};
  • 通过time函数获取linux(…)系统时间,单位为秒,将刚刚定义的time_t类型的变量取地址作为参数
 time_t timep;   
 struct tm *p; 
 time(&timep);
  • 此时timep已经拥有了从1970年1月1日开始的秒值,取其地址作为参数调用gmtime()函数 此函数将秒值转换为结构体成员值
 p = gmtime(&timep);
  • 此时的时间可通过调用tm结构体成员来显示查看 注意:这里时间是UTC时间 ,未将其转换为当地时间 故这里将其+8
    注意:这里的星期 数组 其元素位置必须遵守tm结构体中p->tm_wday值 其0代表周日 1代表周一
char *wday[]={"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
 printf("%d年 %d月 %d日 %s %d:%d:%d\n",(1900+p->tm_year),
        (1+p->tm_mon), p->tm_mday,wday[p->tm_wday], p->tm_hour+8, p->tm_min, p->tm_sec);
  • 整体实现
#include<stdio.h>
#include<time.h>

int main(int argc, char const *argv[])
{
    time_t timep;
    struct tm *p;
    time(&timep);
    p=gmtime(&timep);

    char *wday[]={"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    printf("%d年 %d月 %d日 %s %d:%d:%d\n",(1900+p->tm_year),
        (1+p->tm_mon), p->tm_mday,wday[p->tm_wday], p->tm_hour+8, p->tm_min, p->tm_sec);

    return 0;
}
输出:2021515日 周六 10:18:58
  • 练习:
    1.c语言获取系统时间在文件IO中经常用到,那么接下来编写一个程序,使之每隔1秒保存一次当前系统的时间和行号,保存到一个普通文件 time.txt中。
    要求在按 ctrl+c 终止该程序后,文件 time.txt 能正常保存到所有的信息,并且在重启程序能继续保存时间,
    并且保持行号连续
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<unistd.h>

int main(int argc, char const *argv[])
{
    
    char TimeArr[56]={0};   //储存时间信息组成的字符串
    int line=0;             //行号
    FILE *localFile=fopen("time.txt","a+"); //以可读可写并创建追加的模式打开文件
    
    while(1){
        int ret=fscanf(localFile,"%d%[^\n]",&line,TimeArr);  //读取行号 移动读写的起始位置 一遍后续的追加写入

        if(ret != 2)          //如果读取的正确个数不足2 说明到达结尾   退出读取循环                                                                                                                        
        {
            break;
        }
        
    }
    printf("开始写入!!!\n");

    
    while(1){
        //星期 数组 根据tm结构体中提供的tm_wday成员 获取其星期 下标 其数组的元素顺序需遵循tm结构体而定
        char *wday[]={"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; 
        time_t timep;   //time_t是一个long int类型 用于储存系统时间(初始值以秒为单位,从1970年1月1号开始的时间) 
        struct tm *p;   //这是时间结构体 包含秒 分 时 天...
        time(&timep);   //获取时间戳 也就是获取linux系统中时间 单位为秒 储存至长整型timep中
        
        p = gmtime(&timep); //将以秒为单位的时间分解为结构体中成员值 年 月 日 时....

        //获取结构体成员值 此时其成员已具备详细时间信息  将其时间信息组合成一个字符串形式        
        sprintf(TimeArr,"%d年 %d月 %d日 %s %d:%d:%d\n",(1900+p->tm_year),
        (1+p->tm_mon), p->tm_mday,wday[p->tm_wday], p->tm_hour+8, p->tm_min, p->tm_sec);
        
        //将line行号及时间字符串 输出到参数1关联的文件中
        fprintf(localFile,"%d\t%s",line+1,TimeArr);

        fflush(localFile);  //强制写入  每次往缓存区写入一条数据将强制将缓存区的数据存入文件
       
        sleep(1);   //一秒写一次
        line++;
        
    }
    
    
    fclose(localFile); //关闭文件
   
    return 0;
}

  • 输出:
1	2021515日 周六 10:46:52
2	2021515日 周六 10:46:53
3	2021515日 周六 10:46:54
4	2021515日 周六 10:46:55
5	2021515日 周六 10:46:56
6	2021515日 周六 10:46:57
7	2021515日 周六 10:46:58
8	2021515日 周六 10:46:59
9	2021515日 周六 10:47:0
10	2021515日 周六 10:47:2
11	2021515日 周六 10:47:3
12	2021515日 周六 10:47:4
13	2021515日 周六 10:47:5
14	2021515日 周六 10:47:6
15	2021515日 周六 10:47:7
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值