ODATE.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : ODATE.CPP
  21. //Description : Date Information Object
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <windowsx.h>
  25. #include <OSTR.h>
  26. #include <OMISC.h>
  27. #include <OTRANSL.h>
  28. #include <ODATE.h>
  29. //--------- Define static member variables -----------//
  30. #define JULIAN_ADJUSTMENT 1721425L
  31. static char month_str_array[][10] =
  32. {
  33. "January",
  34. "February",
  35. "March",
  36. "April",
  37. "May",
  38. "June",
  39. "July",
  40. "August",
  41. "September",
  42. "October",
  43. "November",
  44. "December"
  45. } ;
  46. static int month_tot[]=
  47. { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 } ;
  48. // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  49. // 31 28 31 30 31 30 31 31 30 31 30 31
  50. //------------ Begin of function DateInfo::julain ----------//
  51. //
  52. // Convert from year, month, day integer format to julian date format
  53. //
  54. // Julian day is the number of days since the date Jan 1, 4713 BC
  55. // Ex. Jan 1, 1981 is 2444606
  56. //
  57. // <int> year, month, day = the components of the date
  58. //
  59. // Return : <long> the julian date
  60. // -1 illegal given date
  61. //
  62. long DateInfo::julian( int year, int month, int day )
  63. {
  64. long total, dayYear ;
  65. dayYear = day_year( year, month, day) ;
  66. if (dayYear < 1)
  67. return( -1) ; /* Illegal Date */
  68. total = ytoj(year) ;
  69. total+= dayYear ;
  70. total+= JULIAN_ADJUSTMENT ;
  71. return total;
  72. }
  73. //-------------- End of function DateInfo::julian ------------//
  74. //--------- Begin of function DateInfo::julian ---------//
  75. //
  76. // Convert a string such as "07/03/93" to long number
  77. //
  78. // Returns:
  79. // <int> >0 - Julian day
  80. // That is the number of days since the date Jan 1, 4713 BC
  81. // Ex. Jan 1, 1981 is 2444606
  82. // 0 - NULL Date (dbf_date is all blank)
  83. // -1 - Illegal Date
  84. //
  85. long DateInfo::julian( char *dateStr )
  86. {
  87. return julian( m.atoi( dateStr,4 ), m.atoi( dateStr+4,2 ),
  88. m.atoi( dateStr+6,2 ) );
  89. }
  90. //---------- End of function DateInfo::julian ----------//
  91. //------------ Begin of function DateInfo::get_date ---------//
  92. //
  93. // Return year, month or day of the given julian date
  94. //
  95. // <long> julianDate = the julian date to be converted
  96. // <char> returnType = 'Y'-year, 'M'-month, 'D'-day
  97. //
  98. // Return : the year, month or day of the julian date
  99. // -1 if the julian date passed is invalid
  100. //
  101. int DateInfo::get_date( long julianDate, char returnType )
  102. {
  103. int year, month, day, nDays, maxDays ;
  104. long totalDays ;
  105. if ( julianDate > 5373484 || julianDate < JULIAN_ADJUSTMENT )
  106. return -1;
  107. totalDays = (long) (julianDate) - JULIAN_ADJUSTMENT ;
  108. year = (int) ((double)totalDays/365.2425) + 1 ;
  109. nDays = (int) (totalDays - ytoj(year)) ;
  110. if ( nDays <= 0 )
  111. {
  112. year-- ;
  113. nDays = (int) (totalDays - ytoj(year)) ;
  114. }
  115. if (year%4 == 0 && year%100 != 0 || year%400 == 0)
  116. maxDays = 366 ;
  117. else
  118. maxDays = 365 ;
  119. if ( nDays > maxDays )
  120. {
  121. year++ ;
  122. nDays -= maxDays ;
  123. }
  124. if ( month_day( year, nDays, month, day ) < 0 )
  125. return -1;
  126. //............................................//
  127. switch( returnType )
  128. {
  129. case 'Y':
  130. return year;
  131. case 'M': // return the month
  132. return month;
  133. case 'D':
  134. return day;
  135. }
  136. return 0;
  137. }
  138. //------------- End of function DateInfo::get_date --------//
  139. //------------ Begin of function DateInfo::date_str ---------//
  140. //
  141. // Convert from julian date format to string date format
  142. // Return a date string in the format of DD MMM YYYY e.g. 15 Jan 1992
  143. //
  144. // Julian day is the number of days since the date Jan 1, 4713 BC
  145. // Ex. Jan 1, 1981 is 2444606
  146. //
  147. // <long> julianDate = the julian date to be converted
  148. // [int] shortMonthStr = short month string or not (e.g. Jan instead of January)
  149. // (default : 0)
  150. //
  151. // Return : <char*> the formated date string
  152. //
  153. char* DateInfo::date_str( long julianDate, int shortMonthStr)
  154. {
  155. int year, month, day, nDays, maxDays ;
  156. long totalDays ;
  157. static char strBuf[16];
  158. if ( julianDate > 5373484 || julianDate < JULIAN_ADJUSTMENT )
  159. {
  160. strBuf[0]=NULL;
  161. return strBuf;
  162. }
  163. totalDays = (long) (julianDate) - JULIAN_ADJUSTMENT ;
  164. year = (int) ((double)totalDays/365.2425) + 1 ;
  165. nDays = (int) (totalDays - ytoj(year)) ;
  166. if ( nDays <= 0 )
  167. {
  168. year-- ;
  169. nDays = (int) (totalDays - ytoj(year)) ;
  170. }
  171. if (year%4 == 0 && year%100 != 0 || year%400 == 0)
  172. maxDays = 366 ;
  173. else
  174. maxDays = 365 ;
  175. if ( nDays > maxDays )
  176. {
  177. year++ ;
  178. nDays -= maxDays ;
  179. }
  180. if ( month_day( year, nDays, month, day ) < 0 )
  181. {
  182. strBuf[0]=NULL;
  183. return strBuf;
  184. }
  185. //--------------------------------------------//
  186. static String str;
  187. #if(defined(SPANISH))
  188. if( shortMonthStr )
  189. {
  190. str = itoa(day,strBuf,10); // day
  191. str += "-";
  192. strcpy(strBuf, translate.process(month_str_array[month-1]));
  193. if(strlen(strBuf) > 3)
  194. strBuf[3] = '\0'; // limit month to 3 chars
  195. str += strBuf; // month
  196. str += "-";
  197. str += itoa(year,strBuf,10); // year
  198. }
  199. else
  200. {
  201. str = itoa(day,strBuf,10); // day
  202. str += " de ";
  203. str += translate.process(month_str_array[month-1]);
  204. str += " de ";
  205. str += itoa(year,strBuf,10); // year
  206. }
  207. #elif(defined(FRENCH))
  208. str = itoa(day,strBuf,10); // day
  209. str += " ";
  210. if( shortMonthStr )
  211. {
  212. strcpy(strBuf, translate.process(month_str_array[month-1]));
  213. if(strlen(strBuf) > 3)
  214. strBuf[3] = '\0'; // limit month to 4 chars
  215. if(month == 7) // Juillet(July) abbreviated to Jul.
  216. strBuf[2] = 'l';
  217. str += strBuf; // month
  218. }
  219. else
  220. {
  221. str += translate.process(month_str_array[month-1]);
  222. }
  223. str += " ";
  224. str += itoa(year,strBuf,10); // year
  225. #else
  226. // GERMAN and US
  227. str = translate.process(month_str_array[month-1]);
  228. if( shortMonthStr )
  229. str = str.left(3);
  230. str += " ";
  231. str += itoa(day,strBuf,10);
  232. str += ", ";
  233. str += itoa(year,strBuf,10);
  234. #endif
  235. return str;
  236. }
  237. //------------- End of function DateInfo::date_str --------//
  238. //------------ Begin of function DateInfo::month_str ---------//
  239. //
  240. // <int> monthNo = the month (1-12)
  241. //
  242. // Return : <char*> the month string
  243. //
  244. char* DateInfo::month_str(int monthNo)
  245. {
  246. return translate.process(month_str_array[monthNo-1]);
  247. }
  248. //------------- End of function DateInfo::month_str --------//
  249. //---------- Begin of function DateInfo::day_year ----------//
  250. //
  251. // Returns an (int) day of the year starting from 1.
  252. // Ex. Jan 1, returns 1
  253. //
  254. // Returns -1 if it is an illegal date.
  255. //
  256. int DateInfo::day_year( int year, int month, int day )
  257. {
  258. int isLeap, monthDays ;
  259. isLeap = ( year%4 == 0 && year%100 != 0 || year%400 == 0 ) ? 1 : 0 ;
  260. monthDays = month_tot[ month+1 ] - month_tot[ month] ;
  261. if ( month == 2 ) monthDays += isLeap ;
  262. if ( year < 0 ||
  263. month < 1 || month > 12 ||
  264. day < 1 || day > monthDays )
  265. return( -1 ) ; /* Illegal Date */
  266. if ( month <= 2 ) isLeap = 0 ;
  267. return( month_tot[month] + day + isLeap ) ;
  268. }
  269. //------------- End of function DateInfo::day_year -----------//
  270. //----------- Begin of function DateInfo::ytoj ----------//
  271. //
  272. // c4ytoj - Calculates the number of days to the year
  273. //
  274. // This calculation takes into account the fact that
  275. // 1) Years divisible by 400 are always leap years.
  276. // 2) Years divisible by 100 but not 400 are not leap years.
  277. // 3) Otherwise, years divisible by four are leap years.
  278. //
  279. // Since we do not want to consider the current year, we will
  280. // subtract the year by 1 before doing the calculation.
  281. long DateInfo::ytoj( int yr )
  282. {
  283. yr-- ;
  284. return( yr*365L + yr/4L - yr/100L + yr/400L ) ;
  285. }
  286. //--------------- End of function DateInfo::ytoj -----------//
  287. //--------- Begin of function DateInfo::month_day ---------//
  288. //
  289. // Given the year and the day of the year, returns the
  290. // month and day of month.
  291. //
  292. int DateInfo::month_day( int year, int days, int &monthRef, int &dayRef )
  293. {
  294. int isLeap, i ;
  295. isLeap = ( year%4 == 0 && year%100 != 0 || year%400 == 0 ) ? 1 : 0 ;
  296. if ( days <= 59 ) isLeap = 0 ;
  297. for( i = 2; i <= 13; i++)
  298. {
  299. if ( days <= month_tot[i] + isLeap )
  300. {
  301. monthRef = --i ;
  302. if ( i <= 2) isLeap = 0 ;
  303. dayRef = days - month_tot[ i] - isLeap ;
  304. return( 0) ;
  305. }
  306. }
  307. dayRef = 0 ;
  308. monthRef = 0 ;
  309. return( -1 ) ;
  310. }
  311. //----------- End of function DateInfo::month_day -----------//
  312. //--------- Begin of function DateInfo::time_str ---------//
  313. //
  314. // Given a integer as time and then return the time string
  315. //
  316. // <int> inTime = the time in integer format (e.g. 1600 --> "16:00" )
  317. //
  318. char* DateInfo::time_str(int inTime)
  319. {
  320. // #### begin Gilbert 18/8 ####//
  321. static char strBuf[6] = "00:00";
  322. //itoa( inTime/100, strBuf, 10 );
  323. //itoa( inTime%100, strBuf+3, 10 );
  324. strBuf[4] = '0' + inTime % 10;
  325. strBuf[3] = '0' + (inTime/10) % 10;
  326. strBuf[1] = '0' + (inTime/100) % 10;
  327. strBuf[0] = '0' + (inTime/1000) % 10;
  328. // #### end Gilbert 18/8 ####//
  329. return strBuf;
  330. }
  331. //---------- End of function DateInfo::time_str ----------//
  332. //-------- Begin of function DateInfo::days_in_month ------//
  333. //
  334. // return the no. of days in a given month
  335. //
  336. // <int> inMonth = the specified month
  337. //
  338. // return : <int> = the no. of days in a given monthh
  339. //
  340. int DateInfo::days_in_month(int inMonth)
  341. {
  342. return month_tot[inMonth+1] - month_tot[inMonth];
  343. }
  344. //---------- End of function DateInfo::days_in_month ------//
  345. //-------- Begin of function DateInfo::add_months ------//
  346. //
  347. // Add months to the given julian date.
  348. //
  349. // <int> inDate = the input julian date
  350. // <int> addMonth = the no. of months to add
  351. //
  352. // return : <int> - the result julian date
  353. //
  354. int DateInfo::add_months(int inDate, int addMonth)
  355. {
  356. int inYear = year(inDate);
  357. int inMonth = month(inDate);
  358. int inDay = day(inDate);
  359. inMonth += addMonth;
  360. if( inMonth > 12 )
  361. {
  362. inMonth -= 12;
  363. inYear++;
  364. }
  365. if( inDay > days_in_month(inMonth) )
  366. inDay = days_in_month(inMonth);
  367. return julian( inYear, inMonth, inDay );
  368. }
  369. //---------- End of function DateInfo::add_months ------//
  370. //-------- Begin of function DateInfo::file_time_to_julian ------//
  371. //
  372. int DateInfo::file_time_to_julian(FILETIME& fileTime)
  373. {
  374. /*
  375. WORD dosTime, dosDate;
  376. CoFileTimeToDosDateTime( &fileTime, &dosDate, &dosTime );
  377. //--------------------------------------------//
  378. // Bits Contents
  379. // 0-4 Days of the month (1-31).
  380. // 5-8 Months (1 = January, 2 = February, and so forth).
  381. // 9-15 Year offset from 1980 (add 1980 to get actual year).
  382. //--------------------------------------------//
  383. return julian( 1980 + (dosDate>>9), (dosDate>>5) & 0x0F, dosDate & 0x0F );
  384. */
  385. return 0;
  386. }
  387. //---------- End of function DateInfo::file_time_to_julian ------//