[go: up one dir, main page]

Menu

[r110]: / trunk / core / time.cpp  Maximize  Restore  History

Download this file

210 lines (173 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
///////////////////////////////////////////////////////////////////////////////
//
// File : $Id$
// Subject : IBPP, Time class implementation
//
///////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright 2000-2007 T.I.P. Group S.A. and the IBPP Team (www.ibpp.org)
//
// The contents of this file are subject to the IBPP License (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at http://www.ibpp.org or in the 'license.txt'
// file which must have been distributed along with this file.
//
// This software, distributed under the License, is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
///////////////////////////////////////////////////////////////////////////////
//
// COMMENTS
// * Tabulations should be set every four characters when editing this file.
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
#pragma warning(disable: 4786 4996)
#ifndef _DEBUG
#pragma warning(disable: 4702)
#endif
#endif
#include "_ibpp.h"
#ifdef HAS_HDRSTOP
#pragma hdrstop
#endif
#include <time.h> // Can't use <ctime> thanks to MSVC6 buggy library
using namespace ibpp_internals;
void IBPP::Time::Now()
{
time_t systime = time(0);
tm* loctime = localtime(&systime);
IBPP::itot(&mTime, loctime->tm_hour, loctime->tm_min, loctime->tm_sec, 0);
}
void IBPP::Time::SetTime(int t)
{
if (t < 0 || t > 863999999)
throw LogicExceptionImpl("Time::SetTime", _("Invalid time value"));
mTime = t;
}
void IBPP::Time::SetTime(int hour, int minute, int second, int tenthousandths)
{
if (hour < 0 || hour > 23 ||
minute < 0 || minute > 59 ||
second < 0 || second > 59 ||
tenthousandths < 0 || tenthousandths > 9999)
throw LogicExceptionImpl("Time::SetTime",
_("Invalid hour, minute, second values"));
IBPP::itot(&mTime, hour, minute, second, tenthousandths);
}
void IBPP::Time::GetTime(int& hour, int& minute, int& second) const
{
IBPP::ttoi(mTime, &hour, &minute, &second, 0);
}
void IBPP::Time::GetTime(int& hour, int& minute, int& second, int& tenthousandths) const
{
IBPP::ttoi(mTime, &hour, &minute, &second, &tenthousandths);
}
int IBPP::Time::Hours() const
{
int hours;
IBPP::ttoi(mTime, &hours, 0, 0, 0);
return hours;
}
int IBPP::Time::Minutes() const
{
int minutes;
IBPP::ttoi(mTime, 0, &minutes, 0, 0);
return minutes;
}
int IBPP::Time::Seconds() const
{
int seconds;
IBPP::ttoi(mTime, 0, 0, &seconds, 0);
return seconds;
}
int IBPP::Time::SubSeconds() const // Actually tenthousandths of seconds
{
int tenthousandths;
IBPP::ttoi(mTime, 0, 0, 0, &tenthousandths);
return tenthousandths;
}
IBPP::Time::Time(int hour, int minute, int second, int tenthousandths)
{
SetTime(hour, minute, second, tenthousandths);
}
IBPP::Time::Time(const IBPP::Time& copied)
{
mTime = copied.mTime;
}
IBPP::Time& IBPP::Time::operator=(const IBPP::Timestamp& assigned)
{
mTime = assigned.GetTime();
return *this;
}
//lint -e{1529} no need to test for assignment to this here
IBPP::Time& IBPP::Time::operator=(const IBPP::Time& assigned)
{
mTime = assigned.mTime;
return *this;
}
// Time calculations. Internal format is the number of seconds elapsed since
// midnight. Splits such a time in its hours, minutes, seconds components.
void IBPP::ttoi(int itime, int *h, int *m, int *s, int* t)
{
int hh, mm, ss, tt;
hh = (int) (itime / 36000000); itime = itime - hh * 36000000;
mm = (int) (itime / 600000); itime = itime - mm * 600000;
ss = (int) (itime / 10000);
tt = (int) (itime - ss * 10000);
if (h != 0) *h = hh;
if (m != 0) *m = mm;
if (s != 0) *s = ss;
if (t != 0) *t = tt;
return;
}
// Get the internal time format, given hour, minute, second.
void IBPP::itot (int *ptime, int hour, int minute, int second, int tenthousandths)
{
*ptime = hour * 36000000 + minute * 600000 + second * 10000 + tenthousandths;
return;
}
namespace ibpp_internals
{
//
// The following functions are helper conversions functions between IBPP
// Date, Time, Timestamp and ISC_DATE, ISC_TIME and ISC_TIMESTAMP.
// (They must be maintained if the encoding used by Firebird evolve.)
// These helper functions are used from row.cpp and from array.cpp.
//
void encodeDate(ISC_DATE& isc_dt, const IBPP::Date& dt)
{
// There simply has a shift of 15019 between the native Firebird
// date model and the IBPP model.
isc_dt = (ISC_DATE)(dt.GetDate() + 15019);
}
void decodeDate(IBPP::Date& dt, const ISC_DATE& isc_dt)
{
// There simply has a shift of 15019 between the native Firebird
// date model and the IBPP model.
dt.SetDate((int)isc_dt - 15019);
}
void encodeTime(ISC_TIME& isc_tm, const IBPP::Time& t)
{
isc_tm = (ISC_TIME)t.GetTime();
}
void decodeTime(IBPP::Time& t, const ISC_TIME& isc_tm)
{
t.SetTime((int)isc_tm);
}
void encodeTimestamp(ISC_TIMESTAMP& isc_ts, const IBPP::Timestamp& ts)
{
encodeDate(isc_ts.timestamp_date, ts);
encodeTime(isc_ts.timestamp_time, ts);
}
void decodeTimestamp(IBPP::Timestamp& ts, const ISC_TIMESTAMP& isc_ts)
{
decodeDate(ts, isc_ts.timestamp_date);
decodeTime(ts, isc_ts.timestamp_time);
}
}
//
// EOF
//