time_t实际是一个整数,它是怎样表示年月日以及时间的?

yjinf 2005-11-16 09:04:12
我想了解怎样通过计算得到年月日以及时间。
...全文
7998 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2010-06-21
  • 打赏
  • 举报
回复
05年的帖子又给人挖出来了。。。
用户 昵称 2010-06-21
  • 打赏
  • 举报
回复
年月日时分秒都是整数。
Eleven 2010-06-21
  • 打赏
  • 举报
回复
囧。。。又见05年的帖子。。。
wolffan3150 2010-06-21
  • 打赏
  • 举报
回复
msdn........

struct tm *localtime( const time_t *timer );

#include <stdio.h>
#include <string.h>
#include <time.h>

void main( void )
{
struct tm *newtime;
char am_pm[] = "AM ";
time_t long_time;

time( &long_time ); /* Get time as long integer. */
newtime = localtime( &long_time ); /* Convert to local time. */

if( newtime-> tm_hour > 12 ) /* Set up extension. */
strcpy( am_pm, "PM " );
if( newtime-> tm_hour > 12 ) /* Convert from 24-hour */
newtime-> tm_hour -= 12; /* to 12-hour clock. */
if( newtime-> tm_hour == 0 ) /*Set hour to 12 if midnight. */
newtime-> tm_hour = 12;

printf( "%.19s %s\n ", asctime( newtime ), am_pm );
}


Output

Tue Mar 23 11:28:17 AM
phonelee 2010-06-21
  • 打赏
  • 举报
回复
原来如此
j8daxue 2010-06-10
  • 打赏
  • 举报
回复
挖坟啊。。。。
lishuo512 2010-06-10
  • 打赏
  • 举报
回复
貌似2楼上解释的?
chenyulin 2005-11-16
  • 打赏
  • 举报
回复
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.


thisisll 2005-11-16
  • 打赏
  • 举报
回复
linux常用函数里有讲

定义函数 time_t mktime(strcut tm * timeptr);

函数说明 mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。

返回值 返回经过的秒数。

定义函数 time_t time(time_t *t);

函数说明 此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。

返回值 成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。



我想在windows下应该一样
其他相关内容在
http://ldfren.bosinet.com/download/linux_c/index.htm
这里的 日期时间篇 里有讲
Kudeet 2005-11-16
  • 打赏
  • 举报
回复
You can use localtime. It Converts a time value and corrects for the local time

struct tm *localtime( const time_t *timer );

#include <stdio.h>
#include <string.h>
#include <time.h>

void main( void )
{
struct tm *newtime;
char am_pm[] = "AM";
time_t long_time;

time( &long_time ); /* Get time as long integer. */
newtime = localtime( &long_time ); /* Convert to local time. */

if( newtime->tm_hour > 12 ) /* Set up extension. */
strcpy( am_pm, "PM" );
if( newtime->tm_hour > 12 ) /* Convert from 24-hour */
newtime->tm_hour -= 12; /* to 12-hour clock. */
if( newtime->tm_hour == 0 ) /*Set hour to 12 if midnight. */
newtime->tm_hour = 12;

printf( "%.19s %s\n", asctime( newtime ), am_pm );
}


Output

Tue Mar 23 11:28:17 AM



16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