OSDN Git Service

initial files
[iptd/iPTd_R3.git] / src / Raym / Date.cpp
1 //
2 // Date.cpp
3 //
4
5 #define DBG_LEVEL 0
6 #include <Raym/Log.h>
7 #include <Raym/Date.h>
8
9 #ifdef _WIN32
10
11 #if defined(_MSC_VER) || defined(_MSC_EXTEIO)
12     #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
13 #else
14     #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
15 #endif
16
17 static int gettimeofday(struct timeval *tv, struct timezone *tz)
18 {
19     FILETIME ft;
20     unsigned __int64 tmpres = 0;
21     static int tzflag;
22
23     if (NULL != tv)
24     {
25         GetSystemTimeAsFileTime(&ft);
26
27         tmpres = ft.dwHighDateTime;
28         tmpres <<= 32;
29         tmpres |= ft.dwLowDateTime;
30
31         /*converting file time to unix epoch*/
32         tmpres /= 10;  /*convert into microseconds*/
33         tmpres -= DELTA_EPOCH_IN_MICROSECS;
34         tv->tv_sec = (long)(tmpres / 1000000UL);
35         tv->tv_usec = (long)(tmpres % 1000000UL);
36     }
37
38     if (NULL != tz)
39     {
40 /*
41         if (!tzflag)
42         {
43             _tzset();
44             tzflag++;
45         }
46         tz->tz_minuteswest = _get_timezone / 60;
47         tz->tz_dsttime = _daylight;
48 */
49     }
50
51     return 0;
52 }
53
54 #endif // _WIN32
55
56 namespace Raym
57 {
58
59 Date::Date()
60 {
61     DebugLog2("Date::Date()");
62
63     _time.tv_sec = 0;
64     _time.tv_usec = 0;
65     _zone.tz_minuteswest = 0;
66     _zone.tz_dsttime = 0;
67 }
68
69 Date::~Date()
70 {
71     _time.tv_sec = 0;
72     _time.tv_usec = 0;
73     _zone.tz_minuteswest = 0;
74     _zone.tz_dsttime = 0;
75
76     DebugLog2("Date::~Date()");
77 }
78
79 Date *Date::alloc()
80 {
81     DebugLog2("Date::alloc()");
82
83     return new Date();
84 }
85
86 Date *Date::date()
87 {
88     DebugLog2("Date::date()");
89
90     Date *result = NULL;
91     result = Date::alloc()->init();
92     if (result != NULL)
93     {
94         result->autorelease();
95     }
96     return result;
97 }
98
99 Date *Date::dateWithTimeIntervalSinceNow(TimeInterval seconds)
100 {
101     DebugLog2("Date::dateWithTimeIntervalSinceNow(seconds)");
102
103     Date *result = NULL;
104     result = Date::alloc()->initWithTimeIntervalSinceNow(seconds);
105     if (result != NULL)
106     {
107         result->autorelease();
108     }
109     return result;
110 }
111
112 Date *Date::init()
113 {
114     DebugLog2("Date::init()");
115
116     if (gettimeofday(&_time, &_zone) != 0)
117     {
118         return NULL;
119     }
120     return this;
121 }
122
123 Date *Date::initWithTimeInterval(TimeInterval seconds, Date *refDate)
124 {
125     DebugLog2("Date::initWithTimeInterval(seconds,refDate)");
126
127     if (refDate == NULL)
128     {
129         return NULL;
130     }
131
132     _time = refDate->_time;
133     _zone = refDate->_zone;
134
135     _time.tv_sec += (int)seconds;
136     _time.tv_usec += ((int)(seconds * 1000000)) % 1000000;
137     if (_time.tv_usec >= 1000000)
138     {
139         _time.tv_sec += 1;
140         _time.tv_usec -= 1000000;
141     }
142
143     return this;
144 }
145
146 Date *Date::initWithTimeIntervalSinceNow(TimeInterval seconds)
147 {
148     DebugLog2("Date::initWithTimeIntervalSinceNow(seconds)");
149
150     if (gettimeofday(&_time, &_zone) != 0)
151     {
152         return NULL;
153     }
154
155     _time.tv_sec += (int)seconds;
156     _time.tv_usec += ((int)(seconds * 1000000)) % 1000000;
157     if (_time.tv_usec >= 1000000)
158     {
159         _time.tv_sec += 1;
160         _time.tv_usec -= 1000000;
161     }
162
163     return this;
164 }
165
166 TimeInterval Date::timeIntervalSinceDate(Date *anotherDate)
167 {
168     DebugLog2("Date::timeIntervalSinceDate(anotherDate)");
169
170     if (anotherDate == NULL)
171     {
172         abort();
173     }
174
175     TimeInterval self, another;
176     self = _time.tv_usec;
177     self = _time.tv_sec + self / 1000000;
178     another = anotherDate->_time.tv_usec;
179     another = anotherDate->_time.tv_sec + another / 1000000;
180
181     return self - another;
182 }
183
184 Date *Date::dateByAddingTimeInterval(TimeInterval seconds)
185 {
186     DebugLog2("Date::dateByAddingTimeInterval(seconds)");
187
188     Date *result = Date::alloc()->initWithTimeInterval(seconds, this);
189     if (result != NULL)
190     {
191         result->autorelease();
192     }
193     return result;
194 }
195
196 ComparisonResult Date::compare(Date *anotherDate)
197 {
198     DebugLog2("Date::compare(anotherDate)");
199
200     if (!anotherDate)
201     {
202         abort();
203     }
204     if ((_time.tv_sec < anotherDate->_time.tv_sec) ||
205         ((_time.tv_sec == anotherDate->_time.tv_sec) && (_time.tv_usec < anotherDate->_time.tv_usec)))
206     {
207         return OrderedAscending;
208     }
209     if ((_time.tv_sec == anotherDate->_time.tv_sec) && (_time.tv_usec == anotherDate->_time.tv_usec))
210     {
211         return OrderedSame;
212     }
213     return OrderedDescending;
214 }
215
216 const char *Date::className()
217 {
218     return "Date";
219 }
220
221 } // Raym