bDateController.js 2.7 KB

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