File: common.c

package info (click to toggle)
hebcal 4.31-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 7,760 kB
  • sloc: ansic: 6,361; sh: 535; perl: 134; makefile: 60
file content (407 lines) | stat: -rw-r--r-- 9,600 bytes parent folder | download | duplicates (2)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
   Hebcal - A Jewish Calendar Generator
   Copyright (C) 1994-2004  Danny Sadinoff
   Portions Copyright (c) 2002 Michael J. Radwin. All Rights Reserved.

   https://github.com/hebcal/hebcal

   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.

   Danny Sadinoff can be reached at

   danny@sadinoff.com
 */

#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "common.h"
#include "greg.h"
#include "hebcal.h"

hmonths_t hMonths =
{
    {
        {"VOID"},
        {"Nisan"},
        {"Iyyar"},
        {"Sivan"},
        {"Tamuz"},
        {"Av"},
        {"Elul"},
        {"Tishrei"},
        {"Cheshvan"},
        {"Kislev"},
        {"Tevet"},
        {"Sh'vat"},
        {"Adar"},
        {"Nisan"}
    },
    {
        {"VOID"},
        {"Nisan"},
        {"Iyyar"},
        {"Sivan"},
        {"Tamuz"},
        {"Av"},
        {"Elul"},
        {"Tishrei"},
        {"Cheshvan"},
        {"Kislev"},
        {"Tevet"},
        {"Sh'vat"},
        {"Adar I"},
        {"Adar II"},
        {"Nisan"}
    }
};

int max_days_in_heb_month (int month, int year)
{
    if ( month == IYYAR ||
         month == TAMUZ ||
         month == ELUL ||
         month == TEVET ||
         month == ADAR_II ||
         (month == ADAR_I && !LEAP_YR_HEB( year )) ||
         (month == CHESHVAN && !long_cheshvan( year )) ||
         (month == KISLEV && short_kislev( year )))
        return 29;
    else
        return 30;
}

int lookup_hebrew_month( const char *s )
{
  /* the hebrew months are unique to their second letter
     N         nisan  (november?)
     I         iyyar
     E        Elul
     C        Cheshvan
     K        Kislev
     1        1Adar
     2        2Adar
     Si Sh     sivan, Shvat
     Ta Ti Te Tamuz, Tishrei, Tevet
     Av Ad    Av, Adar
   */
    switch ( tolower( *s++ ))
    {
    case 'n':
        return (tolower (*s) == 'o') ?	/* this catches "november" */
            0
            : NISAN;
    case 'i':
        return IYYAR;
    case 'e':
        return ELUL;
    case 'c':
        return CHESHVAN;
    case 'k':
        return KISLEV;
    case 's':
        switch (tolower (*s))
        {
        case 'i':
            return SIVAN;
        case 'h':
            return SHVAT;
        default:
            return 0;
        }
    case 't':
        switch (tolower (*s))
        {
        case 'a':
            return TAMUZ;
        case 'i':
            return TISHREI;
        case 'e':
            return TEVET;
        default:
            return 0;
        }
    case 'a':
        switch (tolower (*s))
        {
        case 'v':
            return AV;
        case 'd':
            for (; *s; s++)
            {			/* scan for a 1. */
                if (*s == '1')
                    return ADAR_I;
            }
            return ADAR_II;	/* else assume sheni */
        default:
            return 0;
        }
    default:
        return 0;
    }
}




/*
   returns day of week, hours and chalakim of specified molad.
 */
molad_t get_molad( int year, int month)
{
    molad_t retMolad;

    long yearl, m_elapsed, p_elapsed, h_elapsed, parts, m_adj;

    m_adj = (long) month;
    m_adj -= 7;
    if (m_adj < 0) m_adj += MONTHS_IN_HEB(year);

    yearl = (long) year;
    m_elapsed = m_adj +
        235L * ((yearl - 1L) / 19L) +
    12L * ((yearl - 1L) % 19) +
        ((((yearl - 1L) % 19L) * 7) + 1L) / 19L;

    p_elapsed = 204L + (793L * (m_elapsed % 1080));

    h_elapsed = 5L + (12L * m_elapsed) +
        793L * (m_elapsed / 1080L) +
        p_elapsed / 1080L -
        6L;

    parts = (p_elapsed % 1080) + 1080 * (h_elapsed % 24);

    retMolad.day = 1L + 29L * m_elapsed + h_elapsed / 24L;
    retMolad.hour = (int) (h_elapsed % 24L);
    retMolad.chalakim = (int) (parts % 1080L);

    return retMolad;

}


