Source:NetHack 5.0.0/src/calendar.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to calendar.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/calendar.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	calendar.c	$NHDT-Date: 1706213796 2024/01/25 20:16:36 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.116 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Michael Allison, 2007. */
4. /* Copyright (c) Robert Patrick Rankin, 1991                      */
5. /* NetHack may be freely redistributed.  See license for details. */
6. 
7. #include "hack.h"
8. 
9. /*
10.  * Time routines
11.  *
12.  * The time is used for:
13.  *  - seed for rand()
14.  *  - year on tombstone and yyyymmdd in record file
15.  *  - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
16.  *  - night and midnight (the undead are dangerous at midnight)
17.  *  - determination of what files are "very old"
18.  */
19. 
20. /* TIME_type: type of the argument to time(); we actually use &(time_t);
21.    you might need to define either or both of these to 'long *' in *conf.h */
22. #ifndef TIME_type
23. #define TIME_type time_t *
24. #endif
25. #ifndef LOCALTIME_type
26. #define LOCALTIME_type time_t *
27. #endif
28. 
29. staticfn struct tm *getlt(void);
30. 
31. time_t
32. getnow(void)
33. {
34.     time_t datetime = 0;
35. 
36.     (void) time((TIME_type) &datetime);
37.     return datetime;
38. }
39. 
40. staticfn struct tm *
41. getlt(void)
42. {
43.     time_t date = getnow();
44. 
45.     return localtime((LOCALTIME_type) &date);
46. }
47. 
48. int
49. getyear(void)
50. {
51.     return (1900 + getlt()->tm_year);
52. }
53. 
54. 
55. long
56. yyyymmdd(time_t date)
57. {
58.     long datenum;
59.     struct tm *lt;
60. 
61.     if (date == 0)
62.         lt = getlt();
63.     else
64.         lt = localtime((LOCALTIME_type) &date);
65. 
66.     /* just in case somebody's localtime supplies (year % 100)
67.        rather than the expected (year - 1900) */
68.     if (lt->tm_year < 70)
69.         datenum = (long) lt->tm_year + 2000L;
70.     else
71.         datenum = (long) lt->tm_year + 1900L;
72.     /* yyyy --> yyyymm */
73.     datenum = datenum * 100L + (long) (lt->tm_mon + 1);
74.     /* yyyymm --> yyyymmdd */
75.     datenum = datenum * 100L + (long) lt->tm_mday;
76.     return datenum;
77. }
78. 
79. long
80. hhmmss(time_t date)
81. {
82.     long timenum;
83.     struct tm *lt;
84. 
85.     if (date == 0)
86.         lt = getlt();
87.     else
88.         lt = localtime((LOCALTIME_type) &date);
89. 
90.     timenum = lt->tm_hour * 10000L + lt->tm_min * 100L + lt->tm_sec;
91.     return timenum;
92. }
93. 
94. char *
95. yyyymmddhhmmss(time_t date)
96. {
97.     long datenum;
98.     static char datestr[15];
99.     struct tm *lt;
100. 
101.     if (date == 0)
102.         lt = getlt();
103.     else
104.         lt = localtime((LOCALTIME_type) &date);
105. 
106.     /* just in case somebody's localtime supplies (year % 100)
107.        rather than the expected (year - 1900) */
108.     if (lt->tm_year < 70)
109.         datenum = (long) lt->tm_year + 2000L;
110.     else
111.         datenum = (long) lt->tm_year + 1900L;
112.     Snprintf(datestr, sizeof datestr, "%04ld%02d%02d%02d%02d%02d",
113.              datenum, lt->tm_mon + 1,
114.              lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
115.     //debugpline1("yyyymmddhhmmss() produced date string %s", datestr);
116.     return datestr;
117. }
118. 
119. time_t
120. time_from_yyyymmddhhmmss(char *buf)
121. {
122.     int k;
123.     time_t timeresult = (time_t) 0;
124.     struct tm t, *lt;
125.     char *d, *p, y[5], mo[3], md[3], h[3], mi[3], s[3];
126. 
127.     if (buf && strlen(buf) == 14) {
128.         d = buf;
129.         p = y; /* year */
130.         for (k = 0; k < 4; ++k)
131.             *p++ = *d++;
132.         *p = '\0';
133.         p = mo; /* month */
134.         for (k = 0; k < 2; ++k)
135.             *p++ = *d++;
136.         *p = '\0';
137.         p = md; /* day */
138.         for (k = 0; k < 2; ++k)
139.             *p++ = *d++;
140.         *p = '\0';
141.         p = h; /* hour */
142.         for (k = 0; k < 2; ++k)
143.             *p++ = *d++;
144.         *p = '\0';
145.         p = mi; /* minutes */
146.         for (k = 0; k < 2; ++k)
147.             *p++ = *d++;
148.         *p = '\0';
149.         p = s; /* seconds */
150.         for (k = 0; k < 2; ++k)
151.             *p++ = *d++;
152.         *p = '\0';
153.         lt = getlt();
154.         if (lt) {
155.             t = *lt;
156.             t.tm_year = atoi(y) - 1900;
157.             t.tm_mon = atoi(mo) - 1;
158.             t.tm_mday = atoi(md);
159.             t.tm_hour = atoi(h);
160.             t.tm_min = atoi(mi);
161.             t.tm_sec = atoi(s);
162.             timeresult = mktime(&t);
163.         }
164.         if (timeresult == (time_t) -1)
165.             ;
166. #if 0
167. TODO: set_debugpline1, debugpline1 -> function pointer
168.             debugpline1("time_from_yyyymmddhhmmss(%s) would have returned -1",
169.                         buf ? buf : "");
170. #endif
171.         else
172.             return timeresult;
173.     }
174.     return (time_t) 0;
175. }
176. 
177. /*
178.  * moon period = 29.53058 days ~= 30, year = 365.2422 days
179.  * days moon phase advances on first day of year compared to preceding year
180.  *      = 365.2422 - 12*29.53058 ~= 11
181.  * years in Metonic cycle (time until same phases fall on the same days of
182.  *      the month) = 18.6 ~= 19
183.  * moon phase on first day of year (epact) ~= (11*(year%19) + 29) % 30
184.  *      (29 as initial condition)
185.  * current phase in days = first day phase + days elapsed in year
186.  * 6 moons ~= 177 days
187.  * 177 ~= 8 reported phases * 22
188.  * + 11/22 for rounding
189.  */
190. int
191. phase_of_the_moon(void) /* 0-7, with 0: new, 4: full */
192. {
193.     struct tm *lt = getlt();
194.     int epact, diy, goldn;
195. 
196.     diy = lt->tm_yday;
197.     goldn = (lt->tm_year % 19) + 1;
198.     epact = (11 * goldn + 18) % 30;
199.     if ((epact == 25 && goldn > 11) || epact == 24)
200.         epact++;
201. 
202.     return ((((((diy + epact) * 6) + 11) % 177) / 22) & 7);
203. }
204. 
205. boolean
206. friday_13th(void)
207. {
208.     struct tm *lt = getlt();
209. 
210.     /* tm_wday (day of week; 0==Sunday) == 5 => Friday */
211.     return (boolean) (lt->tm_wday == 5 && lt->tm_mday == 13);
212. }
213. 
214. int
215. night(void)
216. {
217.     int hour = getlt()->tm_hour;
218. 
219.     return (hour < 6 || hour > 21);
220. }
221. 
222. int
223. midnight(void)
224. {
225.     return (getlt()->tm_hour == 0);
226. }
227. 
228. /* calendar.c */
229.