asctime()、asctime64() - 文字ストリングへの時間の変換

標準

標準/拡張機能 C/C++ 依存項目

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification、バージョン 3
Language Environment®

両方  

形式

#include <time.h>

char *asctime(const struct tm *timeptr);
#define _LARGE_TIME_API
#include <time.h>

char asctime64(const struct tm *timeptr);

機能説明

timeptr で示される構造体として保管された 時間を文字ストリングに変換します。timeptr 値は、gmtime() または localtime() を 呼び出すと取得できます。両方の関数は、time.h で 定義した tm 構造体を指すポインターを戻します。

asctime() が作成するストリングの結果には、ちょうど 26 文字が 含まれ、以下の形式となっています。
   "%.3s %.3s%3d %.2d:%.2d:%.2d %d¥n"
以下に示すのは、戻されたストリングの例です。
   Fri Jun 16 02:03:55 2006¥n¥0
注 :
  1. time() 関数を呼び出すと戻されるカレンダー時間は、エポック、つまり 協定世界時 (UTC) 1970 年 1 月 1 日 00:00:00 から始まっています。
  2. asctime() 関数は 24 時間時計形式を使用します。
  3. 曜日は、SunMonTueWedThuFri、および Sat に省略されます。
  4. 月は、JanFebMarAprMayJunJulAugSepOctNov、および Dec に省略されます。
  5. すべてのフィールドは幅が一定です。
  6. 一桁しかない日付は、ゼロまたはブランク・スペースが その前に置かれます。
  7. 改行文字 (¥n) および NULL 文字 (¥0) がストリングの最後の 2 つの 位置を占めます。
  8. asctime()、ctime()、およびその他の時間関数は、戻りストリング を保持するために、静的に割り振られた共通のバッファーを使用できます。これらの関数の 1 つへのそれぞれの呼び出しは、直前の 呼び出し結果を破棄することがあります。

関数 asctime64() は、asctime() とまったく同じように動作しますが、2038 年 1 月 19 日の 03:14:07 UTC を超え 9999 年 12 月 31 日の 23:59:59 UTC までを限度とするカレンダー時間を指す、time64_t 値を変換できます。

戻り値

正常に実行された場合、asctime() は結果として生じた文字ストリングを 指すポインターを戻します。

正常に実行されなかった場合、NULL を戻します。

CELEBA06
⁄* CELEBA06                                      

   This example polls the system clock and prints a message                     
   giving the current time.                                                     
                                                                                
 *⁄                                                                             
#include <time.h>                                                               
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
    struct tm *newtime;                                                         
    time_t ltime;                                                               
                                                                                
        ⁄* Get the time in seconds *⁄                                           
    time(&ltime);                                                               
        ⁄* Break it down & store it in the structure tm *⁄                      
    newtime = localtime(&ltime);                                                
                                                                                
        ⁄* Print the local time as a string *⁄                                  
    printf("The current date and time are %s",                                  
             asctime(newtime));                                                 
}                                                                               
出力:
The current date and time are Fri Jun 16 13:29:51 2006

関連情報