• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴(yán)以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            C語言中對時間和日期的處理

            From:http://blog.vckbase.com/mengjin/archive/2005/03/17/3669.html

            C語言中對時間和日期的處理

            Chuck Allison

            Chuck Allison是鹽湖城圣Latter Day教堂總部下耶穌教堂家族歷史研究處的軟件體系設(shè)計師。他擁有數(shù)學(xué)學(xué)士和數(shù)學(xué)碩士學(xué)位。他從1975年起開始編程,從1984年起他開始從事c語言的教學(xué)和開發(fā)。他目前的興趣是面向?qū)ο蟮募夹g(shù)及其教育。他是X3J16ANSI C ++標(biāo)準(zhǔn)化委員會的一員。發(fā)送e-mailallison@decus.org,或者撥打電話到(801)240-4510均可以與他取得聯(lián)系。

            大部分的操作系統(tǒng)有辦法得到當(dāng)前的日期和時間。通過定義在time.h的庫函數(shù),ANSI C能以許多不同的形式得到這個信息。函數(shù)time返回一個類型為time_t的值(通常為long),該函數(shù)在運行期間對當(dāng)前的日期和時間進(jìn)行編碼。然后你可以將這個返回值傳遞給其他能對該值進(jìn)行解碼和格式化的函數(shù)。

            Listing 1中的程序使用函數(shù)timelocaltimestrftime以不同的形式輸出當(dāng)前的日期和時間。函數(shù)localtime把已經(jīng)編碼的時間解碼成如下的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 */
            };

            每次當(dāng)你調(diào)用localtime的時候,它會重寫一個靜態(tài)的結(jié)構(gòu)并返回該結(jié)構(gòu)的地址(因此同一時刻在一個程序中只能取得一個這樣的結(jié)構(gòu),而不能做明顯的拷貝)。函數(shù)ctime返回一個指向靜態(tài)字符串的指針,該字符串以標(biāo)準(zhǔn)的格式包含了完整的時間和日期。strftime根據(jù)用戶的指定格式格式化字符串(例如,%A代表一周中每一天的名稱)。Table 1列出了格式描述符的完整列表。

            時間/日期運算

            通過改變tm結(jié)構(gòu)里的值,可對時間/日期進(jìn)行運算。Listing 2中的程序展示了如何計算將來某天的日期和以秒為單位所計算出的程序執(zhí)行時間。注意函數(shù)time的語法(參數(shù)time_t由地址傳入,并非作為函數(shù)的返回值)。函數(shù)mktime改變tm結(jié)構(gòu)的值,以便日期和時間在一個合適的范圍內(nèi),之后day-of-week (tm_wday)和day-of-year (tm_yday)域進(jìn)行相應(yīng)的更新。mktimetm結(jié)構(gòu)中日期和時間的值置于合適的范圍之內(nèi),相應(yīng)的更新day of week (tm-wday)和day of year (tm-yday)的值。這種情況發(fā)生在當(dāng)一個日期超出了你的實現(xiàn)能夠支持的范圍的時候。例如,我的MS-DOS的編譯器不能編碼1970年1月份之前的日期。函數(shù)asctime返回tm參數(shù)所描述時間的標(biāo)準(zhǔn)字符串(因此ctime (&tval)與asctime (localtime(&tval)是相等的)。函數(shù)difftime返回用秒做單位的兩個time_t的差。

            如果需要處理超出系統(tǒng)范圍的日期,或者需要計算兩個日期的間隔又不是用秒來做單位,那你需要設(shè)計自己的date編碼。Listing 3Listing 5中 的應(yīng)用程序通過使用一個簡單的month-day-year結(jié)構(gòu),展示了確定兩個日期間隔的年數(shù)、月份數(shù)和天數(shù)的技術(shù)。日期的相減就像你在小學(xué)里做的減法 那樣(例如,首先進(jìn)行天數(shù)的相減,如果需要就向月份數(shù)借位,以此類推)。注意跳過的年份都被計算進(jìn)去了。為了簡略起見,date_interval函數(shù)假設(shè)日期都是有效的,并且第一個日期在第二個日期之前。函數(shù)返回一個指向靜態(tài)Date結(jié)構(gòu)的指針,該結(jié)構(gòu)包含了我們想要的答案。

            文件時間/日期戳

            大多數(shù)操作系統(tǒng)為文件維護(hù)時間/日期戳。至少你能得知一個文件最后被修改的時間。(常用的make工 具使用這一信息來決定一個文件是否需要被重新編譯,或者一個應(yīng)用程序是否需要被重新連接)。由于文件系統(tǒng)在不同平臺上有所不同,沒有什么通用的函數(shù)得到一 個文件的時間/日期戳,因此ANSI 標(biāo)準(zhǔn)沒有定義這樣的函數(shù)。然而,大多數(shù)流行的操作系統(tǒng)(包括MS-DOS和VAX/VMS)提供了UNIX函數(shù)stat,該函數(shù)返回相關(guān)的文件信息,包括用time_t表示的最后修改時間。

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

            如果你需要更新一個文件的時間/ 日期戳到當(dāng)前時間,可簡單的重寫文件的第一個字節(jié)。雖然實際內(nèi)容并未改變,但你的文件系統(tǒng)會認(rèn)為文件已經(jīng)被改變了,并且會相應(yīng)的更新時間/日期戳。(知道 你的文件系統(tǒng)!在VAX/VMS下,當(dāng)你得到一個文件的新版本的時候,舊的版本仍會被保留)。這種技術(shù)叫做“‘touching’一個文件”。Listing 7touch的實現(xiàn)在指定文件不存在的時候會創(chuàng)建一個新文件。注意文件以“binary”模式打開(在打開模式字符串中由字符b決定—在將來的專欄中我會詳細(xì)討論文件處理的問題)。

            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 — 采用不同格式輸出當(dāng)前的日期和時間

            #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 —展示如何計算將來某一天的日期以及以秒為單位計算出的執(zhí)行時間

            #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 — 一個簡單的日期結(jié)構(gòu)

            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=

            {

              {
            0312831303130313130313031},

              {
            0312931303130313130313031}

            };

             

            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 — 舉例說明日期間隔函數(shù)的使用

            /* 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 —通過覆蓋舊文件或者創(chuàng)建一個新的文件來更新時間戳

            /* touch.c: Update a file's time stamp */

             

            #include 
            <stdio.h>

             

            void touch(char *fname)

            {

               FILE 
            *= 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 2007-06-11 17:11 楊粼波 閱讀(459) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲国产另类久久久精品小说| 99久久精品无码一区二区毛片| 久久se精品一区精品二区| 成人综合久久精品色婷婷| 亚洲精品国产自在久久| 久久综合色之久久综合| 久久午夜综合久久| 久久一区二区三区免费| 久久99精品国产麻豆蜜芽| 久久黄视频| 国产香蕉久久精品综合网| 一级A毛片免费观看久久精品| 日本加勒比久久精品| 2019久久久高清456| 综合久久国产九一剧情麻豆| 天天躁日日躁狠狠久久 | 久久涩综合| 香蕉久久AⅤ一区二区三区| 久久久久久久91精品免费观看| 亚洲午夜福利精品久久| 大香伊人久久精品一区二区| 无码AV波多野结衣久久| 精品久久久久久亚洲| 久久久久国产| 久久婷婷五月综合97色一本一本| 国产亚洲欧美精品久久久| 国产精品综合久久第一页| 青春久久| 国产精品久久波多野结衣| 久久精品国产亚洲Aⅴ香蕉| 亚洲伊人久久精品影院| 91秦先生久久久久久久| 日本五月天婷久久网站| 国产精品久久久福利| 人妻少妇精品久久| 国产午夜久久影院| 久久只这里是精品66| 亚洲国产精品热久久| 亚洲精品无码久久久影院相关影片 | 国产亚洲精久久久久久无码| 国产午夜精品理论片久久 |