date_t abs2hebrew( long d )
{
    static int mmap[] =
        {
            KISLEV, TEVET, SHVAT, ADAR_I, NISAN,
            IYYAR, SIVAN, TAMUZ,
            TISHREI, TISHREI, TISHREI, CHESHVAN
        };
    date_t hebdate, gregdate;
    int day, month, year;

    if( d >= 10555144L )
    {
        fprintf(stderr, "parameter to abs2hebrew  %ld out of range\n",
                d );
        exit(1);
    }

    gregdate = abs2greg (d);
    hebdate.dd = 1;
    hebdate.mm = 7;
    year = 3760 + gregdate.yy;

    while (hebdate.yy = year + 1,
           d >= hebrew2abs (hebdate))
        year++;

    if( year >= 4635 && year < 10666  )
    {
        /* optimize search */
        month = mmap[gregdate.mm - 1];
    }
    else
    {
        /* we're outside the usual range, so assume nothing about hebrew/gregorian calendar drift... */
        month = TISHREI;
    }

    while (hebdate.mm = month,
           hebdate.dd = max_days_in_heb_month (month, year),
           hebdate.yy = year,
           d > hebrew2abs (hebdate))
        month = (month % MONTHS_IN_HEB (year)) + 1;

    hebdate.dd = 1;

    day = (int) (d - hebrew2abs (hebdate) + 1L);
    if( day < 0)
    {
        fprintf(stderr, "assertion failure d < hebrew2abs(m,d,y) => %ld < %ld!\n",
                d, hebrew2abs(hebdate));
        exit(1);
    }

    hebdate.dd = day;

    return hebdate;
}


/* Days from sunday prior to start of hebrew calendar to mean
   conjunction of tishrei in hebrew YEAR
 */
static long int hebrew_elapsed_days (int year)
{
    long int yearl, m_elapsed, p_elapsed, h_elapsed, parts, day, alt_day;

    yearl = (long) year;
    m_elapsed = 235L * ((yearl - 1L) / 19L) +
        12L * ((yearl - 1L) % 19L) +
        ((((yearl - 1L) % 19L) * 7L) + 1L) / 19L;

    p_elapsed = 204L + (793L * (m_elapsed % 1080L));

    h_elapsed = 5L + (12L * m_elapsed) +
        793L * (m_elapsed / 1080L) +
        p_elapsed / 1080L;

    parts = (p_elapsed % 1080L) + 1080L * (h_elapsed % 24L);

    day = 1L + 29L * m_elapsed + h_elapsed / 24L;

    if ((parts >= 19440L) ||
        ((2L == (day % 7L)) && (parts >= 9924L) && !(LEAP_YR_HEB (year))) ||
        ((1L == (day % 7L)) && (parts >= 16789L) && LEAP_YR_HEB (year - 1)))
        alt_day = day + 1L;
    else
        alt_day = day;

    if ((alt_day % 7L) == 0L ||
        (alt_day % 7L) == 3L ||
        (alt_day % 7L) == 5L)
        return alt_day + 1L;
    else
        return alt_day;
}



/* convert hebrew date to absolute date */
/*Absolute date of Hebrew DATE.
   The absolute date is the number of days elapsed since the (imaginary)
   Gregorian date Sunday, December 31, 1 BC. */
long hebrew2abs (date_t d)
{
    int m;
    long tempabs = (long) d.dd;
    long ret;

    if (d.mm < TISHREI)
    {
        for (m = TISHREI; m <= MONTHS_IN_HEB (d.yy); m++)
            tempabs += (long) max_days_in_heb_month (m, d.yy);

        for (m = NISAN; m < d.mm; m++)
            tempabs += (long) max_days_in_heb_month (m, d.yy);
    }
    else
    {
        for (m = TISHREI; m < d.mm; m++)
            tempabs += (long) max_days_in_heb_month (m, d.yy);
    }


    ret = hebrew_elapsed_days (d.yy) - 1373429L + tempabs;
    return ret;
}

/* Number of days in the hebrew YEAR */
int days_in_heb_year( int year )
{
    return (int) (hebrew_elapsed_days (year + 1) - hebrew_elapsed_days (year));
}

/* true if Cheshvan is long in hebrew YEAR */
int long_cheshvan( int year )
{
    return ((days_in_heb_year (year) % 10) == 5);
}

/* true if Kislev is short in hebrew YEAR */
int short_kislev( int year )
{
    return ((days_in_heb_year (year) % 10) == 3);
}

const char *num2heb(int num) {
    switch(num) {
    case 1: return "א";
    case 2: return "ב";
    case 3: return "ג";
    case 4: return "ד";
    case 5: return "ה";
    case 6: return "ו";
    case 7: return "ז";
    case 8: return "ח";
    case 9: return "ט";
    case 10: return "י";
    case 20: return "כ";
    case 30: return "ל";
    case 40: return "מ";
    case 50: return "נ";
    case 60: return "ס";
    case 70: return "ע";
    case 80: return "פ";
    case 90: return "צ";
    case 100: return "ק";
    case 200: return "ר";
    case 300: return "ש";
    case 400: return "ת";
    default: return "*INVALID*";
    }
}

#define GERESH "׳"
#define GERSHAYIM "״"

char *hebnum_to_string(char *buffer, int num) {
    int nums[10];
    int digits = 0;
    num = num % 1000;
    while (num > 0) {
        int i, incr = 100;
        if (num == 15 || num == 16) {
            nums[digits++] = 9;
            nums[digits++] = num - 9;
            break;
        }
        for (i = 400; i > num; i -= incr) {
            if (i == incr) {
                incr = incr / 10;
            }
        }
        nums[digits++] = i;
        num -= i;
    }
    buffer[0] = '\0';
    if (digits == 1) {
        const char *s = num2heb(nums[0]);
        strcat(buffer, s);
        strcat(buffer, GERESH);
        return buffer;
    }
    for (int i = 0; i < digits; i++) {
        const char *s = num2heb(nums[i]);
        if (i + 1 == digits) {
            strcat(buffer, GERSHAYIM);
        }
        strcat(buffer, s);
    }
    return buffer;
}