1 | /**
|
---|
2 | * @file util.cpp
|
---|
3 | * @author myusgun@gmail.com
|
---|
4 | * @brief util
|
---|
5 | */
|
---|
6 | #include "cf/util.h"
|
---|
7 |
|
---|
8 | #include <stdio.h>
|
---|
9 | #include <time.h>
|
---|
10 | #include <errno.h>
|
---|
11 | #include <string.h>
|
---|
12 |
|
---|
13 | #ifdef _ON_WINDOWS
|
---|
14 | # include <windows.h>
|
---|
15 | # include <sys/timeb.h>
|
---|
16 | #else
|
---|
17 | # include <sys/time.h>
|
---|
18 | # include <sys/types.h>
|
---|
19 | # include <unistd.h>
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | /*--------------------------------------------------------------*/
|
---|
23 | static const cf::char_t * getWeekName(const cf::int32_t week)
|
---|
24 | {
|
---|
25 | static const cf::char_t * weekday[] = {
|
---|
26 | "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
|
---|
27 | };
|
---|
28 |
|
---|
29 | return weekday[week];
|
---|
30 | }
|
---|
31 | /*--------------------------------------------------------------*/
|
---|
32 |
|
---|
33 | cf::util * cf::util::getInstance()
|
---|
34 | {
|
---|
35 | static cf::util instance;
|
---|
36 |
|
---|
37 | return &instance;
|
---|
38 | }
|
---|
39 |
|
---|
40 | cf::util::datetime cf::util::getDateTime() const
|
---|
41 | throw (cf::exception)
|
---|
42 | {
|
---|
43 | cf::util::datetime dt;
|
---|
44 | struct timeval timeVal;
|
---|
45 | struct tm * timebuf = NULL;
|
---|
46 |
|
---|
47 | #ifdef _ON_WINDOWS
|
---|
48 | __time64_t t64;
|
---|
49 | __timeb64 tb64;
|
---|
50 |
|
---|
51 | _time64 (&t64);
|
---|
52 | _ftime64 (&tb64);
|
---|
53 | timebuf = _localtime64 (&t64);
|
---|
54 | timeVal.tv_usec = tb64.millitm;
|
---|
55 | #else
|
---|
56 | gettimeofday(&timeVal, NULL);
|
---|
57 | timebuf = localtime((const time_t *)&timeVal.tv_sec);
|
---|
58 | timeVal.tv_usec /= 1000;
|
---|
59 | #endif
|
---|
60 | if (!timebuf)
|
---|
61 | THROW_EXCEPTION("cannot get localtime(" << errno
|
---|
62 | << ":" << strerror(errno) << ")");
|
---|
63 |
|
---|
64 | dt.mYear = timebuf->tm_year + 1900;
|
---|
65 | dt.mMonth = timebuf->tm_mon + 1;
|
---|
66 | dt.mDay = timebuf->tm_mday;
|
---|
67 | dt.mWeek = timebuf->tm_wday;
|
---|
68 | dt.mWeekName = getWeekName(dt.mWeek);
|
---|
69 |
|
---|
70 | dt.mHour = timebuf->tm_hour;
|
---|
71 | dt.mMin = timebuf->tm_min;
|
---|
72 | dt.mSec = timebuf->tm_sec;
|
---|
73 |
|
---|
74 | dt.mUsec = (cf::int32_t)timeVal.tv_usec;
|
---|
75 |
|
---|
76 | return dt;
|
---|
77 | }
|
---|
78 |
|
---|
79 | cf::int64_t cf::util::getTimeStamp() const
|
---|
80 | {
|
---|
81 | #ifdef _ON_WINDOWS
|
---|
82 | return _time64(NULL);
|
---|
83 | #else
|
---|
84 | return time(NULL);
|
---|
85 | #endif
|
---|
86 | }
|
---|