source: libcf/trunk/src/cf_util.c@ 154

Last change on this file since 154 was 154, checked in by cheese, 10 years ago

#1 fix file-context bug

File size: 2.7 KB
Line 
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/* {{{ */
27struct timezone
28{
29 int tz_minuteswest; /* minutes W of Greenwich */
30 int tz_dsttime; /* type of dst correction */
31};
32
33static int
34gettimeofday (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/**
79 * 시스템 오류코드를 반환
80 *
81 * \return 시스템 오류코드
82 *
83 * \remarks
84 * windows : GetLastError () <br />
85 * linux/unix : errno
86 */
87int
88CF_Util_GetSystemError (void)
89{
90#if defined(_WIN32) || defined(_WIN64)
91 return GetLastError ();
92#else
93 return errno;
94#endif
95}
96
97/**
98 * 현재 시간을 가져옴
99 *
100 * \return 성공 시, CF_OK; 실패 시, 오류 코드
101 *
102 * \param dt CF_UTIL_DATETIME 구조체 주소
103 */
104int
105CF_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);
114
115 dt->year = timebuf->tm_year + 1900;
116 dt->month = timebuf->tm_mon + 1;
117 dt->day = timebuf->tm_mday;
118 dt->week = timebuf->tm_wday;
119
120 dt->hour = timebuf->tm_hour;
121 dt->min = timebuf->tm_min;
122 dt->sec = timebuf->tm_sec;
123
124 dt->usec = (int) timeVal.tv_usec;
125
126 return CF_OK;
127}
128
129/**
130 * 시간정보를 문자열로 변환
131 *
132 * \return 성공 시, CF_OK; 실패 시, 오류 코드
133 *
134 * \param dt CF_UTIL_DATETIME 구조체 주소
135 * \param str 변환한 문자열을 저장할 충분한 공간의 메모리
136 *
137 * \remarks
138 * 날짜/시간 문자열 형식 : yyyy-MM-dd HH:mm:ss.SSS
139 */
140int
141CF_Util_GetTimeString (const CF_UTIL_DATETIME * dt,
142 char * str)
143{
144 ASSERT_ARGS (dt == NULL);
145 ASSERT_ARGS (str == NULL);
146
147 snprintf (str, CF_UTIL_DATETIME_LENGTH,
148 "%04d-%02d-%02d %02d:%02d:%02d.%03d",
149 dt->year, dt->month, dt->day,
150 dt->hour, dt->min, dt->sec, dt->usec);
151
152 return CF_OK;
153}
Note: See TracBrowser for help on using the repository browser.