parsha.cpp
1 /***************************************************************************
2  * Copyright (C) 2003 by Jonathan Singer *
3  * jsinger@leeta.net *
4  * Calendar routines from Hebrew Calendar by Frank Yellin *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  ***************************************************************************/
11 #include "parsha.h"
12 #include <tdelocale.h>
13 
14 TQStringList Parsha::parshiot_names;
15 
16 Parsha::Parsha()
17 {
18 
19 }
20 
21 Parsha::~Parsha()
22 {
23 }
24 
25 TQString
26  Parsha::FindParshaName(int daynumber, int kvia, bool leap_p,
27  bool israel_p)
28 {
29 // The names of the Parshiot.
30  parshiot_names <<
31  i18n
32  ("These are weekly readings and do not have translations. They may have different spellings in your language; otherwise, just translate the sound to your characters",
33  "Bereshit") << i18n("Noach") << i18n("Lech L'cha") <<
34  i18n("Vayera") << i18n("Chaye Sarah") << i18n("Toldot") <<
35  i18n("Vayetze") << i18n("Vayishlach") << i18n("Vayeshev") <<
36  i18n("Miketz") << i18n("Vayigash") << i18n("Vayechi") <<
37  i18n("Shemot") << i18n("Vaera") << i18n("Bo") << i18n("Beshalach")
38  << i18n("Yitro") << i18n("Mishpatim") << i18n("Terumah") <<
39  i18n("Tetzaveh") << i18n("Ki Tisa") << i18n("Vayakhel") <<
40  i18n("Pekudei") << i18n("Vayikra") << i18n("Tzav") <<
41  i18n("Shemini") << i18n("Tazria") << i18n("Metzora") <<
42  i18n("Acharei Mot") << i18n("Kedoshim") << i18n("Emor") <<
43  i18n("Behar") << i18n("Bechukotai") << i18n("Bemidbar") <<
44  i18n("Naso") << i18n("Behaalotcha") << i18n("Shelach") <<
45  i18n("Korach") << i18n("Chukat") << i18n("Balak") <<
46  i18n("Pinchas") << i18n("Matot") << i18n("Masei") <<
47  i18n("Devarim") << i18n("Vaetchanan") << i18n("Ekev") <<
48  i18n("Reeh") << i18n("Shoftim") << i18n("Ki Tetze") <<
49  i18n("Ki Tavo") << i18n("Nitzavim") << i18n("Vayelech") <<
50  i18n("Haazinu");
51 
52 // Tables for each of the year types. XX indicates that it is a Holiday, and
53 // a special parsha is read that week. For some year types, Israel is different
54 // than the diaspora.
55 //
56 // The names indicate the day of the week on which Rosh Hashanah fell, whether
57 // it is a short/normal/long year (kvia=0,1,2), and whether it is a leap year.
58 // Some year types also have an _Israel version.
59 //
60 // Numbers are indices into the table above for a given week. Numbers > 100 indicate
61 // a double parsha. E.g. 150 means read both table entries 50 and 51.
62 //
63 // These tables were stolen (with some massaging) from the GNU code.
64 
65 #define XX 255
66  static unsigned const char Sat_short[] =
67  { XX, 52, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
68  11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 121, 23, 24, XX, 25,
69  126, 128, 30, 131, 33, 34, 35, 36, 37, 38, 39, 40, 141, 43, 44,
70  45, 46, 47, 48, 49, 50,
71  };
72 
73  static unsigned const char Sat_long[] =
74  { XX, 52, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
75  11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 121, 23, 24, XX, 25,
76  126, 128, 30, 131, 33, 34, 35, 36, 37, 38, 39, 40, 141, 43, 44,
77  45, 46, 47, 48, 49, 150,
78  };
79 
80  static unsigned const char Mon_short[] =
81  { 51, 52, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
82  12, 13, 14, 15, 16, 17, 18, 19, 20, 121, 23, 24, XX, 25, 126,
83  128, 30, 131, 33, 34, 35, 36, 37, 38, 39, 40, 141, 43, 44, 45,
84  46, 47, 48, 49, 150,
85  };
86 
87  static unsigned const char Mon_long[] = /* split */
88  { 51, 52, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
89  12, 13, 14, 15, 16, 17, 18, 19, 20, 121, 23, 24, XX, 25, 126,
90  128, 30, 131, 33, XX, 34, 35, 36, 37, 138, 40, 141, 43, 44, 45,
91  46, 47, 48, 49, 150,
92  };
93 
94 #define Mon_long_Israel Mon_short
95 
96 #define Tue_normal Mon_long
97 #define Tue_normal_Israel Mon_short
98 
99  static unsigned const char Thu_normal[] =
100  { 52, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
101  12, 13, 14, 15, 16, 17, 18, 19, 20, 121, 23, 24, XX, XX, 25,
102  126, 128, 30, 131, 33, 34, 35, 36, 37, 38, 39, 40, 141, 43, 44,
103  45, 46, 47, 48, 49, 50,
104  };
105  static unsigned const char Thu_normal_Israel[] =
106  { 52, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
107  12, 13, 14, 15, 16, 17, 18, 19, 20, 121, 23, 24, XX, 25, 126,
108  128, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 141, 43, 44,
109  45, 46, 47, 48, 49, 50,
110  };
111 
112  static unsigned const char Thu_long[] =
113  { 52, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
114  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, XX, 25,
115  126, 128, 30, 131, 33, 34, 35, 36, 37, 38, 39, 40, 141, 43, 44,
116  45, 46, 47, 48, 49, 50,
117  };
118 
119  static unsigned const char Sat_short_leap[] =
120  { XX, 52, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
121  11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
122  26, 27, XX, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
123  40, 141, 43, 44, 45, 46, 47, 48, 49, 150,
124  };
125 
126  static unsigned const char Sat_long_leap[] =
127  { XX, 52, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
128  11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
129  26, 27, XX, 28, 29, 30, 31, 32, 33, XX, 34, 35, 36, 37, 138,
130  40, 141, 43, 44, 45, 46, 47, 48, 49, 150,
131  };
132 
133 #define Sat_long_leap_Israel Sat_short_leap
134 
135  static unsigned const char Mon_short_leap[] =
136  { 51, 52, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
137  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
138  27, XX, 28, 29, 30, 31, 32, 33, XX, 34, 35, 36, 37, 138, 40,
139  141, 43, 44, 45, 46, 47, 48, 49, 150,
140  };
141  static unsigned const char Mon_short_leap_Israel[] =
142  { 51, 52, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
143  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
144  27, XX, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
145  141, 43, 44, 45, 46, 47, 48, 49, 150,
146  };
147 
148  static unsigned const char Mon_long_leap[] =
149  { 51, 52, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
150  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
151  27, XX, XX, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
152  40, 141, 43, 44, 45, 46, 47, 48, 49, 50,
153  };
154  static unsigned const char Mon_long_leap_Israel[] =
155  { 51, 52, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
156  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
157  27, XX, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
158  41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
159  };
160 
161 #define Tue_normal_leap Mon_long_leap
162 #define Tue_normal_leap_Israel Mon_long_leap_Israel
163 
164  static unsigned const char Thu_short_leap[] =
165  { 52, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
166  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
167  27, 28, XX, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
168  41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
169  };
170 
171  static unsigned const char Thu_long_leap[] =
172  { 52, XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
173  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
174  27, 28, XX, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
175  41, 42, 43, 44, 45, 46, 47, 48, 49, 150,
176  };
177 
178 /* Find the parsha for a given day of the year. daynumber is the day of the year.
179  * kvia and leap_p refer to the year type.
180  */
181 
182  int week = daynumber / 7; // week of the year
183  unsigned const char *array = NULL;
184  int index;
185 
186  // get the appropriate array by exhaustive search into the 14 year types. Since we
187  // know it's a Shabbat, we can find out what day Rosh Hashanah was on by looking
188  // at daynumber %7.
189  if (!leap_p)
190  {
191  switch (daynumber % 7)
192  {
193  case 1: /* RH was on a Saturday */
194  if (kvia == 0)
195  array = Sat_short;
196  else if (kvia == 2)
197  array = Sat_long;
198  break;
199  case 6: /* RH was on a Monday */
200  if (kvia == 0)
201  array = Mon_short;
202  else if (kvia == 2)
203  array = israel_p ? Mon_long_Israel : Mon_long;
204  break;
205  case 5: /* RH was on a Tueday */
206  if (kvia == 1)
207  array = israel_p ? Tue_normal_Israel : Tue_normal;
208  break;
209  case 3: /* RH was on a Thu */
210  if (kvia == 1)
211  array = israel_p ? Thu_normal_Israel : Thu_normal;
212  else if (kvia == 2)
213  array = Thu_long;
214  break;
215  }
216  }
217  else /* leap year */
218  switch (daynumber % 7)
219  {
220  case 1: /* RH was on a Sat */
221  if (kvia == 0)
222  array = Sat_short_leap;
223  else if (kvia == 2)
224  array = israel_p ? Sat_long_leap_Israel : Sat_long_leap;
225  break;
226  case 6: /* RH was on a Mon */
227  if (kvia == 0)
228  array = israel_p ? Mon_short_leap_Israel : Mon_short_leap;
229  else if (kvia == 2)
230  array = israel_p ? Mon_long_leap_Israel : Mon_long_leap;
231  break;
232  case 5: /* RH was on a Tue */
233  if (kvia == 1)
234  array =
235  israel_p ? Tue_normal_leap_Israel : Tue_normal_leap;
236  break;
237  case 3: /* RH was on a Thu */
238  if (kvia == 0)
239  array = Thu_short_leap;
240  else if (kvia == 2)
241  array = Thu_long_leap;
242  break;
243 
244  }
245 
246  TQString buffer;
247 
248  if (array == NULL)
249  /* Something is terribly wrong. */
250  {
251  buffer = "??Parsha??";
252  return buffer;
253  }
254  index = array[week];
255  if (index == XX) // no Parsha this week.
256  {
257  buffer = "";
258  return buffer;
259  }
260  else if (index < 100)
261  {
262  buffer = parshiot_names[index];
263  return buffer;
264  }
265  else
266  { // Create a double parsha
267  buffer =
268  parshiot_names[index - 100] + "-" + parshiot_names[index -
269  99];
270  return buffer;
271 
272  }
273 }