雨疏风骤

编程/音乐/护肤/美食

  VC知识库BLOG :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 登录 ::
  14 随笔 :: 0 文章 :: 1179 评论 :: 9 Trackbacks
<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

留言簿(90)

随笔分类

随笔档案

文章档案

相册

走过路过不要错过^_^

搜索

最新评论

阅读排行榜

评论排行榜

C语言中对时间和日期的处理

Chuck Allison

Chuck Allison是盐湖城圣Latter Day教堂总部下耶稣教堂家族历史研究处的软件体系设计师。他拥有数学学士和数学硕士学位。他从1975年起开始编程,从1984年起他开始从事c语言的教学和开发。他目前的兴趣是面向对象的技术及其教育。他是X3J16ANSI C ++标准化委员会的一员。发送e-mailallison@decus.org,或者拨打电话到(801)240-4510均可以与他取得联系。

大部分的操作系统有办法得到当前的日期和时间。通过定义在time.h的库函数,ANSI C能以许多不同的形式得到这个信息。函数time返回一个类型为time_t的值(通常为long),该函数在运行期间对当前的日期和时间进行编码。然后你可以将这个返回值传递给其他能对该值进行解码和格式化的函数。

Listing 1中的程序使用函数timelocaltimestrftime以不同的形式输出当前的日期和时间。函数localtime把已经编码的时间解码成如下的struct

struct tm
{
   int tm_sec;     /* (0 - 61) */
   int tm_min;     /* (0 - 59) */
   int tm_hour;    /* (0 - 23) */
   int tm_mday;    /* (1 - 31) */
   int tm_mon;     /* (0 - 11) */
   int tm_year;    /* past 1900 */
   int tm_wday;    /* (0 - 6) */
   int tm_yday;    /* (0 - 365) */
   int tm_isdst;   /* daylight savings flag */
};

每次当你调用localtime的时候,它会重写一个静态的结构并返回该结构的地址(因此同一时刻在一个程序中只能取得一个这样的结构,而不能做明显的拷贝)。函数ctime返回一个指向静态字符串的指针,该字符串以标准的格式包含了完整的时间和日期。strftime根据用户的指定格式格式化字符串(例如,%A代表一周中每一天的名称)。Table 1列出了格式描述符的完整列表。

时间/日期运算

