다음을 통해 공유


time, _time32, _time64

시스템 시간을 가져옵니다.

time_t time(
   time_t *timer 
);
__time32_t _time32(
   __time32_t *timer 
);
__time64_t _time64(
   __time64_t *timer 
);

매개 변수

  • timer
    시간에 대한 저장소 위치에 대한 포인터입니다.

반환 값

오류 발생 시-1 또는 1970 년 1 월 1 일 자정 이후 경과 된 초 시간을 반환 합니다.

설명

time 함수는 자정 (00:00), 1970, 1 월 1 일 UTC (협정 세계시) 이후 경과 된 시간을 초 단위로 반환합니다. 반환 값은 timer 가 제공한 위치에 저장 됩니다. 이 매개 변수는 반환 값이 저장되지 않는 경우 NULL 일 수 있습니다.

time 는 _time64 및 time_t 에 대한 래퍼이며 기본적으로 __time64_t 와 같습니다. 컴파일어가 이전 32 비트 time_t 로 time_t를 해석 할 수 있도록 하는 경우, _USE_32BIT_TIME_T 를 정의할 수 있습니다. 응용 프로그램이 2038년 1월 18일 후 실패 할 수 있기 때문에 사용하지 않는 것이 좋습니다 이 매크로의 사용은 64 비트 플랫폼에서 사용할 수 없습니다.

요구 사항

루틴

필수 헤더

time

<time.h>

_time32, _time64

<time.h>

호환성에 대한 자세한 내용은 소개 단원의 호환성 부분을 참조하십시오.

예제

// crt_times.c
// compile with: /W3
// This program demonstrates these time and date functions:
//      time         _ftime    ctime_s     asctime_s
//      _localtime64_s    _gmtime64_s    mktime    _tzset
//      _strtime_s     _strdate_s  strftime
//
// Also the global variable:
//      _tzname
//

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

int main()
{
    char tmpbuf[128], timebuf[26], ampm[] = "AM";
    time_t ltime;
    struct _timeb tstruct;
    struct tm today, gmt, xmas = { 0, 0, 12, 25, 11, 93 };
    errno_t err;

    // 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_s( tmpbuf, 128 );
    printf( "OS time:\t\t\t\t%s\n", tmpbuf );
    _strdate_s( tmpbuf, 128 );
    printf( "OS date:\t\t\t\t%s\n", tmpbuf );

    // Get UNIX-style time and display as number and string. 
    time( &ltime );
    printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
    err = ctime_s(timebuf, 26, &ltime);
    if (err)
    {
       printf("ctime_s failed due to an invalid argument.");
       exit(1);
    }
    printf( "UNIX time and date:\t\t\t%s", timebuf );

    // Display UTC. 
    err = _gmtime64_s( &gmt, &ltime );
    if (err)
    {
       printf("_gmtime64_s failed due to an invalid argument.");
    }
    err = asctime_s(timebuf, 26, &gmt);
    if (err)
    {
       printf("asctime_s failed due to an invalid argument.");
       exit(1);
    }
    printf( "Coordinated universal time:\t\t%s", timebuf );

    // Convert to time structure and adjust for PM if necessary. 
    err = _localtime64_s( &today, &ltime );
    if (err)
    {
       printf("_localtime64_s failed due to an invalid argument.");
       exit(1);
    }
    if( today.tm_hour >= 12 )
    {
   strcpy_s( ampm, sizeof(ampm), "PM" );
   today.tm_hour -= 12;
    }
    if( today.tm_hour == 0 )  // Adjust if midnight hour.
   today.tm_hour = 12;

    // Convert today into an ASCII string 
    err = asctime_s(timebuf, 26, &today);
    if (err)
    {
       printf("asctime_s failed due to an invalid argument.");
       exit(1);
    }

    // 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",
       timebuf + 11, ampm );

    // Print additional time information. 
    _ftime( &tstruct ); // C4996
    // Note: _ftime is deprecated; consider using _ftime_s instead
    printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
    printf( "Zone difference in hours from UTC:\t%u\n", 
             tstruct.timezone/60 );
    printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] ); //C4996
    // Note: _tzname is deprecated; consider using _get_tzname
    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 )
    {
       err = asctime_s(timebuf, 26, &xmas);
       if (err)
       {
          printf("asctime_s failed due to an invalid argument.");
          exit(1);
       }
       printf( "Christmas\t\t\t\t%s\n", timebuf );
    }

    // Use time structure to build a customized time string. 
    err = _localtime64_s( &today, &ltime );
    if (err)
    {
        printf(" _localtime64_s failed due to invalid arguments.");
        exit(1);
    }

    // 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 );
}
  

해당 .NET Framework 항목

해당 사항 없음. 표준 C 함수를 호출하려면 PInvoke를 사용합니다. 자세한 내용은 플랫폼 호출 예제를 참조하십시오.

참고 항목

참조

시간 관리

asctime, _wasctime

asctime_s, _wasctime_s

_ftime, _ftime32, _ftime64

gmtime, _gmtime32, _gmtime64

gmtime_s, _gmtime32_s, _gmtime64_s

localtime, _localtime32, _localtime64

localtime_s, _localtime32_s, _localtime64_s

_utime, _utime32, _utime64, _wutime, _wutime32, _wutime64