今天给各位分享windows获取系统时间c语言的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、c语言中取系统时间2、看过来,看过来 C语言获取系统时间的几种方式3、C语言怎样提取系统时间且将其用于求时间差运算4、c语言如何取得系统时间??5、C语言怎样获取系统当前的时间并把它保存到定义的变量中6、C语言获取系统时间的几种方式 !
c语言中取系统时间
主要分为两种方法:
1.这种方法比较高级
#include time.h
#include stdio.h
#include time.h
int main(int argc, char ** argv)
{
time_t temp;
struct tm *t;
time(temp);
t = localtime(temp);
printf(“当前时间是:\n%d年%d月%d日\n”, t-tm_year+1900, t-tm_mon+1 , t-tm_mday);
printf(“%d时%d分%d秒\n”, t-tm_hour, t-tm_min, t-tm_sec);
/*
t结构体内的成员变量还有以下几个:
tm_wday星期的第几天tm_yday这天是这年的第几天
*/
return 0;
}
需要注意的是tm_year返回的是1900年之后的年数,tm_mon返回的比实际月份小1(至于为什么要这样设计,我不是太清楚)
2.这种方法较为简单方便,但是同时可能会对接下来的其它操作不利。
#include time.h
#include stdio.h
int main(int argc, char ** argv)
{
time_t temp;
time(temp);
printf(“当前时间为:\n%s”, ctime(temp));
return 0;
}
看过来,看过来 C语言获取系统时间的几种方式
我们在写C语言程序的时候,有的时候会用到读取本机的时间和日期,怎么做呢?其实很简单的,下面简单说一下:
C语言中读取系统时间的函数为time(),其函数原型为:#include time.htime_t time( time_t * ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。
可以调用ctime()函数进行时间转换输出:char * ctime(const time_t *timer);
将日历时间转换成本地时间,按年月日格式,进行输出,如:Wed Sep 23 08:43:03 2015C语言还提供了将秒数转换成相应的时间结构的函数:
struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间)
struct tm * localtime(const time_t * timer); //将日历时间转为本地时间将通过time()函数返回的值,转成时间结构structtm :
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()为负。*/};
编程者可以根据程序功能的情况,灵活的进行日期的读取与输出了。
下面给出一段简单的代码:
#includetime.h
int main()
{
time_t timep;
struct tm *p;
time (timep);
p=gmtime(timep);
printf(“%d\n”,p-tm_sec); /*获取当前秒*/
printf(“%d\n”,p-tm_min); /*获取当前分*/
printf(“%d\n”,8+p-tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/
printf(“%d\n”,p-tm_mday);/*获取当前月份日数,范围是1-31*/
printf(“%d\n”,1+p-tm_mon);/*获取当前月份,范围是0-11,所以要加1*/
printf(“%d\n”,1900+p-tm_year);/*获取当前年份,从1900开始,所以要加1900*/
printf(“%d\n”,p-tm_yday); /*从今年1月1日算起至今的天数,范围为0-365*/
}
C语言怎样提取系统时间且将其用于求时间差运算
提供两种方法作为参考:第一种,使用系统函数GetSystemTime和结构体SYSTEMTIME#include windows.hSYSTEMTIME stbegin,stend在开始时间点运行 GetSystemTime(stbegin); 在结束时间点运行 GetSystemTime(stend); 这时获得起始时间和结束时间,可以进行时间差运算。typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds; } SYSTEMTIME,
*PSYSTEMTIME; 第二种,如果使用MFC或者ATL库,可以使用COleDateTime类COleDateTime stbegin,stend;开始时间:stbegin=COleDateTime.GetCurrentTime();结束时间:stend=COleDateTime.GetCurrentTime();时间差:COleDateTime stresult=stend-stend;再使用COleDateTime的format函数可以把值转换成一个字符串
c语言如何取得系统时间??
#include “time.h”
time_t time(time_t *timer);
调用后将当前系统时间与1900年1月1日相差的秒数存入到timer中,timer可看成是一个长整型数
struct tm* localtime(const time_t *timer)
将time()函数调用的结果做为参数传入到localtime()函数中就能得到当前时间和日期,注意得到的年是和1970的差值,月份是和1月的差值
struct tm是一个结构体,定义如下
struct tm
{
int tm_sec; //当前秒
int tm_min; //当前分钟
int tm_hour; //当前小时
int tm_mday; //当前在本月中的天,如11月1日,则为1
int tm_mon; //当前月,范围是0~11
int tm_year; //当前年和1900的差值,如2006年则为36
int tm_wday; //当前在本星期中的天,范围0~6
int tm_yday; //当前在本年中的天,范围0~365
int tm_isdst; //这个我也不清楚
}
求当前时间的示例
int getSystemTime()
{
time_t timer;
struct tm* t_tm;
time(timer);
t_tm = localtime(timer);
printf(“today is %4d%02d%02d%02d%02d%02d\n”, t_tm.tm_year+1900,
t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec);
return 0;
}
其他时间的函数和结构还有:
timeval结构
#include include/linux/time.h
struct timeval
{
time_t tv_sec;
susecond_t tv_usec; //当前妙内的微妙数
};
tms结构
保存着一个进程及其子进程使用的cpu时间
struct tms
{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}
timer_struct结构
#include include/linux/timer.h
struct timer_struct
{
unsigned long expires; //定时器被激活的时刻
void (*fn)(void); //定时器激活后的处理函数
}
utime函数
更改文件的存取和修改时间
int utime(const char pathname, const struct utimbuf *times) // return value 0 or -1
times 为空指针,存取和修改时间设置为当前时间
struct utimbuf
{
time_t actime;
time_t modtime;
}
C语言怎样获取系统当前的时间并把它保存到定义的变量中
C语言中读取系统时间的函数为time(),其函数原型为:
#include time.h
time_t time( time_t * ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。
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 *变量来接收结果
例:
#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()为负。*/
};
C语言获取系统时间的几种方式 !
1 使用time_t time( time_t * timer ) 精确到秒
2 使用clock_t clock() 得到的是CPU时间 精确到1/CLOCKS_PER_SEC秒
3 计算时间差使用double difftime( time_t timer1, time_t timer0 )
4 使用DWORD GetTickCount() 精确到毫秒
5 如果使用MFC的CTime类,可以用CTime::GetCurrentTime() 精确到秒
6 要获取高精度时间,可以使用
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
获取系统的计数器的频率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
获取计数器的值
然后用两次计数器的差除以Frequency就得到时间。
7 Multimedia Timer Functions
The following functions are used with multimedia timers.
timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
windows获取系统时间c语言的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、windows获取系统时间c语言的信息别忘了在本站进行查找喔。