bDateController.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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} LocalBadiDateObject
  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 {PlainObject} greg_date
  73. * @property {Integer} greg_date.year
  74. * @property {Integer} greg_date.month
  75. * @property {Integer} greg_date.day
  76. * @property {Integer} greg_date.hour
  77. * @property {Integer} greg_date.minute
  78. * @property {Integer} greg_date.second
  79. */
  80. /**
  81. * @param {DateConfig} dateObj
  82. * @returns {LocalBadiDateObject}
  83. */
  84. const getTodayJSON = exports.getTodayJSON = function (dateObj = {}) {
  85. const now = new Date();
  86. const latitude = sanitizeFloat(dateObj.latitude);
  87. const longitude = sanitizeFloat(dateObj.longitude);
  88. const timezoneId = sanitizeTimeZone(dateObj.timezoneId);
  89. const nowBadi = createDateObject(now, {
  90. latitude, longitude, timezoneId
  91. });
  92. return {
  93. now,
  94. nowBadi,
  95. json: {
  96. message: 'Today is ' + nowBadi.badiDate.format(),
  97. badi_date: {
  98. year: nowBadi.badiDate.year,
  99. month: nowBadi.badiDate.month,
  100. day: nowBadi.badiDate.day,
  101. month_name: nowBadi.badiDate.format('MM+')
  102. },
  103. greg_date: {
  104. year: now.getFullYear(),
  105. month: now.getMonth() + 1,
  106. day: now.getDate(),
  107. hour: now.getHours(),
  108. minute: now.getMinutes(),
  109. second: now.getSeconds()
  110. }
  111. }
  112. };
  113. };
  114. exports.today = function (req, res) {
  115. const {json, nowBadi} = getTodayJSON(req.query);
  116. // eslint-disable-next-line no-console -- CLI
  117. console.log('Today: ' + nowBadi.badiDate.format());
  118. res.json(json);
  119. };
  120. exports.todayHtml = function (req, res) {
  121. res.set('content-type', 'text/html;charset=utf-8');
  122. res.end(JSON.stringify(getTodayJSON(req.query).json, null, 2));
  123. };
  124. exports.date = function (req, res) {
  125. const dateInfo = getDate(req.query);
  126. // eslint-disable-next-line no-console -- CLI
  127. console.log(
  128. 'Date: ' + dateInfo.now.toString() + ' -> ' +
  129. dateInfo.nowBadi.badiDate.format()
  130. );
  131. res.json(dateInfo.json);
  132. };
  133. const getDate = exports.getDate = function (dateObj) {
  134. const year = sanitizeInteger(dateObj.year);
  135. const month = sanitizeInteger(dateObj.month) - 1;
  136. const day = sanitizeInteger(dateObj.day);
  137. const hour = sanitizeInteger(dateObj.hour);
  138. const minute = sanitizeInteger(dateObj.minute);
  139. const second = sanitizeInteger(dateObj.second);
  140. const now = new Date(year, month, day, hour, minute, second);
  141. const latitude = sanitizeFloat(dateObj.latitude);
  142. const longitude = sanitizeFloat(dateObj.longitude);
  143. const timezoneId = sanitizeTimeZone(dateObj.timezoneId);
  144. const nowBadi = createDateObject(now, {
  145. latitude, longitude, timezoneId
  146. });
  147. return {
  148. now,
  149. nowBadi,
  150. json: {
  151. message: 'The date is: ' + nowBadi.badiDate.format(),
  152. badi_date: {
  153. year: nowBadi.badiDate.year,
  154. month: nowBadi.badiDate.month,
  155. day: nowBadi.badiDate.day,
  156. month_name: nowBadi.badiDate.format('MM+'),
  157. timezone_id: nowBadi.timezoneId
  158. },
  159. greg_date: {
  160. year,
  161. month: month + 1,
  162. day,
  163. hour,
  164. minute,
  165. second,
  166. timezoneOffset: now.getTimezoneOffset()
  167. }
  168. }
  169. };
  170. };