linux中时间的获取

本文详细介绍了time.h中的time_t与tm结构体,并通过示例代码展示了如何获取当前时间并将其格式化为可读形式。对于理解C/C++中时间处理的基本概念至关重要。

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

time_t是一个在time.h中定义好的结构体。原形如下:

  struct   tm

  {

  int   tm_sec;//seconds   0-61

  int   tm_min;//minutes   1-59

  int   tm_hour;//hours   0-23

  int   tm_mday;//day   of   the   month   1-31

  int   tm_mon;//months   since   jan   0-11

  int   tm_year;//years   from   1900

  int   tm_wday;//days   since   Sunday,   0-6

  int   tm_yday;//days   since   Jan   1,   0-365

  int   tm_isdst;//Daylight   Saving   time   indicator

  };

void mygettime()
{
	time_t t;
    struct tm *tm_t;
     
    time(&t);
    tm_t=localtime(&t);
    printf("the current time is:%02d:%02d:%02d\n",tm_t->tm_hour,tm_t->tm_min,tm_t->tm_sec);
    printf("the current day is:%4d:%02d:%02d\n",
	printf("the current day is:%d:%2d:%2d\n",
    tm_t->tm_year+1900,tm_t->tm_mon+1,tm_t->tm_mday); //这边得自己手动加个1990,和加个 1,因为。。。见定义。。。
		
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值