반응형
기본적으로 struct timeval 구조체는 유닉스에서 시간을 표시하기 위해서 사용하는 것이다. 그래서 윈도우에서 이 구조체를 사용해야 할 때, 어떻게 시간을 표시해야 하는지 잠시 헤매일 수 있다.(방법은

아주 간단하다. )


 먼저 struct timeval 구조체는 다음과 같다.


timeval

The Windows Sockets timeval structure is used to specify time values. It is associated with the Berkeley Software Distribution (BSD) file Time.h.

struct timeval {
  long    tv_sec;         // seconds 
  long    tv_usec;        // and microseconds 
};

Members

tv_sec
Time value, in seconds.
tv_usec
Time value, in microseconds.

Requirements

  Version: Requires Windows Sockets 2.0.
  Header: Declared in Winsock2.h.


 그런데 실제로 역시서 tv_sec 은 1970년 1월 1일부터 현재까지의 시간을 초로 표시한 것이다.

어디서 많이 보던 값 같지는 않은가? 그렇다 time_t 를 이용하는 clock() 함수가 리턴하는 값과

동일하다. 즉 time_t 를 이용하는 곳에 이 값을 이용하면 된다는 것이다. 현재의 시간을 time_t

를 이용해서 출력하는 함수는 localtime 이다.


localtime

Converts a time value and corrects for the local time zone.

struct tm *localtime( const time_t *timer );

Routine Required Header Compatibility
localtime <time.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

localtime returns a pointer to the structure result. If the value in timer represents a date before midnight, January 1, 1970, localtime returns NULL. The fields of the structure type tm store the following values, each of which is an int:

tm_sec

Seconds after minute (0 – 59)

tm_min

Minutes after hour (0 – 59)

tm_hour

Hours after midnight (0 – 23)

tm_mday

Day of month (1 – 31)

tm_mon

Month (0 – 11; January = 0)

tm_year

Year (current year minus 1900)

tm_wday

Day of week (0 – 6; Sunday = 0)

tm_yday

Day of year (0 – 365; January 1 = 0)

tm_isdst

Positive value if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative value if status of daylight saving time is unknown. The C run-time library assumes the United States’s rules for implementing the calculation of Daylight Saving Time (DST).

Parameter

timer

Pointer to stored time

Remarks

The localtime function converts a time stored as a time_t value and stores the result in a structure of type tm. The long value timer represents the seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). This value is usually obtained from the time function.

gmtime, mktime, and localtime all use a single statically allocated tm structure for the conversion. Each call to one of these routines destroys the result of the previous call.

localtime corrects for the local time zone if the user first sets the global environment variable TZ. When TZ is set, three other environment variables (_timezone, _daylight, and _tzname) are automatically set as well. See _tzset for a description of these variables. TZ is a Microsoft extension and not part of the ANSI standard definition of localtime.

Note   The target environment should try to determine whether daylight saving time is in effect.

Example

/* LOCALTIM.C: This program uses time to get the current time 
 * and then uses localtime to convert this time to a structure 
 * representing the local time. The program converts the result 
 * from a 24-hour clock to a 12-hour clock and determines the 
 * proper extension (AM or PM).
 */

#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
 여기서 long_time 에 timeval.tv_sec 값을 넣어주면 우리가 원하는 작업이 끝나게
된다.
반응형
Posted by Real_G