bDateController.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. const luxon = require('luxon');
  3. const {LocalBadiDate} = require('badidate');
  4. const here = {
  5. latitude: '40.712',
  6. longitude: '-74.006',
  7. timezoneId: 'America/New York'
  8. };
  9. /**
  10. * @param {Date} date
  11. * @returns {LocalBadiDate}
  12. */
  13. function createDateObject (date) {
  14. const {latitude, longitude, timezoneId} = here;
  15. const luxonDate = luxon.DateTime.fromJSDate(date);
  16. return new LocalBadiDate(luxonDate, latitude, longitude, timezoneId);
  17. }
  18. /**
  19. * @param {any} s
  20. * @returns {Integer}
  21. */
  22. function sanitizeInput (s) {
  23. if (s === '' || s === undefined || s === null || s === false ||
  24. Number.isNaN(Number(s))) {
  25. return 0;
  26. }
  27. return Number.parseInt(s);
  28. }
  29. exports.test = function (req, res) {
  30. res.json({message: 'Hi there'});
  31. };
  32. /**
  33. * @typedef {PlainObject} LocalBadiDateObject
  34. * @property {string} message
  35. * @property {PlainObject} badi_date
  36. * @property {Integer} badi_date.year
  37. * @property {Integer} badi_date.month
  38. * @property {Integer} badi_date.day
  39. * @property {string} badi_date.month_name
  40. * @property {PlainObject} greg_date
  41. * @property {Integer} greg_date.year
  42. * @property {Integer} greg_date.month
  43. * @property {Integer} greg_date.day
  44. * @property {Integer} greg_date.hour
  45. * @property {Integer} greg_date.minute
  46. * @property {Integer} greg_date.second
  47. */
  48. /**
  49. * @returns {LocalBadiDateObject}
  50. */
  51. const getTodayJSON = exports.getTodayJSON = function () {
  52. const now = new Date();
  53. const nowBadi = createDateObject(now);
  54. return {
  55. now,
  56. nowBadi,
  57. json: {
  58. message: 'Today is ' + nowBadi.badiDate.format(),
  59. badi_date: {
  60. year: nowBadi.badiDate.year,
  61. month: nowBadi.badiDate.month,
  62. day: nowBadi.badiDate.day,
  63. month_name: nowBadi.badiDate.format('MM+')
  64. },
  65. greg_date: {
  66. year: now.getFullYear(),
  67. month: now.getMonth() + 1,
  68. day: now.getDate(),
  69. hour: now.getHours(),
  70. minute: now.getMinutes(),
  71. second: now.getSeconds()
  72. }
  73. }
  74. };
  75. };
  76. exports.today = function (req, res) {
  77. const {json, nowBadi} = getTodayJSON();
  78. // eslint-disable-next-line no-console -- CLI
  79. console.log('Today: ' + nowBadi.badiDate.format());
  80. res.json(json);
  81. };
  82. exports.todayHtml = function (req, res) {
  83. res.set('content-type', 'text/html;charset=utf-8');
  84. res.end(JSON.stringify(getTodayJSON().json, null, 2));
  85. };
  86. exports.date = function (req, res) {
  87. const dateInfo = getDate(req.query);
  88. // eslint-disable-next-line no-console -- CLI
  89. console.log(
  90. 'Date: ' + dateInfo.now.toString() + ' -> ' +
  91. dateInfo.nowBadi.badiDate.format()
  92. );
  93. res.json(dateInfo.json);
  94. };
  95. const getDate = exports.getDate = function (dateObj) {
  96. const year = sanitizeInput(dateObj.year);
  97. const month = sanitizeInput(dateObj.month) - 1;
  98. const day = sanitizeInput(dateObj.day);
  99. const hour = sanitizeInput(dateObj.hour);
  100. const minute = sanitizeInput(dateObj.minute);
  101. const second = sanitizeInput(dateObj.second);
  102. const now = new Date(year, month, day, hour, minute, second);
  103. const nowBadi = createDateObject(now);
  104. return {
  105. now,
  106. nowBadi,
  107. json: {
  108. message: 'The date is: ' + nowBadi.badiDate.format(),
  109. badi_date: {
  110. year: nowBadi.badiDate.year,
  111. month: nowBadi.badiDate.month,
  112. day: nowBadi.badiDate.day,
  113. month_name: nowBadi.badiDate.format('MM+')
  114. },
  115. greg_date: {
  116. year,
  117. month: month + 1,
  118. day,
  119. hour,
  120. minute,
  121. second
  122. }
  123. }
  124. };
  125. };