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