C语言如何获取本地时间,然后取时、分、秒的值

C语言如何获取本地时间,然后取时、分、秒的值,第1张

C语言有2个获取时间的函数,分别是time()和localtime(),time()函数返回unix时间戳-即从1970年1月1日0:00开始所经过得秒数,而localtime()函数则是将这个秒数转化为当地的具体时间(年月日时分秒)

这里时间转化要用到一个“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日 /

int tm_isdst;    / 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0 / 

};

示例代码:

#include<stdioh>

#include<timeh>

int getTime()

{

time_t t;                   //保存unix时间戳的变量 ,长整型

struct tm lt;           //保存当地具体时间的变量

int p;                     

time(&t);                // 等价于 t =time(NULL);获取时间戳

lt = localtime(&t);  //转化为当地时间

p = lt->tm_sec;     //将秒数赋值给p

return p;

}

应该就是这样啦~

#include <stdioh>

#include <timeh> 

int main()

time_t rawtime; 

struct tm  timeinfo; 

time ( &rawtime ); 

timeinfo = localtime ( &rawtime ); 

printf ( "当前系统时间: %s", asctime (timeinfo) ); 

return 0;

}

说明:

time_t // 时间类型(timeh 定义) 

struct tm { // 时间结构,timeh 定义如下: 

int tm_sec; 

int tm_min; 

int tm_hour; 

int tm_mday; 

int tm_mon; 

int tm_year; 

int tm_wday; 

int tm_yday; 

int tm_isdst; 

time ( &rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime 

localtime ( &rawtime ); //转为当地时间,tm 时间结构 

asctime() // 转为标准ASCII时间格式: 

//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

要包含头文件 timeh

//获取并显示日期、时间

time_t nowTime;

time(&nowTime);

struct tm sysTime=localtime(&nowTime);

cout<<"\t现在是"<<1900+sysTime->tm_year<<"年"<<sysTime->tm_mon+1<<"月"<<sysTime->tm_mday<<"日 星期";

switch(sysTime->tm_wday)

{

case 1: cout<<"一\t";break;

case 2: cout<<"二\t";break;

case 3: cout<<"三\t";break;

case 4: cout<<"四\t";break;

case 5: cout<<"五\t";break;

case 6: cout<<"六\t";break;

case 7: cout<<"日\t";break;

}

cout<<sysTime->tm_hour<<":"<<sysTime->tm_min<<":"<<sysTime->tm_sec;

//时间显示完毕

程序主要通过当前系统日历的struct tm结构体获得,主要代码如下,\x0d\#include \x0d\#include \x0d\//程序功能输出当前时间在24H下的小时数 \x0d\int main(int argc, char argv[])\x0d\{\x0d\struct tm ptr;\x0d\time_t lt;\x0d\time(&lt);//当前系统时间 \x0d\ptr=localtime(&lt);//获取本地日历时间指针 \x0d\printf("hour=%d(24H )\n",ptr->tm_hour);//输出24H下的小时数 \x0d\return 0;\x0d\}\x0d\\x0d\结构体tm定义如下,\x0d\struct tm {\x0d\int tm_sec; / 秒_取值区间为[0,59] /\x0d\int tm_min; / 分 - 取值区间为[0,59] /\x0d\int tm_hour; / 时 - 取值区间为[0,23] /\x0d\int tm_mday; / 一个月中的日期 - 取值区间为[1,31] /\x0d\int tm_mon; / 月份(从一月开始,0代表一月) - 取值区间为[0,11] /\x0d\int tm_year; / 年份,其值从1900开始 /\x0d\int tm_wday; / 星期_取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 /\x0d\int tm_yday; / 从每年的1月1日开始的天数_取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 /\x0d\int tm_isdst; / 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。/\x0d\long int tm_gmtoff; /指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数/\x0d\const char tm_zone; /当前时区的名字(与环境变量TZ有关)/\x0d\};

给出日期,用timeh里的函数,可以获得“时间结构”,结构里有星期几的参数。

其他的算法,请去我的百度空间,看我的文章:C语言时间函数的应用。

有详细程序和说明。

(4)输入年月日计算这天是星期几

公元计年从1年1月1日开始,这天是星期一。平年一年有365天,365除7取余数为1。也就是说平年的星期几等于上一年的星期几加1。闰年的星期几等于上一年的星期几加2。

所以,若知年份,就可以推算出这年元旦推移了多少天,变星期几,再调用YMD_2_JD(),算出某月某日推移了多少天,就算得这天是星期几。

(1)

time_t time ( time_t  timer );

The function returns this value, and if the argument is not a null pointer,

the value is also set to the object pointed by timer

(2)

struct tm  localtime ( const time_t  timer );

Uses the time pointed by timer to fill a tm

structure with the values that represent the corresponding local time

(3)

struct tm {

        int tm_sec;     / seconds after the minute - [0,59] /

        int tm_min;     / minutes after the hour - [0,59] /

        int tm_hour;    / hours since midnight - [0,23] /

        int tm_mday;    / day of the month - [1,31] /

        int tm_mon;     / months since January - [0,11] /

        int tm_year;    / years since 1900 /

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

        int tm_yday;    / days since January 1 - [0,365] /

        int tm_isdst;   / daylight savings time flag /

        };

( 4 )

int atoi(  const char str );

Convert a string to integer

你代码中的if语句就是拿当前时间records中第i个时间比较,如果当前时间的小时不大于records[i]的小时,且分钟小于records[i]中的分钟,则返回1(应该是没超出),否则返回0(超出)。

懂了吗,宝贝?

什么意思?就是输出计算机编译程序时候的时间吗?

printf(__DATA__);

printf(__TIME__);

不用运算 系统有宏定义

void main()

{

printf(__DATA__);

printf(__TIME__);

getch();

}

以上就是关于C语言如何获取本地时间,然后取时、分、秒的值全部的内容,包括:C语言如何获取本地时间,然后取时、分、秒的值、用c语言获取时间、求救!!C语言 读取当天日期、时间的函数(VC的编译环境)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10137409.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存