bDateController.js 3.1 KB

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