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 |
|
---|
78 | int
|
---|
79 | CF_Util_GetSystemError (void)
|
---|
80 | {
|
---|
81 | #if defined(_WIN32) || defined(_WIN64)
|
---|
82 | return WSAGetLastError ();
|
---|
83 | #else
|
---|
84 | return errno;
|
---|
85 | #endif
|
---|
86 | }
|
---|
87 |
|
---|
88 | int
|
---|
89 | CF_Util_GetCurrentTime (CF_UTIL_DATETIME * dt)
|
---|
90 | {
|
---|
91 | struct timeval timeVal;
|
---|
92 | struct tm * timebuf;
|
---|
93 |
|
---|
94 | ASSERT_ARGS (dt == NULL);
|
---|
95 |
|
---|
96 | gettimeofday (&timeVal, NULL);
|
---|
97 | timebuf = localtime ((const time_t *)&timeVal.tv_sec);
|
---|
98 |
|
---|
99 | dt->year = timebuf->tm_year + 1900;
|
---|
100 | dt->month = timebuf->tm_mon + 1;
|
---|
101 | dt->day = timebuf->tm_mday;
|
---|
102 | dt->week = timebuf->tm_wday;
|
---|
103 |
|
---|
104 | dt->hour = timebuf->tm_hour;
|
---|
105 | dt->min = timebuf->tm_min;
|
---|
106 | dt->sec = timebuf->tm_sec;
|
---|
107 |
|
---|
108 | dt->usec = (int) timeVal.tv_usec;
|
---|
109 |
|
---|
110 | return CF_OK;
|
---|
111 | }
|
---|
112 |
|
---|
113 | int
|
---|
114 | CF_Util_GetTimeString (const CF_UTIL_DATETIME * dt,
|
---|
115 | char * buffer)
|
---|
116 | {
|
---|
117 | ASSERT_ARGS (dt == NULL);
|
---|
118 | ASSERT_ARGS (buffer == NULL);
|
---|
119 |
|
---|
120 | snprintf (buffer, CF_UTIL_DATETIME_LENGTH,
|
---|
121 | "%04d-%02d-%02d %02d:%02d:%02d.%03d",
|
---|
122 | dt->year, dt->month, dt->day,
|
---|
123 | dt->hour, dt->min, dt->sec, dt->usec);
|
---|
124 |
|
---|
125 | return CF_OK;
|
---|
126 | }
|
---|