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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
* Grace - GRaphing, Advanced Computation and Exploration of data
*
* Home page: http://plasma-gate.weizmann.ac.il/Grace/
*
* Copyright (c) 1996-2005 Grace Development Team
*
* Maintained by Evgeny Stambulchik
*
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* date and time conversion functions */
/*
* We use two calendars here : the one that was established in 532 by
* Denys and lasted until 1582, and the one that was created by Luigi
* Lilio (Alyosius Lilius) and Christoph Klau (Christophorus Clavius)
* for pope Gregorius XIII. Both use the same months (they were
* introduced under emperor Augustus, a few years after julian
* calendar introduction, both Julius and Augustus were honored by a
* month beeing named after each one).
* The leap years occured regularly in Denys's calendar : once every
* four years, there is no year 0 in this calendar (the leap year -1
* was just before year 1). This calendar was not compliant with earth
* motion and the dates were slowly shifting with regard to
* astronomical events.
* This was corrected in 1582 by introducing gregorian calendar. First
* a ten days shift was introduced to reset correct dates (Thursday
* October the 4th was followed by Friday October the 15th). The rules
* for leap years were also changed : three leap years are removed
* every four centuries. These years are those that are multiple of
* 100 but not multiple of 400 : 1700, 1800, and 1900 were not leap
* years, but 1600 and 2000 were (will be) leap years.
* We still use gregorian calendar today, but we now have several time
* scales for increased accuracy. The International Atomic Time is a
* linear scale : the best scale to use for scientific reference. The
* Universal Time Coordinate (often confused with Greenwhich Mean
* Time) is a legal time that is almost synchronized with earth
* motion. However, since the earth is slightly slowing down, leap
* seconds are introduced from time to time in UTC (about one second
* every 18 monthes). UTC is not a continuous scale ! When a leap
* second is introduced by International Earth Rotation Service, this
* is published in advance and the legal time sequence is as follows :
* 23:59:59 followed one second later by 23:59:60 followed one second
* later by 00:00:00. At the time of this writing (1999-01-05) the
* difference between IAT and UTC was 32 seconds, and the last leap
* second was introduced in 1998-12-31.
* These calendars allow to represent any date from the mist of the
* past to the fog of the future, but they are not convenient for
* computation. Another time scale is of possible : counting only the
* days from a reference. Such a time scale was introduced by
* Joseph-Juste Scaliger (Josephus Justus Scaliger) in 1583. He
* decided to use "-4713-01-01T12:00:00" as a reference date because
* it was at the same time a monday, first of January of a leap year,
* there was an exact number of 19 years Meton cycle between this date
* and year 1 (for Easter computation), and it was at the beginning of
* a 15 years "roman indiction" cycle. The day number is called
* "julian day", but it has really nothing to do with the julian
* calendar.
* In Grace, we consider both julian days and calendar dates, and do
* not consider leap seconds. The following routines are used to parse
* the dates according to these formats and to convert between
* them. If you find yourself in a situation were you need UTC, a very
* precise scale, and should take into account leap seconds ... you
* should convert your data yourself (for example using International
* Atomic Time). But if you bother with that you probably already know
* what to do ;-)
* Luc */
#include "grace/baseP.h"
/*
* set of functions to convert julian calendar elements
* with negative years to julian day
*/
static int neg_julian_non_leap (int year)
{
/* one leap year every four years, leap years : -4713, -4709, ..., -5, -1 */
return (3 - year) & 3;
}
static long neg_julian_cal_to_jul(int y, int m, int d)
{
/* day 0 : -4713-01-01
* day 1721423 : -1-12-31
*/
return (1461L*(y + 1L))/4L
+ (m*489)/16 - ((m > 2) ? (neg_julian_non_leap(y) ? 32L : 31L) : 30L)
+ d + 1721057L;
}
static int neg_julian_year_estimate(long n)
{
/* year bounds : 4n - 6887153 <= 1461y <= 4n - 6885693
* lower bound reached 31st December of leap years
* upper bound reached 1st January of leap years
* the lower bound gives a low estimate of the year
*/
return (int) ((4L*n - 6887153L)/1461L);
}
/*
* set of functions to convert julian calendar elements
* with positive years to julian day
*/
static int pos_julian_non_leap(int year)
{
/* one leap year every four years, leap years : 4, 8, ..., 1576, 1580 */
return year & 3;
}
static long pos_julian_cal_to_jul(int y, int m, int d)
{
/* day 1721424 : 1-01-01
* day 2299160 : 1582-10-04
*/
return (1461L*(y -1L))/4L
+ (m*489)/16 - ((m > 2) ? (pos_julian_non_leap(y) ? 32L : 31L) : 30L)
+ d + 1721423L;
}
static int pos_julian_year_estimate(long n)
{
/* year bounds : 4n - 6885692 <= 1461y <= 4n - 6884232
* lower bound reached 31st December of leap years
* upper bound reached 1st January of leap years
* the lower bound gives a low estimate of the year
*/
int y = (int) ((4L*n - 6885692L)/1461L);
/* make sure we stay in the positive model even with our underestimate */
return (y < 1) ? 1 : y;
}
/*
* set of functions to convert gregorian calendar elements to julian day
*/
static int gregorian_non_leap(int year)
{
/* one leap year every four years, except for multiple of 100 that
* are not also multiple of 400 (so 1600, 1896, 1904, and 2000 are
* leap years, but 1700, 1800 and 1900 are non leap years
*/
return (year & 3) || ((year % 100) == 0 && ((year/100 & 3)));
}
static long gregorian_cal_to_jul(int y, int m, int d)
{
long c;
/* day 2299161 : 1582-10-15 */
c = (long) ((y - 1)/100);
return (1461L*(y - 1))/4 + c/4 - c
+ (m*489)/16 - ((m > 2) ? (gregorian_non_leap(y) ? 32L : 31L) : 30L)
+ d + 1721425L;
}
static int gregorian_year_estimate(long n)
{
/*
* year bounds : 400n - 688570288 <= 146097y <= 400n - 688423712
* lower bound reached on : 1696-12-31, 2096-12-31, 2496-12-31 ...
* upper bound reached on : 1904-01-01, 2304-01-01, 2704-01-01 ...
* the lower bound gives a low estimate of the year
*/
return (int) ((400L*n - 688570288L)/146097L);
}
/*
* convert calendar elements to Julian day
*/
long cal_to_jul(int y, int m, int d)
{
long n;
n = gregorian_cal_to_jul(y, m, d);
if (n < 2299161L) {
/* the date belongs to julian calendar */
n = (y < 0)
? neg_julian_cal_to_jul(y, m, d)
: pos_julian_cal_to_jul(y, m, d);
}
return n;
}
/*
* convert julian day to calendar elements
*/
static void jul_to_some_cal(long n,
int (*some_non_leap) (int),
long (*some_cal_to_jul) (int, int, int),
int (*some_year_estimate) (long),
int *y, int *m, int *d)
{
int non_leap, day_of_year, days_until_end_of_year;
/* lower estimation of year */
*y = some_year_estimate(n);
non_leap = some_non_leap(*y);
days_until_end_of_year = (int) (some_cal_to_jul(*y, 12, 31) - n);
while (days_until_end_of_year < 0) {
/* correction of the estimate */
(*y)++;
non_leap = some_non_leap(*y);
days_until_end_of_year += non_leap ? 365 : 366;
}
day_of_year = (non_leap ? 365 : 366) - days_until_end_of_year;
/* estimate of the month : one too high only on last days of January */
*m = (16*(day_of_year + (non_leap ? 32 : 31))) / 489;
/* day of month */
*d = day_of_year
- (*m*489)/16 + ((*m > 2) ? (non_leap ? 32 : 31) : 30);
if (*d < 1) {
/* no luck, our estimate is false near end of January */
*m = 1;
*d += 31;
}
}
/*
* convert julian day to calendar elements
*/
void jul_to_cal(long n, int *y, int *m, int *d)
{
if (n < 1721424L) {
jul_to_some_cal(n, neg_julian_non_leap,
neg_julian_cal_to_jul, neg_julian_year_estimate,
y, m, d);
} else if (n < 2299161L) {
jul_to_some_cal(n, pos_julian_non_leap,
pos_julian_cal_to_jul, pos_julian_year_estimate,
y, m, d);
} else {
jul_to_some_cal(n, gregorian_non_leap,
gregorian_cal_to_jul, gregorian_year_estimate,
y, m, d);
}
}
/*
* convert julian day and hourly elements to julian day
*/
double jul_and_time_to_jul(long jul, int hour, int min, double sec)
{
return ((double) jul)
+ (((double) (((hour - 12)*60 + min)*60)) + sec)/86400.0;
}
/*
* convert calendar and hourly elements to julian day
*/
double cal_and_time_to_jul(int y, int m, int d,
int hour, int min, double sec)
{
return jul_and_time_to_jul (cal_to_jul(y, m, d), hour, min, sec);
}
/*
* convert julian day to calendar and hourly elements
*/
void jul_to_cal_and_time(double jday, int rounding,
int *y, int *m, int *d,
int *hour, int *min, int *sec)
{
long n;
double tmp;
/* find the time of the day */
n = (long) floor(jday + 0.5);
tmp = 24.0*(jday + 0.5 - n);
*hour = (int) floor(tmp);
tmp = 60.0*(tmp - *hour);
*min = (int) floor(tmp);
tmp = 60.0*(tmp - *min);
*sec = (int) floor(tmp + 0.5);
/* perform some rounding */
if (*sec >= 60 || rounding > ROUND_SECOND) {
/* we should round to at least nearest minute */
if (*sec >= 30) {
(*min)++;
}
*sec = 0;
if (*min == 60 || rounding > ROUND_MINUTE) {
/* we should round to at least nearest hour */
if (*min >= 30) {
(*hour)++;
}
*min = 0;
if (*hour == 24 || rounding > ROUND_HOUR) {
/* we should round to at least nearest day */
if (*hour >= 12) {
n++;
}
*hour = 0;
}
}
}
/* now find the date */
jul_to_cal(n, y, m, d);
/* perform more rounding */
if (rounding == ROUND_MONTH) {
int m2, y2;
if (*m < 12) {
m2 = *m + 1;
y2 = *y;
} else {
m2 = 1;
y2 = *y + 1;
}
if ((cal_to_jul(y2, m2, 1) - n) <= (n - cal_to_jul(*y, *m, 1))) {
*m = m2;
*y = y2;
}
*d = 1;
}
}
|