Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateManager.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import i18n from 'i18n-js';
  20. /**
  21. * Singleton used to manage date translations.
  22. * Translations are hardcoded as toLocaleDateString does not work on current android JS engine
  23. */
  24. export default class DateManager {
  25. static instance: DateManager | null = null;
  26. daysOfWeek: Array<string> = [];
  27. monthsOfYear: Array<string> = [];
  28. constructor() {
  29. this.daysOfWeek.push(i18n.t('date.daysOfWeek.sunday')); // 0 represents sunday
  30. this.daysOfWeek.push(i18n.t('date.daysOfWeek.monday'));
  31. this.daysOfWeek.push(i18n.t('date.daysOfWeek.tuesday'));
  32. this.daysOfWeek.push(i18n.t('date.daysOfWeek.wednesday'));
  33. this.daysOfWeek.push(i18n.t('date.daysOfWeek.thursday'));
  34. this.daysOfWeek.push(i18n.t('date.daysOfWeek.friday'));
  35. this.daysOfWeek.push(i18n.t('date.daysOfWeek.saturday'));
  36. this.monthsOfYear.push(i18n.t('date.monthsOfYear.january'));
  37. this.monthsOfYear.push(i18n.t('date.monthsOfYear.february'));
  38. this.monthsOfYear.push(i18n.t('date.monthsOfYear.march'));
  39. this.monthsOfYear.push(i18n.t('date.monthsOfYear.april'));
  40. this.monthsOfYear.push(i18n.t('date.monthsOfYear.may'));
  41. this.monthsOfYear.push(i18n.t('date.monthsOfYear.june'));
  42. this.monthsOfYear.push(i18n.t('date.monthsOfYear.july'));
  43. this.monthsOfYear.push(i18n.t('date.monthsOfYear.august'));
  44. this.monthsOfYear.push(i18n.t('date.monthsOfYear.september'));
  45. this.monthsOfYear.push(i18n.t('date.monthsOfYear.october'));
  46. this.monthsOfYear.push(i18n.t('date.monthsOfYear.november'));
  47. this.monthsOfYear.push(i18n.t('date.monthsOfYear.december'));
  48. }
  49. /**
  50. * Get this class instance or create one if none is found
  51. * @returns {DateManager}
  52. */
  53. static getInstance(): DateManager {
  54. if (DateManager.instance == null) {
  55. DateManager.instance = new DateManager();
  56. }
  57. return DateManager.instance;
  58. }
  59. static isWeekend(date: Date): boolean {
  60. return date.getDay() === 6 || date.getDay() === 0;
  61. }
  62. getMonthsOfYear(): Array<string> {
  63. return this.monthsOfYear;
  64. }
  65. /**
  66. * Gets a translated string representing the given date.
  67. *
  68. * @param dateString The date with the format YYYY-MM-DD
  69. * @return {string} The translated string
  70. */
  71. getTranslatedDate(dateString: string): string {
  72. const dateArray = dateString.split('-');
  73. const date = new Date();
  74. date.setFullYear(
  75. parseInt(dateArray[0], 10),
  76. parseInt(dateArray[1], 10) - 1,
  77. parseInt(dateArray[2], 10)
  78. );
  79. return `${this.daysOfWeek[date.getDay()]} ${date.getDate()} ${
  80. this.monthsOfYear[date.getMonth()]
  81. } ${date.getFullYear()}`;
  82. }
  83. }