bDateController.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. 'use strict';
  2. const luxon = require('luxon');
  3. const {LocalBadiDate} = require('badidate');
  4. /**
  5. * @typedef {PlainObject} DateConfig
  6. * @property {string} latitude
  7. * @property {string} longitude
  8. * @property {string} timezoneId
  9. */
  10. /**
  11. * @param {Date} date
  12. * @param {DateConfig} dateCfg
  13. * @returns {LocalBadiDate}
  14. */
  15. function createDateObject (date, {
  16. // Bahjí
  17. latitude = 32.9434,
  18. longitude = 35.0924,
  19. timezoneId = 'Asia/Jerusalem'
  20. } = {}) {
  21. const luxonDate = luxon.DateTime.fromJSDate(date);
  22. return new LocalBadiDate(luxonDate, latitude, longitude, timezoneId);
  23. }
  24. /**
  25. * @param {string} tz
  26. * @returns {string|undefined}
  27. */
  28. function sanitizeTimeZone (tz) {
  29. try {
  30. const timeZone = tz.replace(/ /gu, '_');
  31. Intl.DateTimeFormat(undefined, {timeZone});
  32. return timeZone;
  33. } catch (err) {
  34. // Will allow default above to be used
  35. return undefined;
  36. }
  37. }
  38. /**
  39. * @param {any} s
  40. * @returns {Float|undefined}
  41. */
  42. function sanitizeFloat (s) {
  43. if (s === '' || s === undefined || s === null || s === false ||
  44. Number.isNaN(Number(s))) {
  45. // Will allow default above to be used
  46. return undefined;
  47. }
  48. return Number.parseFloat(s);
  49. }
  50. /**
  51. * @param {any} s
  52. * @returns {Integer}
  53. */
  54. function sanitizeInteger (s) {
  55. if (s === '' || s === undefined || s === null || s === false ||
  56. Number.isNaN(Number(s))) {
  57. return 0;
  58. }
  59. return Number.parseInt(s);
  60. }
  61. exports.test = function (req, res) {
  62. res.json({message: 'Hi there'});
  63. };
  64. /**
  65. * @typedef {PlainObject} BadiDateInfo
  66. * @property {string} message
  67. * @property {PlainObject} badi_date
  68. * @property {Integer} badi_date.year
  69. * @property {Integer} badi_date.month
  70. * @property {Integer} badi_date.day
  71. * @property {string} badi_date.month_name
  72. * @property {string} badi_date.timezone_id
  73. * @property {PlainObject} greg_date
  74. * @property {Integer} greg_date.year
  75. * @property {Integer} greg_date.month
  76. * @property {Integer} greg_date.day
  77. * @property {Integer} greg_date.hour
  78. * @property {Integer} greg_date.minute
  79. * @property {Integer} greg_date.second
  80. * @property {Integer} greg_date.timezoneOffset
  81. */
  82. /**
  83. * @typedef {PlainObject} BadiDateResponse
  84. * @property {Date} now
  85. * @property {LocalBadiDate} nowBadi
  86. * @property {BadiDateInfo} json
  87. */
  88. /**
  89. * @param {DateConfig} dateObj
  90. * @returns {BadiDateResponse}
  91. */
  92. const getTodayJSON = exports.getTodayJSON = function (dateObj = {}) {
  93. const now = new Date();
  94. const latitude = sanitizeFloat(dateObj.latitude);
  95. const longitude = sanitizeFloat(dateObj.longitude);
  96. const timezoneId = sanitizeTimeZone(dateObj.timezoneId);
  97. const nowBadi = createDateObject(now, {
  98. latitude, longitude, timezoneId
  99. });
  100. return {
  101. now,
  102. nowBadi,
  103. json: {
  104. message: 'Today is ' + nowBadi.badiDate.format(),
  105. badi_date: {
  106. year: nowBadi.badiDate.year,
  107. month: nowBadi.badiDate.month,
  108. day: nowBadi.badiDate.day,
  109. month_name: nowBadi.badiDate.format('MM+'),
  110. timezone_id: nowBadi.timezoneId
  111. },
  112. greg_date: {
  113. year: now.getFullYear(),
  114. month: now.getMonth() + 1,
  115. day: now.getDate(),
  116. hour: now.getHours(),
  117. minute: now.getMinutes(),
  118. second: now.getSeconds(),
  119. timezoneOffset: now.getTimezoneOffset()
  120. }
  121. }
  122. };
  123. };
  124. exports.today = function (req, res) {
  125. const {json, nowBadi} = getTodayJSON(req.query);
  126. // eslint-disable-next-line no-console -- CLI
  127. console.log('Today: ' + nowBadi.badiDate.format());
  128. res.json(json);
  129. };
  130. exports.todayHtml = function (req, res) {
  131. res.set('content-type', 'text/html;charset=utf-8');
  132. res.end(JSON.stringify(getTodayJSON(req.query).json, null, 2));
  133. };
  134. exports.date = function (req, res) {
  135. const dateInfo = getDate(req.query);
  136. // eslint-disable-next-line no-console -- CLI
  137. console.log(
  138. 'Date: ' + dateInfo.now.toString() + ' -> ' +
  139. dateInfo.nowBadi.badiDate.format()
  140. );
  141. res.json(dateInfo.json);
  142. };
  143. /**
  144. * @typedef {DateConfig} FullDateConfig
  145. * @property {Integer} year
  146. * @property {Integer} month
  147. * @property {Integer} day
  148. * @property {Integer} hour
  149. * @property {Integer} minute
  150. * @property {Integer} second
  151. */
  152. /**
  153. * @param {FullDateConfig} dateObj
  154. * @returns {BadiDateResponse}
  155. */
  156. const getDate = exports.getDate = function (dateObj) {
  157. const year = sanitizeInteger(dateObj.year);
  158. const month = sanitizeInteger(dateObj.month) - 1;
  159. const day = sanitizeInteger(dateObj.day);
  160. const hour = sanitizeInteger(dateObj.hour);
  161. const minute = sanitizeInteger(dateObj.minute);
  162. const second = sanitizeInteger(dateObj.second);
  163. const now = new Date(year, month, day, hour, minute, second);
  164. const latitude = sanitizeFloat(dateObj.latitude);
  165. const longitude = sanitizeFloat(dateObj.longitude);
  166. const timezoneId = sanitizeTimeZone(dateObj.timezoneId);
  167. const nowBadi = createDateObject(now, {
  168. latitude, longitude, timezoneId
  169. });
  170. return {
  171. now,
  172. nowBadi,
  173. json: {
  174. message: 'The date is: ' + nowBadi.badiDate.format(),
  175. badi_date: {
  176. year: nowBadi.badiDate.year,
  177. month: nowBadi.badiDate.month,
  178. day: nowBadi.badiDate.day,
  179. month_name: nowBadi.badiDate.format('MM+'),
  180. timezone_id: nowBadi.timezoneId
  181. },
  182. greg_date: {
  183. year,
  184. month: month + 1,
  185. day,
  186. hour,
  187. minute,
  188. second,
  189. timezoneOffset: now.getTimezoneOffset()
  190. }
  191. }
  192. };
  193. };