| 1 |
// $Id: b_time.h,v 1.5 2003/03/02 04:12:45 fukasawa Exp $ |
| 2 |
|
| 3 |
//============================================================================= |
| 4 |
/** |
| 5 |
* @file b_time.h |
| 6 |
* |
| 7 |
* @author Fukasawa Mitsuo |
| 8 |
* |
| 9 |
* |
| 10 |
* Copyright (C) 2001-2004 BEE Co.,Ltd. All rights reserved. |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or |
| 13 |
* modify it under the terms of the GNU General Public License |
| 14 |
* as published by the Free Software Foundation; either version 2 |
| 15 |
* of the License, or (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 |
*/ |
| 26 |
//============================================================================= |
| 27 |
|
| 28 |
#ifndef B_TIME_H |
| 29 |
#define B_TIME_H |
| 30 |
|
| 31 |
#include "b_ace.h" |
| 32 |
|
| 33 |
//----------------------------------------------------------------------------- |
| 34 |
// bee time class. |
| 35 |
//----------------------------------------------------------------------------- |
| 36 |
class b_time |
| 37 |
{ |
| 38 |
public: |
| 39 |
b_time() { memset(m_tv, 0, sizeof(m_tv)); } |
| 40 |
b_time(const struct tm& tv) { set(tv); } |
| 41 |
b_time(const ACE_Time_Value& tv) { set(tv); } |
| 42 |
b_time(const b_time& rhs) { copy(rhs); } |
| 43 |
~b_time() {} |
| 44 |
|
| 45 |
b_time operator=(const b_time& rhs) { |
| 46 |
if (this == &rhs) |
| 47 |
return *this; |
| 48 |
copy(rhs); |
| 49 |
return *this; |
| 50 |
} |
| 51 |
b_time operator=(const string& rhs) { |
| 52 |
BCHAR * fmt = _TX("%02d%02d/%02d/%02d %02d:%02d:%02d.%d0"); |
| 53 |
if (rhs.size() == 16) { |
| 54 |
fmt = _TX("%02d%02d%02d%02d%02d%02d%02d%02d"); |
| 55 |
} else if (rhs.size() == 12) { |
| 56 |
m_tv[0] = m_tv[7] = 0; |
| 57 |
fmt = _TX("%02d%02d%02d%02d%02d%02d"); |
| 58 |
} |
| 59 |
_stscanf(rhs.c_str(), fmt, &m_tv[0], &m_tv[1], &m_tv[2], |
| 60 |
&m_tv[3], &m_tv[4], &m_tv[5], |
| 61 |
&m_tv[6], &m_tv[7]); |
| 62 |
return *this; |
| 63 |
} |
| 64 |
|
| 65 |
ACE_Time_Value value() { |
| 66 |
struct tm tv; |
| 67 |
tv.tm_year = ((m_tv[0] * 100) + m_tv[1]) - 1900; |
| 68 |
tv.tm_mon = m_tv[2] - 1; |
| 69 |
tv.tm_mday = m_tv[3]; |
| 70 |
tv.tm_hour = m_tv[4]; |
| 71 |
tv.tm_min = m_tv[5]; |
| 72 |
tv.tm_sec = m_tv[6]; |
| 73 |
time_t t = mktime(&tv); |
| 74 |
ACE_Time_Value v(t, m_tv[7] * 10000); |
| 75 |
return v; |
| 76 |
} |
| 77 |
|
| 78 |
void toSemi(string& retstr) { |
| 79 |
BCHAR buf[48]; |
| 80 |
_stprintf(buf, _TX("%02d%02d%02d%02d%02d%02d%02d"), |
| 81 |
((m_tv[0] * 100) + m_tv[1]), m_tv[2], m_tv[3], m_tv[4], |
| 82 |
m_tv[5], m_tv[6], m_tv[7]); |
| 83 |
retstr = buf; |
| 84 |
} |
| 85 |
string toSemi() { |
| 86 |
string timeStr; |
| 87 |
toSemi(timeStr); |
| 88 |
return timeStr; // As area is in heap, return value; |
| 89 |
} |
| 90 |
|
| 91 |
void toString(string& retstr) { |
| 92 |
BCHAR buf[48]; |
| 93 |
_stprintf(buf, _TX("%d/%02d/%02d %02d:%02d:%02d.%02d"), |
| 94 |
((m_tv[0] * 100) + m_tv[1]), m_tv[2], m_tv[3], m_tv[4], |
| 95 |
m_tv[5], m_tv[6], m_tv[7]); |
| 96 |
retstr = buf; |
| 97 |
} |
| 98 |
string toString() { |
| 99 |
string timeStr; |
| 100 |
toString(timeStr); |
| 101 |
return timeStr; // As area is in heap, return value; |
| 102 |
} |
| 103 |
|
| 104 |
void set(const struct tm& tv) { |
| 105 |
m_tv[0] = (tv.tm_year + 1900) / 100; |
| 106 |
m_tv[1] = (tv.tm_year + 1900) % 100; |
| 107 |
m_tv[2] = tv.tm_mon + 1; |
| 108 |
m_tv[3] = tv.tm_mday; |
| 109 |
m_tv[4] = tv.tm_hour; |
| 110 |
m_tv[5] = tv.tm_min; |
| 111 |
m_tv[6] = tv.tm_sec; |
| 112 |
m_tv[7] = 0; |
| 113 |
} |
| 114 |
|
| 115 |
void set(const ACE_Time_Value& tv) { |
| 116 |
time_t sec = tv.sec(); |
| 117 |
struct tm * when = localtime(&sec); |
| 118 |
set(*when); |
| 119 |
m_tv[7] = (BYTE)(tv.usec() / 10000); // convert to 10msec |
| 120 |
} |
| 121 |
|
| 122 |
void set(const time_t& tv) { |
| 123 |
struct tm * when = localtime(&tv); |
| 124 |
set(*when); |
| 125 |
m_tv[7] = 0; |
| 126 |
} |
| 127 |
|
| 128 |
friend bool operator==(const b_time& lhs, const b_time& rhs); |
| 129 |
friend bool operator<(const b_time& lhs, const b_time& rhs); |
| 130 |
|
| 131 |
static UINT lapse(time_t curtime); |
| 132 |
static int makeDateName(time_t curtime, string& tvname); |
| 133 |
|
| 134 |
void copy(const b_time& rhs) { |
| 135 |
m_tv[0] = rhs.m_tv[0]; m_tv[1] = rhs.m_tv[1]; |
| 136 |
m_tv[2] = rhs.m_tv[2]; m_tv[3] = rhs.m_tv[3]; |
| 137 |
m_tv[4] = rhs.m_tv[4]; m_tv[5] = rhs.m_tv[5]; |
| 138 |
m_tv[6] = rhs.m_tv[6]; m_tv[7] = rhs.m_tv[7]; |
| 139 |
} |
| 140 |
// |
| 141 |
public: |
| 142 |
BYTE m_tv[8]; // 0-1: year, 2:month, 3:day, 4:hour, 5:minutes, 6:second |
| 143 |
// 7:10msec |
| 144 |
}; |
| 145 |
|
| 146 |
// Operator function |
| 147 |
inline bool operator==(const b_time& lhs, const b_time& rhs) |
| 148 |
{ |
| 149 |
if ((lhs.m_tv[0] == rhs.m_tv[0]) || (lhs.m_tv[1] == rhs.m_tv[1]) || |
| 150 |
(lhs.m_tv[2] == rhs.m_tv[2]) || (lhs.m_tv[3] == rhs.m_tv[3]) || |
| 151 |
(lhs.m_tv[4] == rhs.m_tv[4]) || (lhs.m_tv[5] == rhs.m_tv[5]) || |
| 152 |
(lhs.m_tv[6] == rhs.m_tv[6])) |
| 153 |
{ |
| 154 |
return true; |
| 155 |
} |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
inline bool operator<(const b_time& lhs, const b_time& rhs) |
| 160 |
{ |
| 161 |
u_int lt = (lhs.m_tv[0] << 24) + (lhs.m_tv[1] << 16) + |
| 162 |
(lhs.m_tv[2] << 8) + lhs.m_tv[3]; |
| 163 |
u_int rt = (rhs.m_tv[0] << 24) + (rhs.m_tv[1] << 16) + |
| 164 |
(rhs.m_tv[2] << 8) + rhs.m_tv[3]; |
| 165 |
if (lt < rt) |
| 166 |
return true; |
| 167 |
else if (lt > rt) |
| 168 |
return false; |
| 169 |
|
| 170 |
lt = (lhs.m_tv[4] << 16) + (lhs.m_tv[5] << 8) + lhs.m_tv[6]; |
| 171 |
rt = (rhs.m_tv[4] << 16) + (rhs.m_tv[5] << 8) + rhs.m_tv[6]; |
| 172 |
if (lt < rt) |
| 173 |
return true; |
| 174 |
else if (lt > rt) |
| 175 |
return false; |
| 176 |
|
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
#endif // B_TIME_H |