为了账号安全,请及时绑定邮箱和手机立即绑定

获取时间的C C++函数?

获取时间的C C++函数?

aluckdog 2019-02-04 10:06:17
获取时间的C C++函数
查看完整描述

3 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

1、C语言中读取系统时间的函数为time(),其函数原型为:#include <time.h>time_t time( time_t * ) ;time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。2、C语言还提供了将秒数转换成相应的时间格式的函数:char * ctime(const time_t *timer); //将日历时间转换成本地时间,返回转换后的字符串指针 可定义字符串或是字符指针来接收返回值struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间),返回结构体指针 可定义struct tm *变量来接收结果struct tm * localtime(const time_t * timer); //将日历时间转化为本地时间,返回结构体指针 可定义struct tm *变量来接收结果3、例程:

12345678910111213141516171819202122#include <time.h>void main(){time_t t;struct tm *pt ;char *pc ;time(&t);pc=ctime(&t) ; printf("ctime:%s", pc );pt=localtime(&t) ; printf("year=%d", pt->tm_year+1900 );}//时间结构体struct tm 说明:struct tm {int tm_sec; /* 秒 – 取值区间为[0,59] */int tm_min; /* 分 - 取值区间为[0,59] */int tm_hour; /* 时 - 取值区间为[0,23] */int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */int tm_year; /* 年份,其值等于实际年份减去1900 */int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/};


查看完整回答
反对 回复 2019-03-22
?
慕码人8056858

TA贡献1803条经验 获得超6个赞

常用的与时间有关的Win32 API有两个:GetSystemTime(); 与 SetSystemTime(); 下面是函数原型

VOID GetSystemTime(LPSYSTEMTIME lpSystemTime);
BOOL SetSystemTime( const SYSTEMTIME *lpSystemTime );
我们查一下 MSDN 看看 LPSYSTEMTIME 与 SYSTEMTIME 是什么:

typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;

void main()
{
SYSTEMTIME st;
GetSystemTime(&st); // Win32 API 获取系统当前时间,并存入结构体st中

......下面 你自己处理。
}



查看完整回答
反对 回复 2019-03-22
?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

/* asctime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "The current date/time is: %s", asctime (timeinfo) );

return 0;
}

/* localtime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );

return 0;
}

/* ctime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;

time ( &rawtime );
printf ( "The current local time is: %s", ctime (&rawtime) );

return 0;
}

/* gmtime example */
#include <stdio.h>
#include <time.h>

#define MST (-7)
#define UTC (0)
#define CCT (+8)

int main ()
{
time_t rawtime;
tm * ptm;

time ( &rawtime );

ptm = gmtime ( &rawtime );

puts ("Current time around the World:");
printf ("Phoenix, AZ (U.S.) : %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
printf ("Beijing (China) : %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);

return 0;
}



查看完整回答
反对 回复 2019-03-22
  • 3 回答
  • 0 关注
  • 664 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信