calendar-utils.nut 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. This code is based on https://github.com/leafo/lua-date
  3. Copyright (C) 2006, by Jas Latrix ([email protected])
  4. All Rights Deserved.
  5. use this code at your own risk!.
  6. keep out of reach of children.
  7. Ported and adapted to Squirrel/SquiLu by Domingo Alvarez Duarte
  8. */
  9. class CalendarBase {
  10. static _month_names = [
  11. "January", "February", "March", "April", "May", "June", "July",
  12. "August", "September", "October", "November", "December"
  13. ];
  14. /*
  15. static _month_names = [
  16. "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio",
  17. "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"
  18. ];
  19. */
  20. static _month_names_abbr = [
  21. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  22. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  23. ];
  24. /*
  25. static _month_names_abbr = [
  26. "Ene", "Feb", "Mar", "Abr", "May", "Jun",
  27. "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"
  28. ];
  29. */
  30. static _day_names = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  31. //static _day_names = ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"];
  32. //static _day_names_abbr = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  33. static _day_names_abbr = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
  34. //static _day_names_abbr = ["Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab"];
  35. //static _day_names_abbr = ["Do", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa"];
  36. static _month_names_abbr = [
  37. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  38. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  39. ];
  40. /*
  41. static _month_names_abbr = [
  42. "Ene", "Feb", "Mar", "Abr", "May", "Jun",
  43. "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"
  44. ];
  45. */
  46. static _days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
  47. static _seconds_in_hour = 3600;
  48. static _seconds_in_day = 86400;
  49. static _seconds_in_week = 604800;
  50. constructor(){}
  51. function parse_hour_minutes(s){
  52. local ar_t = [];
  53. local function set_matches(m){ar_t.push(v.tointeger()); return true;};
  54. set_matches.setenv(this);
  55. s.gmatch("(%d+)", set_matches);
  56. return {
  57. hour = ar_t.get(0, 0),
  58. minutes = ar_t.get(1, 0),
  59. seconds = ar_t.get(2, 0),
  60. };
  61. }
  62. function parse_day_month_year(s){
  63. local ar_t = [];
  64. local function set_matches(m){ar_t.push(v.tointeger()); return true;};
  65. set_matches.setenv(this);
  66. s.gmatch("(%d+)", set_matches);
  67. local day = ar_t.get(0, 0);
  68. local month = ar_t.get(1, 0);
  69. local year = ar_t.get(2, 0);
  70. if(day > 31){
  71. local tmp = day;
  72. day = year;
  73. year = tmp;
  74. }
  75. return {day = day, month = month, year = year,};
  76. }
  77. function parse_year_month_day(s){
  78. local ar_t = [];
  79. local function set_matches(m){ar_t.push(v.tointeger()); return true;};
  80. set_matches.setenv(this);
  81. s.gmatch("(%d+)", set_matches);
  82. local year = ar_t.get(0, 0);
  83. local month = ar_t.get(1, 0);
  84. local day = ar_t.get(2, 0);
  85. if(day > 31){
  86. local tmp = day;
  87. day = year;
  88. year = tmp;
  89. }
  90. return {day = day, month = month, year = year,};
  91. }
  92. function parse_date( value ){
  93. local adate = os.date("*t");
  94. if (value.len() > 0){
  95. local nums = [];
  96. local function set_matches(m) {nums.push(m.tointeger()); return true};
  97. set_matches.setenv(this);
  98. local setDay = function(idx){
  99. local aday = nums[idx];
  100. if (aday > 0 && aday < 32) adate.day = aday;
  101. }
  102. local setMonth = function(idx){
  103. local amonth = nums[idx];
  104. if (amonth > 0 && amonth < 13) adate.month = amonth-1; //zero based
  105. }
  106. local setYear = function(idx){
  107. local ayear = nums[idx];
  108. if (ayear < 100) ayear += 2000; //current milenium
  109. adate.year = ayear;
  110. }
  111. local setHour = function(){
  112. local ahour = nums[0];
  113. if (ahour > 0 && ahour < 23) adate.hour = ahour;
  114. }
  115. local setMin = function(){
  116. local amin = nums[1];
  117. if (amin > 0 && amin < 60) adate.min = amin;
  118. }
  119. local setSec = function(){
  120. local asec = nums[2];
  121. if (asec > 0 && asec < 60) adate.sec = asec;
  122. }
  123. //print(value);
  124. local avalue = value.split(' ');
  125. avalue[0].gmatch("(%d+)", set_matches);
  126. switch(nums.len()){
  127. case 1: //day in current month
  128. setDay(0);
  129. break;
  130. case 2: //day and month in current year
  131. if (nums[1] > 12) {
  132. setDay(1);
  133. setMonth(0);
  134. }
  135. else
  136. {
  137. setDay(0);
  138. setMonth(1);
  139. }
  140. break;
  141. case 3:
  142. if (nums[0] > 31){
  143. //year and month and day
  144. setYear(0);
  145. setMonth(1);
  146. setDay(2);
  147. }
  148. else
  149. {
  150. //day and month and year
  151. if (nums[2] > 12){
  152. setDay(1);
  153. setMonth(0);
  154. }
  155. else
  156. {
  157. setDay(0);
  158. setMonth(1);
  159. }
  160. setYear(2);
  161. }
  162. break;
  163. }
  164. if (avalue.len() > 1){
  165. nums = [];
  166. avalue[1].gmatch("(%d+)",set_matches);
  167. //print(nums.len(), avalue[1]);
  168. adate.hour = 0;
  169. adate.min = 0;
  170. adate.sec = 0;
  171. local nl = nums.len();
  172. if (nl > 0) setHour();
  173. if (nl > 1) setMin();
  174. if (nl > 2) setSec();
  175. }
  176. }
  177. //foreach( k,v in adate) do print(k,v);
  178. //print(os.date(os.time(adate)))
  179. return adate;
  180. }
  181. function julian_to_unix(J){
  182. // converts a julian date into unit time
  183. return (J - 2440588) * 86400;
  184. }
  185. function get_julian_day(y,m,d){
  186. // returns the current time in julian date format
  187. local now = os.time({year=y,month=m,day=d});
  188. return now / 86400.0 + 2440588;
  189. }
  190. function get_julian_now(){
  191. // returns the current time in julian date format
  192. local now = os.time();
  193. return now / 86400.0 + 2440588;
  194. }
  195. function get_timezone_offset(){
  196. // returns the number of seconds of timezone offset
  197. local tz = os.date("%z").tointeger();
  198. local tzh = math.floor(tz / 100 + 0.5);
  199. local tzm = math.abs(tz) % 100 / 60.0;
  200. if (tzh < 0) tzm *= -1;
  201. return (tzh + tzm) * 3600;
  202. }
  203. //month 0 - 11
  204. function makemonth(y,m){
  205. local month_array = [];
  206. local atime = os.time({year=y,month=m,day=1});
  207. local adate = os.date("*t", atime);
  208. //print("makemonth", y, m, adate.year, adate.month, adate.day);
  209. // get back to the nearest monday
  210. atime -= _seconds_in_day * adate.wday;
  211. adate = os.date("*t", atime);
  212. if (adate.day < 7 && adate.day != 1){
  213. //go back one more week
  214. atime -= _seconds_in_week;
  215. adate = os.date("*t", atime);
  216. }
  217. local count_weeks = 0;
  218. //print("makemonth", adate.year, adate.month, adate.day);
  219. do
  220. {
  221. local week_array = [];
  222. month_array.push(week_array);
  223. do
  224. {
  225. // insert the week days
  226. //print(adate.day);
  227. week_array.push(adate.day);
  228. atime += _seconds_in_day;
  229. adate = os.date("*t", atime);
  230. } while(adate && adate.wday != 0); // sunday
  231. } while(adate && adate.month == m);
  232. return month_array;
  233. }
  234. }
  235. /*
  236. local html_foot = "\n</body>\n</html>\n";
  237. local html_head = [=[
  238. <html>
  239. <style>
  240. th {background:black; color: silver; vertical-align: middle;}
  241. td {vertical-align: middle; text-align:center;}
  242. td.sun {color: red;}
  243. td.sat {color: blue;}
  244. </style>
  245. <body>
  246. ]=];
  247. local html_yearhead = "\n<table align=\"left\">";
  248. local html_monhead = "\n<tr><th colspan = \"7\">%s, %s</th></tr><tr><td>sun</td><td>mon</td><td>tue</td><td>wed</td><td>thu</td><td>fri</td><td>sat</td></tr>";
  249. local html_monweek = "\n<tr><td class=\"sun\">%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td class=\"sat\">%d</td></tr>";
  250. local html_yearfoot = "\n</table>";
  251. function makecalendar(y, iox){
  252. iox.write(html_yearhead);
  253. for(local i = 0; i < 12; ++i){
  254. local tm = CalendarBase.makemonth(y, i);
  255. iox.write(format(html_monhead, CalendarBase._month_names[i], y.tostring()));
  256. foreach( k, v in tm) iox.write(format(html_monweek, v[0], v[1], v[2], v[3], v[4], v[5], v[6]));
  257. }
  258. iox.write(html_yearfoot);
  259. }
  260. local outf = file("year-calendar.html", "w");
  261. outf.write(html_head);
  262. foreach(v in [2012,2013,2014] ){
  263. local y = v.tointeger();
  264. if (y) makecalendar(y, outf);
  265. }
  266. outf.write(html_foot);
  267. */