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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/********************************************************************************
* *
* D a t e C l a s s *
* *
*********************************************************************************
* Copyright (C) 2005,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXStream.h"
#include "FXDate.h"
/*
Notes:
- Henry F. Fliegel and Thomas C. Van Flandern, "A Machine Algorithm for
Processing Calendar Dates". CACM, Vol. 11, No. 10, October 1968, pp 657.
- Major clean up and simplification was done!
- Added week number calculations!
- Start of the JD count is 0 at 12 NOON 1 JAN -4712 (4713 BC).
- Reminder, MJD = JD - 2400000.5.
*/
using namespace FX;
/*******************************************************************************/
namespace FX {
// Many nanoseconds in a second
static const FXTime seconds=1000000000L;
// Julian day number of GPS week zero (Jan 6, 1980)
static const FXuint GPS_EPOCH_JDAY=2444245;
// Julian day number of UNIX epoch (Jan 1, 1970)
static const FXuint UNIX_EPOCH_JDAY=2440588;
// Julian day number of J2000 (Jan 1, 2000)
static const FXuint J2000_EPOCH_JDAY=2451545;
// UNIX time to GPS time offset in nanoseconds
static const FXTime UNIX_TO_GPS=315964800L*seconds;
// Short month names
const FXchar FXDate::shortMonthName[12][4]={
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
};
// Long month names
const FXchar FXDate::longMonthName[12][10]={
"January","February","March","April","May","June","July","August","September","October","November","December"
};
// Short week day name
const FXchar FXDate::shortWeekDay[7][4]={
"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
};
// Long week day name
const FXchar FXDate::longWeekDay[7][10]={
"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
};
// Number of days in nomimal month
static const FXuchar monthDays[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31
};
/*******************************************************************************/
// Initialize with year and day of year
FXDate::FXDate(FXint yr,FXint dy){
setDate(yr,dy);
}
// Initialize with year, month, and day of month
FXDate::FXDate(FXint yr,FXint mo,FXint dy){
setDate(yr,mo,dy);
}
// Set date to year and day of year
void FXDate::setDate(FXint yr,FXint dy){
if(dy<1 || dy>366){ fxerror("FXDate::setDate: bad argument.\n"); }
julian=(1461*(yr+4799))/4-(3*((yr+4899)/100))/4+dy-31739;
}
// Get year and day of year from date
void FXDate::getDate(FXint& yr,FXint& dy) const {
FXint l,n,i,j;
l=julian+68569;
n=(4*l)/146097;
l=l-(146097*n+3)/4;
i=(4000*(l+1))/1461001;
l=l-(1461*i)/4+31;
j=(80*l)/2447;
l=j/11;
yr=100*(n-49)+i+l;
dy=julian-(1461*(yr+4799))/4+(3*((yr+4899)/100))/4+31739;
}
// Set date to year, month, and day of month
void FXDate::setDate(FXint yr,FXint mo,FXint dy){
if(mo<1 || mo>12 || dy<1 || dy>31){ fxerror("FXDate::setDate: bad argument.\n"); }
julian=(1461*(yr+4800+(mo-14)/12))/4+(367*(mo-2-12*((mo-14)/12)))/12-(3*((yr+4900+(mo-14)/12)/100))/4+dy-32075;
}
// Get year, month, and day of month from date
void FXDate::getDate(FXint& yr,FXint& mo,FXint& dy) const {
FXint l,n,i,j;
l=julian+68569;
n=(4*l)/146097;
l=l-(146097*n+3)/4;
i=(4000*(l+1))/1461001;
l=l-(1461*i)/4+31;
j=(80*l)/2447;
dy=l-(2447*j)/80;
l=j/11;
mo=j+2-(12*l);
yr=100*(n-49)+i+l;
}
// Set date from nanoseconds since 1/1/1970
// Technically, Julian Day starts at noon; however we truncate
// incoming time to 00:00:00.
void FXDate::setTime(FXTime ns){
const FXTime days=86400L*seconds;
julian=UNIX_EPOCH_JDAY+(FXuint)(((0<=ns)?ns:ns-days+1)/days);
}
// Get nanoseconds since 1/1/1970 from date
// Return time in nanoseconds at start of the day
FXTime FXDate::getTime() const {
const FXTime days=86400L*seconds;
return ((FXTime)julian-(FXTime)UNIX_EPOCH_JDAY)*days;
}
// is value a leap year?
FXbool FXDate::leapYear(FXint yr){
return ((yr%4==0) && (yr%100!=0)) || (yr%400==0);
}
// Return number of days in a given year
FXint FXDate::daysInYear(FXint yr){
return leapYear(yr) ? 366 : 365;
}
// Return number of days in the month in given year, month
FXint FXDate::daysInMonth(FXint yr,FXint mo){
return (mo==2 && leapYear(yr)) ? 29 : monthDays[mo];
}
// Return day of the month
FXint FXDate::day() const {
FXint yr,mo,dy;
getDate(yr,mo,dy);
return dy;
}
// Return month
FXint FXDate::month() const {
FXint yr,mo,dy;
getDate(yr,mo,dy);
return mo;
}
// Return year
FXint FXDate::year() const {
FXint yr,mo,dy;
getDate(yr,mo,dy);
return yr;
}
// Return day of the week
FXint FXDate::dayOfWeek() const {
return (julian+1)%7; // Sunday is day 0 of week
}
// Return true if leap year
FXbool FXDate::leapYear() const {
return leapYear(year());
}
// Return number of days in this year
FXint FXDate::daysInYear() const {
return daysInYear(year());
}
// Return days in this month
FXint FXDate::daysInMonth() const {
FXint yr,mo,dy;
getDate(yr,mo,dy);
return daysInMonth(yr,mo);
}
// Return day of year
FXint FXDate::dayOfYear() const {
FXDate s(year(),1);
return julian-s.julian+1;
}
// Return ISO8601 week number of this date
FXint FXDate::weekOfYear() const {
FXint d4=(((julian+31741-julian%7)%146097)%36524)%1461;
FXint L=d4/1460;
FXint d1=(d4-L)%365+L;
return 1+d1/7;
}
// Add d days to this date
FXDate& FXDate::addDays(FXint d){
julian+=d;
return *this;
}
// Add m months to this date; day of month is adjusted for leap-years
FXDate& FXDate::addMonths(FXint m){
FXint yr,mo,dy,mx;
getDate(yr,mo,dy);
if(0<=m){
yr=yr+(mo-1+m)/12;
mo=1+(mo-1+m)%12;
}
else{
yr=yr+(mo-12+m)/12;
mo=1+(mo+2147483627+m)%12;
}
mx=daysInMonth(yr,mo);
if(dy>mx) dy=mx;
setDate(yr,mo,dy);
return *this;
}
// Add y years to this date; day of month is adjusted for leap-years
FXDate& FXDate::addYears(FXint y){
FXint yr,mo,dy;
getDate(yr,mo,dy);
yr+=y;
if(dy>28 && mo==2 && !leapYear(yr)) dy=28;
setDate(yr,mo,dy);
return *this;
}
// Return current local date
FXDate FXDate::localDate(){
#if defined(WIN32)
SYSTEMTIME t;
GetLocalTime(&t);
return FXDate(t.wYear,t.wMonth,t.wDay);
#elif defined(HAVE_LOCALTIME_R)
struct tm result,*t;
time_t ltime;
time(<ime);
t=localtime_r(<ime,&result);
return FXDate(t->tm_year+1900,t->tm_mon+1,t->tm_mday);
#else
struct tm *t;
time_t ltime;
time(<ime);
t=localtime(<ime);
return FXDate(t->tm_year+1900,t->tm_mon+1,t->tm_mday);
#endif
}
// Return current universal (UTC) date
FXDate FXDate::universalDate(){
#if defined(WIN32)
SYSTEMTIME t;
GetSystemTime(&t);
return FXDate(t.wYear,t.wMonth,t.wDay);
#elif defined(HAVE_GMTIME_R)
struct tm result,*t;
time_t ltime;
time(<ime);
t=gmtime_r(<ime,&result);
return FXDate(t->tm_year+1900,t->tm_mon+1,t->tm_mday);
#else
struct tm *t;
time_t ltime;
time(<ime);
t=gmtime(<ime);
return FXDate(t->tm_year+1900,t->tm_mon+1,t->tm_mday);
#endif
}
// save to store
FXStream& operator<<(FXStream& store,const FXDate& d){
store << d.julian;
return store;
}
// load from store
FXStream& operator>>(FXStream& store,FXDate& d){
store >> d.julian;
return store;
}
}
|