[151] | 1 | /**
|
---|
| 2 | * \file cf_util.c
|
---|
| 3 | *
|
---|
| 4 | * \author myusgun <myusgun@gmail.com>
|
---|
| 5 | *
|
---|
| 6 | * \brief 유틸 구현
|
---|
| 7 | */
|
---|
| 8 | #include "cf_util.h"
|
---|
| 9 | #include "cf_error.h"
|
---|
| 10 |
|
---|
| 11 | #include <stdio.h>
|
---|
| 12 | #include <time.h>
|
---|
| 13 |
|
---|
| 14 | #if defined(_WIN32) || defined(_WIN64)
|
---|
| 15 | # include <windows.h>
|
---|
| 16 | #else
|
---|
| 17 | # include <sys/time.h>
|
---|
| 18 | # include <errno.h>
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 | #define ASSERT_ARGS(x) \
|
---|
| 22 | if ((x)) \
|
---|
| 23 | return CF_ERROR_UTIL_INVALID_ARGS
|
---|
| 24 |
|
---|
| 25 | #if defined(_WIN32) || defined(_WIN64)
|
---|
| 26 | /* {{{ */
|
---|
| 27 | struct timezone
|
---|
| 28 | {
|
---|
| 29 | int tz_minuteswest; /* minutes W of Greenwich */
|
---|
| 30 | int tz_dsttime; /* type of dst correction */
|
---|
| 31 | };
|
---|
| 32 |
|
---|
| 33 | static int
|
---|
| 34 | gettimeofday (struct timeval *tv, struct timezone *tz)
|
---|
| 35 | {
|
---|
| 36 | FILETIME ft;
|
---|
| 37 | unsigned __int64 buf =0;
|
---|
| 38 | //static int tzflag = 0;
|
---|
| 39 |
|
---|
| 40 | if (NULL != tv)
|
---|
| 41 | {
|
---|
| 42 | GetSystemTimeAsFileTime (&ft);
|
---|
| 43 |
|
---|
| 44 | buf |= ft.dwHighDateTime;
|
---|
| 45 | buf <<= 32;
|
---|
| 46 | buf |= ft.dwLowDateTime;
|
---|
| 47 |
|
---|
| 48 | if (buf)
|
---|
| 49 | {
|
---|
| 50 | buf /= 10;
|
---|
| 51 | buf -= ((369 * 365 + 89) * (unsigned __int64) 86400) * 1000000;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | tv->tv_sec = (long)(buf / 1000000UL);
|
---|
| 55 | tv->tv_usec = (long)(buf % 1000000UL);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /*
|
---|
| 59 | if (NULL != tz)
|
---|
| 60 | {
|
---|
| 61 | if (!tzflag)
|
---|
| 62 | {
|
---|
| 63 | _tzset();
|
---|
| 64 | tzflag++;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | // Adjust for the timezone west of Greenwich
|
---|
| 68 | tz->tz_minuteswest = _timezone / 60;
|
---|
| 69 | tz->tz_dsttime = _daylight;
|
---|
| 70 | }
|
---|
| 71 | */
|
---|
| 72 |
|
---|
| 73 | return 0;
|
---|
| 74 | }
|
---|
| 75 | /* }}} */
|
---|
| 76 | #endif
|
---|
| 77 |
|
---|
[152] | 78 | /**
|
---|
| 79 | * 시스템 오류코드를 반환
|
---|
| 80 | *
|
---|
| 81 | * \return 시스템 오류코드
|
---|
| 82 | *
|
---|
| 83 | * \remarks
|
---|
| 84 | * windows : GetLastError () <br />
|
---|
| 85 | * linux/unix : errno
|
---|
| 86 | */
|
---|
[151] | 87 | int
|
---|
| 88 | CF_Util_GetSystemError (void)
|
---|
| 89 | {
|
---|
| 90 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[152] | 91 | return GetLastError ();
|
---|
[151] | 92 | #else
|
---|
| 93 | return errno;
|
---|
| 94 | #endif
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[152] | 97 | /**
|
---|
| 98 | * 현재 시간을 가져옴
|
---|
| 99 | *
|
---|
| 100 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 101 | *
|
---|
[154] | 102 | * \param dt CF_UTIL_DATETIME 구조체 주소
|
---|
[152] | 103 | */
|
---|
[151] | 104 | int
|
---|
| 105 | CF_Util_GetCurrentTime (CF_UTIL_DATETIME * dt)
|
---|
| 106 | {
|
---|
| 107 | struct timeval timeVal;
|
---|
| 108 | struct tm * timebuf;
|
---|
| 109 |
|
---|
| 110 | ASSERT_ARGS (dt == NULL);
|
---|
| 111 |
|
---|
| 112 | gettimeofday (&timeVal, NULL);
|
---|
| 113 | timebuf = localtime ((const time_t *)&timeVal.tv_sec);
|
---|
[158] | 114 | if (timebuf == NULL)
|
---|
| 115 | return CF_ERROR_UTIL_GET_LOCALTIME;
|
---|
[151] | 116 |
|
---|
| 117 | dt->year = timebuf->tm_year + 1900;
|
---|
| 118 | dt->month = timebuf->tm_mon + 1;
|
---|
| 119 | dt->day = timebuf->tm_mday;
|
---|
| 120 | dt->week = timebuf->tm_wday;
|
---|
| 121 |
|
---|
| 122 | dt->hour = timebuf->tm_hour;
|
---|
| 123 | dt->min = timebuf->tm_min;
|
---|
| 124 | dt->sec = timebuf->tm_sec;
|
---|
| 125 |
|
---|
| 126 | dt->usec = (int) timeVal.tv_usec;
|
---|
| 127 |
|
---|
| 128 | return CF_OK;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[152] | 131 | /**
|
---|
| 132 | * 시간정보를 문자열로 변환
|
---|
| 133 | *
|
---|
| 134 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 135 | *
|
---|
[154] | 136 | * \param dt CF_UTIL_DATETIME 구조체 주소
|
---|
[152] | 137 | * \param str 변환한 문자열을 저장할 충분한 공간의 메모리
|
---|
[154] | 138 | *
|
---|
| 139 | * \remarks
|
---|
| 140 | * 날짜/시간 문자열 형식 : yyyy-MM-dd HH:mm:ss.SSS
|
---|
[152] | 141 | */
|
---|
[151] | 142 | int
|
---|
| 143 | CF_Util_GetTimeString (const CF_UTIL_DATETIME * dt,
|
---|
[152] | 144 | char * str)
|
---|
[151] | 145 | {
|
---|
[152] | 146 | ASSERT_ARGS (dt == NULL);
|
---|
| 147 | ASSERT_ARGS (str == NULL);
|
---|
[151] | 148 |
|
---|
[152] | 149 | snprintf (str, CF_UTIL_DATETIME_LENGTH,
|
---|
[151] | 150 | "%04d-%02d-%02d %02d:%02d:%02d.%03d",
|
---|
| 151 | dt->year, dt->month, dt->day,
|
---|
| 152 | dt->hour, dt->min, dt->sec, dt->usec);
|
---|
| 153 |
|
---|
| 154 | return CF_OK;
|
---|
| 155 | }
|
---|