如何用C语言获取当前系统时间?620
发布网友
发布时间:2023-11-09 23:58
我来回答
共5个回答
热心网友
时间:2024-06-08 14:27
需要利用C语言的时间函数time和localtime,具体说明如下:
一、函数接口介绍:
1、time函数。
形式为time_t time (time_t *__timer);
其中time_t为time.h定义的结构体,一般为长整型。
这个函数会获取当前时间,并返回。 如果参数__timer非空,会存储相同值到__timer指向的内存中。
time函数返回的为unix时间戳,即从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。
由于是秒作为单位的,所以这并不是习惯上的时间,要转为习惯上的年月日时间形式就需要另外一个函数了。
2、localtime函数。
形式为struct tm *localtime (const time_t *__timer);
其中tm为一个结构体,包含了年月日时分秒等信息。
这种结构是适合用来输出的。
二、参考代码:
#include <stdio.h>
#include <time.h>
int main ()
{
time_t t;
struct tm * lt;
time (&t);//获取Unix时间戳。
lt = localtime (&t);//转为时间结构。
printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果
return 0;
}
注意事项:
struct tm中的tm_year 值为实际年减去1900, 所以输出的时候要是lt->tm_year+1900。
热心网友
时间:2024-06-08 14:27
这是一个获取时间的,并且写入文件的函数。你琢磨下吧。
void
time()
{
file
*tp;
tp=fopen("系统使用记录.txt","a");
time_t
t;
//struct
tm
*gmt,
*area;
struct
tm
*area;
t
=
time(null);
area
=
localtime(&t);
printf("当前系统时间:
%s",
asctime(area));
fprintf(tp,"
%s",asctime(area));
fclose(tp);
//gmt
=
gmtime(&t);
//printf("gmt
is:
%s",
asctime(gmt));
}
热心网友
时间:2024-06-08 14:28
一般使用函数"
text="点击实体词"
target="_blank"
href="http://www.haosou.com/s?q=time%E5%87%BD%E6%95%B0&ie=utf-8&src=wenda_link">time函数,Windows下可以使用GetTickCount或timeGetTime函数获取系统时间。
热心网友
时间:2024-06-08 14:28
Example
/* TIMES.C illustrates various time and date functions including:
* time _ftime ctime asctime
* localtime gmtime mktime _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();
/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n",
asctime( today ) + 11, ampm );
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:\t%u\n",
tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
printf( "Daylight savings:\t\t\t%s\n",
tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}
Output
OS time: 21:51:03
OS date: 05/03/94
Time in seconds since UTC 1/1/70: 768027063
UNIX time and date: Tue May 03 21:51:03 1994
Coordinated universal time: Wed May 04 04:51:03 1994
12-hour time: 09:51:03 PM
Plus milliseconds: 279
Zone difference in seconds from UTC: 480
Time zone name:
Daylight savings: YES
Christmas Sat Dec 25 12:00:00 1993
Today is Tuesday, day 03 of May in the year 1994.
热心网友
时间:2024-06-08 14:29
lt->tm_year+1900, lt->tm_mon+1