date-formats-K6TQue-Y.mjs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { InjectionToken, inject, LOCALE_ID } from '@angular/core';
  2. import { Subject } from 'rxjs';
  3. /** InjectionToken for datepicker that can be used to override default locale code. */
  4. const MAT_DATE_LOCALE = new InjectionToken('MAT_DATE_LOCALE', {
  5. providedIn: 'root',
  6. factory: MAT_DATE_LOCALE_FACTORY,
  7. });
  8. /**
  9. * @docs-private
  10. * @deprecated No longer used, will be removed.
  11. * @breaking-change 21.0.0
  12. */
  13. function MAT_DATE_LOCALE_FACTORY() {
  14. return inject(LOCALE_ID);
  15. }
  16. const NOT_IMPLEMENTED = 'Method not implemented';
  17. /** Adapts type `D` to be usable as a date by cdk-based components that work with dates. */
  18. class DateAdapter {
  19. /** The locale to use for all dates. */
  20. locale;
  21. _localeChanges = new Subject();
  22. /** A stream that emits when the locale changes. */
  23. localeChanges = this._localeChanges;
  24. /**
  25. * Sets the time of one date to the time of another.
  26. * @param target Date whose time will be set.
  27. * @param hours New hours to set on the date object.
  28. * @param minutes New minutes to set on the date object.
  29. * @param seconds New seconds to set on the date object.
  30. */
  31. setTime(target, hours, minutes, seconds) {
  32. throw new Error(NOT_IMPLEMENTED);
  33. }
  34. /**
  35. * Gets the hours component of the given date.
  36. * @param date The date to extract the hours from.
  37. */
  38. getHours(date) {
  39. throw new Error(NOT_IMPLEMENTED);
  40. }
  41. /**
  42. * Gets the minutes component of the given date.
  43. * @param date The date to extract the minutes from.
  44. */
  45. getMinutes(date) {
  46. throw new Error(NOT_IMPLEMENTED);
  47. }
  48. /**
  49. * Gets the seconds component of the given date.
  50. * @param date The date to extract the seconds from.
  51. */
  52. getSeconds(date) {
  53. throw new Error(NOT_IMPLEMENTED);
  54. }
  55. /**
  56. * Parses a date with a specific time from a user-provided value.
  57. * @param value The value to parse.
  58. * @param parseFormat The expected format of the value being parsed
  59. * (type is implementation-dependent).
  60. */
  61. parseTime(value, parseFormat) {
  62. throw new Error(NOT_IMPLEMENTED);
  63. }
  64. /**
  65. * Adds an amount of seconds to the specified date.
  66. * @param date Date to which to add the seconds.
  67. * @param amount Amount of seconds to add to the date.
  68. */
  69. addSeconds(date, amount) {
  70. throw new Error(NOT_IMPLEMENTED);
  71. }
  72. /**
  73. * Given a potential date object, returns that same date object if it is
  74. * a valid date, or `null` if it's not a valid date.
  75. * @param obj The object to check.
  76. * @returns A date or `null`.
  77. */
  78. getValidDateOrNull(obj) {
  79. return this.isDateInstance(obj) && this.isValid(obj) ? obj : null;
  80. }
  81. /**
  82. * Attempts to deserialize a value to a valid date object. This is different from parsing in that
  83. * deserialize should only accept non-ambiguous, locale-independent formats (e.g. a ISO 8601
  84. * string). The default implementation does not allow any deserialization, it simply checks that
  85. * the given value is already a valid date object or null. The `<mat-datepicker>` will call this
  86. * method on all of its `@Input()` properties that accept dates. It is therefore possible to
  87. * support passing values from your backend directly to these properties by overriding this method
  88. * to also deserialize the format used by your backend.
  89. * @param value The value to be deserialized into a date object.
  90. * @returns The deserialized date object, either a valid date, null if the value can be
  91. * deserialized into a null date (e.g. the empty string), or an invalid date.
  92. */
  93. deserialize(value) {
  94. if (value == null || (this.isDateInstance(value) && this.isValid(value))) {
  95. return value;
  96. }
  97. return this.invalid();
  98. }
  99. /**
  100. * Sets the locale used for all dates.
  101. * @param locale The new locale.
  102. */
  103. setLocale(locale) {
  104. this.locale = locale;
  105. this._localeChanges.next();
  106. }
  107. /**
  108. * Compares two dates.
  109. * @param first The first date to compare.
  110. * @param second The second date to compare.
  111. * @returns 0 if the dates are equal, a number less than 0 if the first date is earlier,
  112. * a number greater than 0 if the first date is later.
  113. */
  114. compareDate(first, second) {
  115. return (this.getYear(first) - this.getYear(second) ||
  116. this.getMonth(first) - this.getMonth(second) ||
  117. this.getDate(first) - this.getDate(second));
  118. }
  119. /**
  120. * Compares the time values of two dates.
  121. * @param first First date to compare.
  122. * @param second Second date to compare.
  123. * @returns 0 if the times are equal, a number less than 0 if the first time is earlier,
  124. * a number greater than 0 if the first time is later.
  125. */
  126. compareTime(first, second) {
  127. return (this.getHours(first) - this.getHours(second) ||
  128. this.getMinutes(first) - this.getMinutes(second) ||
  129. this.getSeconds(first) - this.getSeconds(second));
  130. }
  131. /**
  132. * Checks if two dates are equal.
  133. * @param first The first date to check.
  134. * @param second The second date to check.
  135. * @returns Whether the two dates are equal.
  136. * Null dates are considered equal to other null dates.
  137. */
  138. sameDate(first, second) {
  139. if (first && second) {
  140. let firstValid = this.isValid(first);
  141. let secondValid = this.isValid(second);
  142. if (firstValid && secondValid) {
  143. return !this.compareDate(first, second);
  144. }
  145. return firstValid == secondValid;
  146. }
  147. return first == second;
  148. }
  149. /**
  150. * Checks if the times of two dates are equal.
  151. * @param first The first date to check.
  152. * @param second The second date to check.
  153. * @returns Whether the times of the two dates are equal.
  154. * Null dates are considered equal to other null dates.
  155. */
  156. sameTime(first, second) {
  157. if (first && second) {
  158. const firstValid = this.isValid(first);
  159. const secondValid = this.isValid(second);
  160. if (firstValid && secondValid) {
  161. return !this.compareTime(first, second);
  162. }
  163. return firstValid == secondValid;
  164. }
  165. return first == second;
  166. }
  167. /**
  168. * Clamp the given date between min and max dates.
  169. * @param date The date to clamp.
  170. * @param min The minimum value to allow. If null or omitted no min is enforced.
  171. * @param max The maximum value to allow. If null or omitted no max is enforced.
  172. * @returns `min` if `date` is less than `min`, `max` if date is greater than `max`,
  173. * otherwise `date`.
  174. */
  175. clampDate(date, min, max) {
  176. if (min && this.compareDate(date, min) < 0) {
  177. return min;
  178. }
  179. if (max && this.compareDate(date, max) > 0) {
  180. return max;
  181. }
  182. return date;
  183. }
  184. }
  185. const MAT_DATE_FORMATS = new InjectionToken('mat-date-formats');
  186. export { DateAdapter as D, MAT_DATE_LOCALE as M, MAT_DATE_FORMATS as a, MAT_DATE_LOCALE_FACTORY as b };
  187. //# sourceMappingURL=date-formats-K6TQue-Y.mjs.map