PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/gcal-3.6.2/misc/dst/dst.pl

#
Perl | 346 lines | 273 code | 0 blank | 73 comment | 31 complexity | ad60eb490b08d6a354bf715907948f90 MD5 | raw file
Possible License(s): GPL-3.0
  1. # $Id: dst.pl 0.04 2000/03/24 00:00:04 tom Exp $
  2. #
  3. # dst.pl: Processes ZONEINFO files and converts any dates to the Gcal
  4. # fixed date format, at which Daylight-Saving Times take place.
  5. #
  6. # Any but default configuration could confuse this script.
  7. # It comes along with a UN*X script `dst' which supports the correct usage.
  8. #
  9. # It is *not* guaranteed that this script works for any other call than
  10. # the one given above but it could easily be modified and extended for
  11. # using other special modes of operation.
  12. #
  13. # If you modify this script you have to rename the modified version.
  14. #
  15. # If you make any improvements I would like to hear from you.
  16. # But I do not promise any support.
  17. #
  18. # Copyright (c) 2000 Thomas Esken <esken@uni-muenster.de>
  19. # Im Hagenfeld 84
  20. # D-48147 M"unster
  21. # GERMANY
  22. #
  23. # This software doesn't claim completeness, correctness or usability.
  24. # On principle I will not be liable for ANY damages or losses (implicit
  25. # or explicit), which result from using or handling my software.
  26. # If you use this software, you agree without any exception to this
  27. # agreement, which binds you LEGALLY !!
  28. #
  29. # This program is free software; you can redistribute it and/or modify
  30. # it under the terms of the `GNU General Public License' as published by
  31. # the `Free Software Foundation'; either version 2, or (at your option)
  32. # any later version.
  33. #
  34. # You should have received a copy of the `GNU General Public License'
  35. # along with this program; if not, write to the:
  36. #
  37. #
  38. #
  39. # Initialization statements.
  40. #
  41. $[ = 1; # set array base to 1
  42. #
  43. # Define the field separator used (BLANK actually).
  44. #
  45. $FS = ' '; # set field separator
  46. #
  47. # Define the default return value of this process, which is EXIT_FAILURE.
  48. #
  49. $EXIT_SUCCESS = 0;
  50. $EXIT_FAILURE = 1;
  51. $EXIT_FATAL = 2;
  52. #
  53. $exit_status = $EXIT_FAILURE;
  54. #
  55. # Define some more constant values.
  56. #
  57. $counter = 0;
  58. $dst2nor = 0;
  59. $is_first = 0;
  60. $do_print = 0;
  61. $dst_text = 'Daylight-Saving Time';
  62. #
  63. $warn_before = 0;
  64. $warn_after = 0;
  65. #
  66. # Get possibly given command line arguments.
  67. #
  68. for ($i = 1; $i < ($#ARGV+1); $i++) {
  69. if (substr($ARGV[$i], 1, 1) eq '-') {
  70. if (substr($ARGV[$i], 2, 1) eq 'b') {
  71. $warn_before = substr($ARGV[$i], 3, 999999);
  72. }
  73. elsif (substr($ARGV[$i], 2, 1) eq 'a') {
  74. $warn_after = substr($ARGV[$i], 3, 999999);
  75. }
  76. else {
  77. exit $EXIT_FATAL;
  78. }
  79. }
  80. else {
  81. last;
  82. }
  83. shift;
  84. $i--;
  85. }
  86. #
  87. # Main block.
  88. #
  89. line: while (<>) {
  90. chop; # strip record separator
  91. @Fld = split(/[$FS]+/, $_, 9999);
  92. #
  93. # Initialization statements.
  94. #
  95. $act_loc_wd = $Fld[9];
  96. $act_loc_mo = $Fld[10];
  97. $act_loc_da = $Fld[11];
  98. $act_loc_ti = $Fld[12];
  99. $act_loc_ye = $Fld[13];
  100. $act_loc_tz = $Fld[14];
  101. $act_loc_ds = $Fld[15];
  102. #
  103. if (substr($act_loc_ds, length($act_loc_ds), 1) eq '1') {
  104. $is_dst = 1;
  105. }
  106. else {
  107. $is_dst = 0;
  108. }
  109. if ($counter % 3) {
  110. $counter = 0;
  111. if ($is_dst == 1) {
  112. $do_print = 1;
  113. $result = &incr_time($pre_loc_wd, $pre_loc_da, $pre_loc_mo, $pre_loc_ye, $pre_loc_ti);
  114. }
  115. elsif ($dst2nor == 1) {
  116. $do_print = 1;
  117. }
  118. }
  119. else {
  120. $counter++;
  121. if ($is_dst == 1) {
  122. $dst2nor = 1;
  123. $result = &incr_time($act_loc_wd, $act_loc_da, $act_loc_mo, $act_loc_ye, $act_loc_ti);
  124. }
  125. }
  126. if ($do_print == 1) {
  127. $do_print = 0;
  128. $dst2nor = 0;
  129. $i = 1;
  130. #
  131. $the_year = '';
  132. for (;;) {
  133. $ch = substr($result, $i++, 1);
  134. if ($ch eq $FS) {
  135. last;
  136. }
  137. $the_year = $the_year . $ch;
  138. }
  139. #
  140. $the_month = '';
  141. for (;;) {
  142. $ch = substr($result, $i++, 1);
  143. if ($ch eq $FS) {
  144. last;
  145. }
  146. $the_month = $the_month . $ch;
  147. }
  148. #
  149. $the_day = '';
  150. for (;;) {
  151. $ch = substr($result, $i++, 1);
  152. if ($ch eq $FS) {
  153. last;
  154. }
  155. $the_day = $the_day . $ch;
  156. }
  157. #
  158. $the_wd = '';
  159. for (;;) {
  160. $ch = substr($result, $i++, 1);
  161. if ($ch eq $FS) {
  162. last;
  163. }
  164. $the_wd = $the_wd . $ch;
  165. }
  166. #
  167. $the_hour = '';
  168. for (;;) {
  169. $ch = substr($result, $i++, 1);
  170. if ($ch eq $FS) {
  171. last;
  172. }
  173. $the_hour = $the_hour . $ch;
  174. }
  175. #
  176. $the_min = '';
  177. for (;;) {
  178. $ch = substr($result, $i++, 1);
  179. if ($ch eq $FS) {
  180. last;
  181. }
  182. $the_min = $the_min . $ch;
  183. }
  184. #
  185. $the_sec = substr($result, $i, 999999);
  186. #
  187. if ($is_first == 0) {
  188. $is_first = 1;
  189. #
  190. # Set the return value of this process to EXIT_SUCCESS.
  191. #
  192. $exit_status = $EXIT_SUCCESS;
  193. #
  194. printf ";\n; `%s.rc' ---Daylight-Saving Times--- for Gcal-2.20 or newer\n;\n\$t=%s",
  195. $Fld[1], $dst_text;
  196. }
  197. printf "\n;\nd=%02d%d\n", $the_month, $the_day;
  198. if (($warn_before == 0) && ($warn_after == 0)) {
  199. printf "%04d\@d-%d#+%d \$t (%s->%s) %02d:%02d:%02d->%s",
  200. $the_year, $warn_before, $warn_after, $pre_loc_tz,
  201. $act_loc_tz, $the_hour, $the_min, $the_sec, $act_loc_ti;
  202. }
  203. else {
  204. printf "%04d\@d-%d#+%d %s, \$t (%s->%s) %02d:%02d:%02d->%s",
  205. $the_year, $warn_before, $warn_after, $the_wd, $pre_loc_tz,
  206. $act_loc_tz, $the_hour, $the_min, $the_sec, $act_loc_ti;
  207. }
  208. }
  209. #
  210. $pre_loc_wd = $act_loc_wd;
  211. $pre_loc_mo = $act_loc_mo;
  212. $pre_loc_da = $act_loc_da;
  213. $pre_loc_ti = $act_loc_ti;
  214. $pre_loc_ye = $act_loc_ye;
  215. $pre_loc_tz = $act_loc_tz;
  216. }
  217. if ($exit_status == $EXIT_SUCCESS) {
  218. printf "\n";
  219. }
  220. exit $exit_status;
  221. #
  222. # Function implementations.
  223. #
  224. sub is_leap {
  225. local($year) = @_;
  226. if (($year % 4) || (!($year % 100) && ($year % 400))) {
  227. return 0;
  228. }
  229. 1;
  230. }
  231. #
  232. sub month_number {
  233. local($month_name) = @_;
  234. if ($month_name eq 'Jan') {
  235. return 1;
  236. }
  237. if ($month_name eq 'Feb') {
  238. return 2;
  239. }
  240. if ($month_name eq 'Mar') {
  241. return 3;
  242. }
  243. if ($month_name eq 'Apr') {
  244. return 4;
  245. }
  246. if ($month_name eq 'May') {
  247. return 5;
  248. }
  249. if ($month_name eq 'Jun') {
  250. return 6;
  251. }
  252. if ($month_name eq 'Jul') {
  253. return 7;
  254. }
  255. if ($month_name eq 'Aug') {
  256. return 8;
  257. }
  258. if ($month_name eq 'Sep') {
  259. return 9;
  260. }
  261. if ($month_name eq 'Oct') {
  262. return 10;
  263. }
  264. if ($month_name eq 'Nov') {
  265. return 11;
  266. }
  267. if ($month_name eq 'Dec') {
  268. return 12;
  269. }
  270. 0;
  271. }
  272. #
  273. sub incr_wd {
  274. local($wd) = @_;
  275. if ($wd eq 'Mon') {
  276. return 'Tue';
  277. }
  278. if ($wd eq 'Tue') {
  279. return 'Wed';
  280. }
  281. if ($wd eq 'Wed') {
  282. return 'Thu';
  283. }
  284. if ($wd eq 'Thu') {
  285. return 'Fri';
  286. }
  287. if ($wd eq 'Fri') {
  288. return 'Sat';
  289. }
  290. if ($wd eq 'Sat') {
  291. return 'Sun';
  292. }
  293. if ($wd eq 'Sun') {
  294. return 'Mon';
  295. }
  296. 'ERR';
  297. }
  298. #
  299. sub incr_time {
  300. local($wd, $day, $month, $year, $Time) = @_;
  301. $old_day = $day;
  302. $mo = &month_number($month);
  303. $hr = substr($Time, 1, 2);
  304. $mi = substr($Time, 4, 2);
  305. $se = substr($Time, 7, 2);
  306. $se++;
  307. if ($se > 59) {
  308. $se = 0;
  309. $mi++;
  310. }
  311. if ($mi > 59) {
  312. $mi = 0;
  313. $hr++;
  314. }
  315. if ($hr > 23) {
  316. $hr = 0;
  317. $day++;
  318. if (($day > 31) && ($mo == 1 || $mo == 3 || $mo == 5 || $mo == 7 || $mo == 8 || $mo == 10 || $mo == 12)) {
  319. $day = 1;
  320. $mo++;
  321. }
  322. elsif (($day > 30) && ($mo == 4 || $mo == 6 || $mo == 9 || $mo == 11)) {
  323. $day = 1;
  324. $mo++;
  325. }
  326. elsif (($day > 28) && ($mo == 2)) {
  327. if ((&is_leap($year) == 0) && ($day >= 29)) {
  328. $day = 1;
  329. $mo++;
  330. }
  331. elsif ($day > 29) {
  332. $day = 1;
  333. $mo++;
  334. }
  335. }
  336. if ($mo > 12) {
  337. $mo = 1;
  338. $year++;
  339. }
  340. }
  341. if ($old_day ne $day) {
  342. $wd = &incr_wd($wd);
  343. }
  344. $year . $FS . $mo . $FS . $day . $FS . $wd . $FS . $hr . $FS . $mi . $FS . $se;
  345. }