bDateController.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. const getTodayJSON = exports.getTodayJSON = function () {
  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. return {
  43. now,
  44. nowBadi,
  45. json: {
  46. message: 'Today is ' + nowBadi.toString(),
  47. badi_date: {
  48. year: nowBadi.getYear(),
  49. month: nowBadi.getMonth(),
  50. day: nowBadi.getDay(),
  51. month_name: nowBadi.getMonthName()
  52. },
  53. greg_date: {
  54. year: now.getFullYear(),
  55. month: now.getMonth() + 1,
  56. day: now.getDate(),
  57. hour: now.getHours(),
  58. minute: now.getMinutes(),
  59. second: now.getSeconds()
  60. }
  61. }
  62. };
  63. };
  64. exports.today = function (req, res) {
  65. const {json, nowBadi} = getTodayJSON();
  66. // eslint-disable-next-line no-console
  67. console.log('Today: ' + nowBadi.toString());
  68. res.json(json);
  69. };
  70. exports.todayHtml = function (req, res) {
  71. res.end(JSON.stringify(getTodayJSON().json, null, 2));
  72. };
  73. exports.date = function (req, res) {
  74. const dateInfo = getDate(req.query);
  75. // eslint-disable-next-line no-console
  76. console.log(
  77. 'Date: ' + dateInfo.now.toString() + ' -> ' + dateInfo.nowBadi.toString()
  78. );
  79. res.json(dateInfo.json);
  80. };
  81. const getDate = exports.getDate = function (dateObj) {
  82. const year = sanitizeInput(dateObj.year);
  83. const month = sanitizeInput(dateObj.month) - 1;
  84. const day = sanitizeInput(dateObj.day);
  85. const hour = sanitizeInput(dateObj.hour);
  86. const minute = sanitizeInput(dateObj.minute);
  87. const second = sanitizeInput(dateObj.second);
  88. const now = new Date(year, month, day, hour, minute, second);
  89. const here = {
  90. latitude: '40.712', // New York
  91. longitude: '-74.006'
  92. };
  93. const nowBadi = BadiCal.BadiDate.fromGregorianDate(now, here);
  94. return {
  95. now,
  96. nowBadi,
  97. json: {
  98. message: 'The date is: ' + nowBadi.toString(),
  99. badi_date: {
  100. year: nowBadi.getYear(),
  101. month: nowBadi.getMonth(),
  102. day: nowBadi.getDay(),
  103. month_name: nowBadi.getMonthName()
  104. },
  105. greg_date: {
  106. year,
  107. month: month + 1,
  108. day,
  109. hour,
  110. minute,
  111. second
  112. }
  113. }
  114. };
  115. };