通过改变tm结构里的值,可对时间/日期进行运算。Listing 2中的程序展示了如何计算将来某天的日期和以秒为单位所计算出的程序执行时间。注意函数time的语法(参数time_t由地址传入,并非作为函数的返回值)。函数mktime改变tm结构的值,以便日期和时间在一个合适的范围内,之后day-of-week (tm_wday)和day-of-year (tm_yday)域进行相应的更新。mktimetm结构中日期和时间的值置于合适的范围之内,相应的更新day of week (tm-wday)和day of year (tm-yday)的值。这种情况发生在当一个日期超出了你的实现能够支持的范围的时候。例如,我的MS-DOS的编译器不能编码1970年1月份之前的日期。函数asctime返回tm参数所描述时间的标准字符串(因此ctime (&tval)与asctime (localtime(&tval)是相等的)。函数difftime返回用秒做单位的两个time_t的差。

如果需要处理超出系统范围的日期,或者需要计算两个日期的间隔又不是用秒来做单位,那你需要设计自己的date编码。Listing 3Listing 5中的应用程序通过使用一个简单的month-day-year结构,展示了确定两个日期间隔的年数、月份数和天数的技术。日期的相减就像你在小学里做的减法那样(例如,首先进行天数的相减,如果需要就向月份数借位,以此类推)。注意跳过的年份都被计算进去了。为了简略起见,date_interval函数假设日期都是有效的,并且第一个日期在第二个日期之前。函数返回一个指向静态Date结构的指针,该结构包含了我们想要的答案。

文件时间/日期戳

大多数操作系统为文件维护时间/日期戳。至少你能得知一个文件最后被修改的时间。(常用的make工具使用这一信息来决定一个文件是否需要被重新编译,或者一个应用程序是否需要被重新连接)。由于文件系统在不同平台上有所不同,没有什么通用的函数得到一个文件的时间/日期戳,因此ANSI 标准没有定义这样的函数。然而,大多数流行的操作系统(包括MS-DOS和VAX/VMS)提供了UNIX函数stat,该函数返回相关的文件信息,包括用time_t表示的最后修改时间。

Listing 6中的程序使用statdifftime来确定是否time1.ctime2.c更新(例如,是否最近被修改过)。

如果你需要更新一个文件的时间/日期戳到当前时间,可简单的重写文件的第一个字节。虽然实际内容并未改变,但你的文件系统会认为文件已经被改变了,并且会相应的更新时间/日期戳。(知道你的文件系统!在VAX/VMS下,当你得到一个文件的新版本的时候,旧的版本仍会被保留)。这种技术叫做“‘touching’一个文件”。Listing 7touch的实现在指定文件不存在的时候会创建一个新文件。注意文件以“binary”模式打开(在打开模式字符串中由字符b决定—在将来的专栏中我会详细讨论文件处理的问题)。

1:strftime的格式描述符

Code  Sample Output
---------------------------------------------
%a    Wed
%A    Wednesday
%b    Oct
%B    October
%c    Wed Oct 07 13:24:27 1992
%d    07    (day of month [01-31])
%H    13    (hour in [00-23])
%I    01    (hour in [01-12])
%j    281   (day of year [001-366])
%m    10    (month [01-12])
%M    24    (minute [00-59])
%p    PM
%S    27    (second [00-59] )
%U    40    (Sunday week of year [00-52])
%w    3     (day of week [0-6])
%W    40    (Monday week of year [00-52])
%x    Wed Oct 7, 1992
%X    13:24:27
%y    92
%Y    1992
%Z    EDT   (daylight savings indicator)

Listing 1 time1.c — 采用不同格式输出当前的日期和时间

#include <stdio.h>
#include <time.h>
 
#define BUFSIZE 128
 
main()
{
   time_t tval;
   struct tm *now;
   char buf[BUFSIZE];
   char *fancy_format =
     "Or getting really fancy:\n"
     "%A, %B %d, day %j of %Y.\n"
     "The time is %I:%M %p.";
 
   /* Get current date and time */
   tval = time(NULL);
   now = localtime(&tval);
   printf("The current date and time:\n"
         "%d/%02d/%02d %d:%02d:%02d\n\n",
     now->tm_mon+1, now->tm_mday, now->tm_year,
     now->tm_hour, now->tm_min, now->tm_sec);
   printf("Or in default system format:\n%s\n",
         ctime(&tval));
   strftime(buf,sizeof buf,fancy_format,now);
   puts(buf);
 
   return 0;
}
 
/*  Output
The current date and time:
10/06/92 12:58:00
 
Or in default system format:
Tue Oct 06 12:58:00 1992
 
Or getting really fancy:
Tuesday, October 06, day 280 of 1992.
The time is 12:58 PM.
*/
 
/* End of File */

Listing 2 time2.c —展示如何计算将来某一天的日期以及以秒为单位计算出的执行时间

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
main()
{
   time_t start, stop;
   struct tm *now;
   int ndays;
 
   /* Get current date and time */
   time(&start);
   now = localtime(&start);
 
   /* Enter an interval in days */
   fputs("How many days from now? ",stderr);
   if (scanf("%d",&ndays) !=1)
      return EXIT_FAILURE;
   now->tm_mday += ndays;
   if (mktime(now) != -1)
      printf("New date: %s",asctime(now));
   else
      puts("Sorry. Can't encode your date.");
 
   /* Calculate elapsed time */
   time(&stop);
   printf("Elapsed program time in seconds: %f\n",
     difftime(stop,start));
 
   return EXIT_SUCCESS;
}
 
/* Output
How many days from now? 45
New date: Fri Nov 20 12:40:32 1992
Elapsed program time in seconds: 1.000000
*/
 
/* End of File */

Listing 3 date.h — 一个简单的日期结构

struct Date
{
   int day;
   int month;
   int year;
};
typedef struct Date Date;
 
Date* date_interval(const Date *, const Date *);
/* End of File */

Listing 4 date_int.c — 计算两个日期的间隔

/* date_int.c: Compute duration between two dates */
 
#include "date.h"
 
#define isleap(y) \
 ((y)%4 == 0 && (y)%100 != 0 || (y)%400 == 0)
 
static int Dtab [2][13] =
{
  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
 
Date *date_interval(const Date *d1, const Date *d2)
{
   static Date result;
   int months, days, years, prev_month;
 
   /* Compute the interval - assume d1 precedes d2 */
   years = d2->year - d1->year;
   months = d2->month - d1->month;
   days = d2->day - d1->day;
 
   /* Do obvious corrections (days before months!)
    *
    * This is a loop in case the previous month is
    * February, and days < -28.
    */
   prev_month = d2->month - 1;
   while (days < 0)
   {
      /* Borrow from the previous month */
      if (prev_month == 0)
         prev_month = 12;
      --months;
      days += Dtab[isleap(d2->year)][prev_month--];
   }
 
   if (months < 0)
   {
      /* Borrow from the previous year */
      --years;
      months += 12;
   }
 
   /* Prepare output */
   result.month = months;
   result.day = days;
   result.year = years;
   return &result;
}
/* End of File */

Listing 5 tdate.c — 举例说明日期间隔函数的使用

/* tdate.c: Test date_interval() */
 
#include <stdio.h>
#include <stdlib.h>
#include "date.h"
 
main()
{
   Date d1, d2, *result;
   int nargs;
 
   /* Read in two dates - assume 1st precedes 2nd */
   fputs("Enter a date, MM/DD/YY> ",stderr);
   nargs = scanf("%d/%d/%d%*c", &d1.month,
     &d1.day, &d1.year);
   if (nargs != 3)
      return EXIT_FAILURE;
 
   fputs("Enter a later date, MM/DD/YY> ",stderr);
   nargs = scanf("%d/%d/%d%*c", &d2.month,
     &d2.day, &d2.year);
   if (nargs != 3)
      return EXIT_FAILURE;
 
   /* Compute interval in years, months, and days */
   result = date_interval(&d1, &d2);
   printf("years: %d, months: %d, days: %d\n",
      result->year, result->month, result->day);
   return EXIT_SUCCESS;
 
}
/* Sample Execution:
Enter a date, MM/DD/YY> 10/1/51
Enter a later date, MM/DD/YY> 10/6/92
years: 41, months: 0, days: 5 */
/* End of File */

Listing 6 ftime.c — 确定是否time1.c比time2.c更新

/* ftime.c: Compare file time stamps */
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
 
main()
{
   struct stat fs1, fs2;
 
   if (stat("time1.c",&fs1) == 0 &&
      stat("time2.c",&fs2) == 0)
   {
      double interval =
        difftime(fs2.st_mtime,fs1.st_mtime);
 
      printf("time1.c %s newer than time2.c\n",
        (interval < 0.0) ? "is" : "is not");
      return EXIT_SUCCESS;
   }
   else
      return EXIT_FAILURE;
}
/* Output
time1.c is not newer than time2.c */
/* End of File */

Listing 7 touch.c —通过覆盖旧文件或者创建一个新的文件来更新时间戳

/* touch.c: Update a file's time stamp */
 
#include <stdio.h>
 
void touch(char *fname)
{
   FILE *f = fopen(fname,"r+b");
   if (f != NULL)
   {
      char c = getc(f);
      rewind(f);
      putc(c,f);
   }
   else
      fopen(fname,"wb");
 
   fclose(f);
}
 
/* End of File */

 

posted on 2005-03-17 12:34 燕七 阅读(7391) 评论(91)  编辑 收藏

评论

# re: C语言中对时间和日期的处理 2005-03-17 20:55 一笑
Well done , pretty lady!!! :D

# 哇,一日不见,当刮目相看拉 2005-03-18 08:35 雨疏风骤
笑笑下定决心学e文了耶,怎么一出口都是ABC啊

# re: C语言中对时间和日期的处理 2005-03-18 10:16 Panic的小屋
打广告~
http://blog.vckbase.com/panic/

# re: C语言中对时间和日期的处理 2005-03-19 01:06 obq0387_cn
OK

# re: C语言中对时间和日期的处理 2005-08-03 00:37 视频会议系统
这个还是比较简单的

# re: C语言中对时间和日期的处理 2005-09-30 10:59 视频会议
对于程序的我自己都不是很懂,但是可以去研究一下别人的软件,说不定会有什么灵感哦:) 
推荐一个免安装客户端软件的网络视频会议软件,[url=http://www.eyesom.com]视频会议/网络视频会议[/url]


# re: C语言中对时间和日期的处理 2005-10-20 16:17 过客匆匆
写的相当不错的啊~~~

# re: C语言中对时间和日期的处理 2007-08-07 17:02 car mat
gfdgswr

# re: C语言中对时间和日期的处理 2007-08-08 11:22 casting
螺丝

# re: C语言中对时间和日期的处理 2007-08-08 11:26 Auto Carpet
汽车内饰

# re: C语言中对时间和日期的处理 2007-08-08 11:27 Auto Exhaust Manifold
汽车部件 

# re: C语言中对时间和日期的处理 2007-08-08 11:27 Auto metal Appliances
汽车金属部件

# re: C语言中对时间和日期的处理 2007-08-09 08:15 Auto Parts Manufacturers
汽车部件 

# re: C语言中对时间和日期的处理 2007-08-09 08:15 Bathroom
浴室套装

# re: C语言中对时间和日期的处理 2007-08-09 08:16 Mosquito nets
蚊帐

# re: C语言中对时间和日期的处理 2007-08-09 08:17 Lifting Anchor
建筑部件

# re: C语言中对时间和日期的处理 2007-08-09 08:19 Cosmetic
化妆品

# re: C语言中对时间和日期的处理 2007-08-09 08:19 Elevator Geared Traction Machine
电机

# re: C语言中对时间和日期的处理 2007-08-09 08:20 International Logistics
运输

# re: C语言中对时间和日期的处理 2007-08-09 08:21 LED work light
电灯

# re: C语言中对时间和日期的处理 2007-08-09 08:21 Id Bracelets
医疗器械

# re: C语言中对时间和日期的处理 2007-08-09 08:22 pen


# re: C语言中对时间和日期的处理 2007-08-09 08:23 corner protection
塑料保护

# re: C语言中对时间和日期的处理 2007-08-09 08:23 UL power cord
UL插头

# re: C语言中对时间和日期的处理 2007-08-09 08:24 power cords
插头

# re: C语言中对时间和日期的处理 2007-08-09 08:24 o-rings
O型环

# re: C语言中对时间和日期的处理 2007-08-09 08:25 Sandals
凉鞋

# re: C语言中对时间和日期的处理 2007-08-09 08:25 Shower room
淋浴套件

# re: C语言中对时间和日期的处理 2007-08-09 08:26 Socks
袜子

# re: C语言中对时间和日期的处理 2007-08-09 08:27 welding wire
焊锡

# re: C语言中对时间和日期的处理 2007-08-09 08:28 Ball Bearings
轴承

# re: C语言中对时间和日期的处理 2007-08-09 08:28 Phonebooth
电话亭

# re: C语言中对时间和日期的处理 2007-08-09 08:29 Trolling motor
货运

# re: C语言中对时间和日期的处理 2007-08-09 08:30 China Trade Manufacturers
目录

# re: C语言中对时间和日期的处理 2007-08-09 08:30 China Sports Item
体育器材

# re: C语言中对时间和日期的处理 2007-08-09 08:30 v2conference
www.v2conference.com

# re: C语言中对时间和日期的处理 2007-08-09 08:31 51netphone
www.51netphone.com

# re: C语言中对时间和日期的处理 2007-08-09 08:32 made in ningbo
商业目录

# re: C语言中对时间和日期的处理 2007-08-09 08:32 trade suppliers
交易供应

# re: C语言中对时间和日期的处理 2007-08-09 08:33 hour meter
大撒旦 

# re: C语言中对时间和日期的处理 2007-08-09 08:34 car mat
www.car-mat-suppliers.com

# re: C语言中对时间和日期的处理 2007-08-09 08:34 Desk Lamp
灯具

# re: C语言中对时间和日期的处理 2007-08-09 08:35 Chinese Antique Furniture
家具

# re: C语言中对时间和日期的处理 2007-08-09 08:36 Purse Brite
家居用品

# re: C语言中对时间和日期的处理 2007-08-09 08:36 Packing
包装

# re: C语言中对时间和日期的处理 2007-08-09 08:37 chinese furniture
中国家具

# re: C语言中对时间和日期的处理 2007-08-09 08:38 Ball Valve
铜阀

# re: C语言中对时间和日期的处理 2007-08-09 08:39 宁波摄影
宁波摄影

# ddjzrhus 2007-08-26 08:38 ddjzrhus
 tkpwgwlb http://jraeluxr.com yfdzwwfx ldgkytwq  [URL=http://kubygqrd.com]igvdboap[/URL]  <a href="http://rdgpqtwx.com">fvuoyfjz</a> 

# tbjaktel 2007-08-30 18:35 tbjaktel
 qkxnmvig http://kxauhyhw.com xzpshjqy rjgrejnx  [URL=http://cwgicrdh.com]imkboqge[/URL]  <a href="http://xvrpdpkc.com">odeuivky</a> 

# re: C语言中对时间和日期的处理 2007-11-26 11:01 gfdgsf
<a href="http://www.hu-song.cn/eabout.htm">label(logo)flexo printing machine</a> <a href="http://www.hu-song.cn/eabout.htm">adhesive label die-cutting machine</a> <a href="http://www.zheng-rui.com/news.asp">复膜胶</a> <a href="http://www.cnxingbao.com/index1.htm">UV光固机</a> <a href="http://www.cnxingbao.com">丝网印刷机</a> <a href="http://www.cnxingbao.com">丝印机</a> <a href="http://www.cnxingbao.com/index1.htm">网印机</a> <a href="http://www.hu-song.com/web/cn_product.asp.htm">分切机</a> <a href="http://www.hu-song.com">印刷机</a> <a href="http://www.hu-song.cn">柔印机</a> <a href="http://www.cnxingbao.com">全自动丝网印刷机</a> <a href="http://www.ruianpack.com/aboutus.asp">夹链粘合制袋机</a> <a href="http://www.gxysjx.com">印刷机</a> <a href="http://www.cnxinda.cn/lianx.htm">吹膜机</a> 


# re: C语言中对时间和日期的处理 2007-11-26 11:01 fsdf
<a href="http://www.ruianpack.com">制袋机</a> <a href="http://www.ruianpack.com/aboutus.asp">无纺布制袋机</a> <a href="http://www.ruianpack.com/aboutus1.asp">气泡膜机</a> <a href="http://www.ruianpack.com/aboutus2.asp">收缩膜机</a> <a href="http://www.hu-song.com/web/cn_product.asp.htm">分切机</a> <a href="http://www.zyydjx.com">吹膜机</a> <a href="http://www.zyydjx.com">制袋机</a> <a href="http://www.hu-song.cn/eKultur.htm">die-cutting machine</a> <a href="http://www.hu-song.cn/eabout.htm">label printing machine</a> <a href="http://www.hu-song.cn">柔版印刷机</a> <a href="http://www.hu-song.cn/about.htm">贴合机</a> <a href="http://www.hu-song.cn/eabout.htm">flexo printing machine</a> <a href="http://www.hu-song.cn/eabout.htm">auto slitting machine</a> 

# re: C语言中对时间和日期的处理 2007-11-26 11:02 fdsa

[URL=http://www.ruianpack.com]制袋机[/URL] [URL=http://www.ruianpack.com/aboutus.asp]无纺布制袋机[/URL] [URL=http://www.ruianpack.com/aboutus1.asp]气泡膜机[/URL] [URL=http://www.ruianpack.com/aboutus2.asp]收缩膜机[/URL] [URL=http://www.hu-song.com/web/cn_product.asp.htm]分切机[/URL] [URL=http://www.zyydjx.com">http://www.zyydjx.com]吹膜机[/URL] [URL=http://www.zyydjx.com">http://www.zyydjx.com]制袋机[/URL][URL=http://www.hu-song.cn/eKultur.htm">http://www.hu-song.cn/eKultur.htm]die-cutting machine[/URL] [URL=http://www.hu-song.cn/eabout.htm">http://www.hu-song.cn/eabout.htm]label printing machine[/URL] [url=http://www.hu-song.cn]柔版印刷机[/url] [url=http://www.hu-song.cn/about.htm]贴合机[/url] [URL=http://www.hu-song.cn/eabout.htm">http://www.hu-song.cn/eabout.htm]flexo printing machine[/URL] [URL=http://www.hu-song.cn/eabout.htm">http://www.hu-song.cn/eabout.htm]label(logo)flexo printing machine[/URL] [URL=http://www.hu-song.cn/eabout.htm">http://www.hu-song.cn/eabout.htm]adhesive label die-cutting machine[/URL] [URL=http://www.hu-song.cn/eabout.htm">http://www.hu-song.cn/eabout.htm]auto slitting machine[/URL] 

# re: C语言中对时间和日期的处理 2007-11-26 11:02 fdsa
[URL=http://www.zheng-rui.com/news.asp]复膜胶[/URL] [url=http://www.cnxingbao.com">http://www.cnxingbao.com">http://www.cnxingbao.com">http://www.cnxingbao.com]丝网印刷机[/url] [url=http://www.cnxingbao.com">http://www.cnxingbao.com">http://www.cnxingbao.com">http://www.cnxingbao.com]丝印机[/url] [url=http://www.cnxingbao.com">http://www.cnxingbao.com">http://www.cnxingbao.com">http://www.cnxingbao.com/index1.htm]UV光固机[/url] [url=http://www.hu-song.cn]柔印机[/url] [URL=http://www.dybj.com/en/product/102.asp">http://www.dybj.com/en/product/102.asp]film blowing machine[/URL] [URL=http://www.dybj.com/en/product/102.asp">http://www.dybj.com/en/product/102.asp]stretch film machine[/URL] [URL=http://www.dybj.com/en/product/101.asp">http://www.dybj.com/en/product/101.asp]bag making machine[/URL] [URL=http://www.dybj.com/en/product/101.asp">http://www.dybj.com/en/product/101.asp]bubble film machine[/URL] [URL=http://www.zjbeiyin.com]压痕机[/URL] [url=http://www.cnxingbao.com">http://www.cnxingbao.com">http://www.cnxingbao.com">http://www.cnxingbao.com]全自动丝网印刷机[/url] [URL=http://www.ruianpack.com/aboutus.asp]夹链粘合制袋机[/URL] [URL=http://www.gxysjx.com]印刷机[/URL]  [URL=http://www.cnxinda.cn/lianx.htm]吹膜机[/URL] 


# re: C语言中对时间和日期的处理 2007-11-26 11:02 fdsafd
[URL=http://www.ruianpack.com]制袋机[/URL] [URL=http://www.ruianpack.com/aboutus.asp]无纺布制袋机[/URL] [URL=http://www.ruianpack.com/aboutus1.asp]气泡膜机[/URL] [URL=http://www.ruianpack.com/aboutus2.asp]收缩膜机[/URL] [URL=http://www.hu-song.com/web/cn_product.asp.htm]分切机[/URL] [URL=http://www.zyydjx.com">http://www.zyydjx.com]吹膜机[/URL] [URL=http://www.zyydjx.com">http://www.zyydjx.com]制袋机[/URL][URL=http://www.hu-song.cn/eKultur.htm">http://www.hu-song.cn/eKultur.htm]die-cutting machine[/URL] [URL=http://www.hu-song.cn/eabout.htm">http://www.hu-song.cn/eabout.htm]label printing machine[/URL] [url=http://www.hu-song.cn]柔版印刷机[/url] [url=http://www.hu-song.cn/about.htm]贴合机[/url] [URL=http://www.hu-song.cn/eabout.htm">http://www.hu-song.cn/eabout.htm]flexo printing machine[/URL] [URL=http://www.hu-song.cn/eabout.htm">http://www.hu-song.cn/eabout.htm]label(logo)flexo printing machine[/URL] [URL=http://www.hu-song.cn/eabout.htm">