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
|
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* hdate_hdate.c: convert georgean and hebrew calendars.
*
* Author:
* Yaacov Zamir <kzamir@walla.co.il>
*
* 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.
*/
#include <gnumeric-config.h>
#include <gnumeric.h>
#include <stdio.h>
#include "hdate.h"
/* constants, in 1/18th of minute */
#define HOUR 1080
#define DAY (24*HOUR)
#define WEEK (7*DAY)
#define M(h,p) ((h)*HOUR+p)
#define MONTH (DAY+M(12,793))
/**
@brief Return the days from the start
@param y The years
@warning internal function.
*/
int
hdate_days_from_start (int y)
{
int m, nm, dw, s, l;
l = y * 7 + 1; /* no. of leap months */
m = y * 12 + l / 19; /* total no. of months */
l %= 19;
nm = m * MONTH + M (1 + 6, 779); /* molad new year 3744 (16BC) + 6 hours */
s = m * 28 + nm / DAY - 2;
nm %= WEEK;
dw = nm / DAY;
nm %= DAY;
/* special cases of Molad Zaken */
if ((l < 12 && dw == 3 && nm >= M (9 + 6, 204)) ||
(l < 7 && dw == 2 && nm >= M (15 + 6, 589)))
s++, dw++;
/* ADU */
if (dw == 1 || dw == 4 || dw == 6)
s++;
return s;
}
/**
@brief compute hebrew date from gregorian date
@param d Day of month 1..31
@param m Month 1..12 , if m or d is 0 return current date.
@param y Year in 4 digits e.g. 2001
*/
int
hdate_gdate_to_hdate (int d, int m, int y, int *hd, int *hm, int *hy)
{
int jd;
/* sanity checks */
if (!(m >= 1 && m <= 12) ||
!((d >= 1)
&& ((y >= 3000 && m == 6 && d <= 59)
|| (d <= 31))) || !(y > 0))
return 1;
/* end of cheking */
jd = hdate_gdate_to_jd (d, m, y);
hdate_jd_to_hdate (jd, hd, hm, hy);
return 0;
}
/**
@brief compute general date structure from hebrew date
@param d Day of month 1..31
@param m Month 0..13 , if m or d is 0 return current date.
@param y Year in 4 digits e.g. 5731
@attention no sanity cheak !!
*/
int
hdate_hdate_to_gdate (int d, int m, int y, int *gd, int *gm, int *gy)
{
int jd;
/* sanity checks */
if (!(m >= 1 && m <= 12) ||
!((d >= 1)
&& ((y >= 3000 && m == 6 && d <= 59)
|| (d <= 31))) || !(y > 0))
return 1;
/* end of cheking */
jd = hdate_hdate_to_jd (d, m, y);
hdate_jd_to_gdate (jd, gd, gm, gy);
return 0;
}
/**
@brief Compute Julian day (jd from Gregorian day, month and year (d, m, y)
Algorithm from 'Julian and Gregorian Day Numbers' by Peter Meyer
@author Yaacov Zamir ( algorithm from Henry F. Fliegel and Thomas C. Van Flandern ,1968)
@param d Day of month 1..31
@param m Month 1..12
@param y Year in 4 digits e.g. 2001
*/
int
hdate_gdate_to_jd (int d, int m, int y)
{
int jd;
jd = (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
(367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
(3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
return jd;
}
/**
@brief Converting from the Julian day to the Gregorian day
Algorithm from 'Julian and Gregorian Day Numbers' by Peter Meyer
@author Yaacov Zamir ( Algorithm, Henry F. Fliegel and Thomas C. Van Flandern ,1968)
@param jd Julian day
@param d Return Day of month 1..31
@param m Return Month 1..12
@param y Return Year in 4 digits e.g. 2001
*/
void
hdate_jd_to_gdate (int jd, int *d, int *m, int *y)
{
int l, n, i, j;
l = jd + 68569;
n = (4 * l) / 146097;
l = l - (146097 * n + 3) / 4;
i = (4000 * (l + 1)) / 1461001; /* that's 1,461,001 */
l = l - (1461 * i) / 4 + 31;
j = (80 * l) / 2447;
*d = l - (2447 * j) / 80;
l = j / 11;
*m = j + 2 - (12 * l);
*y = 100 * (n - 49) + i + l; /* that's a lower-case L */
}
/**
@brief Converting from the Julian day to the Hebrew day
@author Amos Shapir 1984 (rev. 1985, 1992) Yaacov Zamir 2003-2005
@param jd Julian day
@param d Return Day of month 1..31
@param m Return Month 1..14
@param y Return Year in 4 digits e.g. 2001
*/
void
hdate_jd_to_hdate (int jd, int *d, int *m, int *y)
{
int s;
int l, n, i, j;
l = jd + 68569;
n = (4 * l) / 146097;
l = l - (146097 * n + 3) / 4;
i = (4000 * (l + 1)) / 1461001; /* that's 1,461,001 */
l = l - (1461 * i) / 4 + 31;
j = (80 * l) / 2447;
l = j / 11;
*y = 100 * (n - 49) + i + l; /* that's a lower-case L */
*d = jd - 1715119; /* julean to days since 1 Tishrei 3744 */
*y += 16;
s = hdate_days_from_start (*y);
*m = hdate_days_from_start (*y + 1);
while (*d >= *m)
{ /* computed year was underestimated */
s = *m;
(*y)++;
*m = hdate_days_from_start (*y + 1);
}
*d -= s;
s = *m - s; /* size of current year */
*y += 3744;
/* compute day and month */
if (*d >= s - 236)
{ /* last 8 months are regular */
*d -= s - 236;
*m = *d * 2 / 59;
*d -= (*m * 59 + 1) / 2;
*m += 4;
if (s > 365 && *m <= 5) /* Adar of Meuberet */
*m += 8;
}
else
{
/* first 4 months have 117-119 days */
s = 114 + s % 10;
*m = *d * 4 / s;
*d -= (*m * s + 3) / 4;
}
}
/**
@brief Compute Julian day (jd from Hebrew day, month and year (d, m, y)
@author Amos Shapir 1984 (rev. 1985, 1992) Yaacov Zamir 2003-2005
@param d Day of month 1..31
@param m Month 1..14
@param y Year in 4 digits e.g. 5753
*/
int
hdate_hdate_to_jd (int d, int m, int y)
{
int s;
y -= 3744;
s = hdate_days_from_start (y);
d += s;
s = hdate_days_from_start (y + 1) - s; /* length of year */
if (m == 13)
{
m = 6;
}
if (m == 14)
{
m = 6;
d += 30;
}
d += (59 * (m - 1) + 1) / 2; /* regular months */
/* special cases */
if (s % 10 > 4 && m > 2) /* long Heshvan */
d++;
if (s % 10 < 4 && m > 3) /* short Kislev */
d--;
if (s > 365 && m > 6) /* leap year */
d += 30;
d -= 6002;
y = (d + 36525) * 4 / 146097 - 1;
d -= y / 4 * 146097 + (y % 4) * 36524;
d += 1715119; /* days since 1 Tishrei 3744 to julean */
return d;
}
|