MCPcopy Index your code
hub / github.com/angular/angular / getDateTranslation

Function getDateTranslation

packages/common/src/i18n/format_date.ts:387–457  ·  view source on GitHub ↗

* Returns the locale translation of a date for a given form, type and width

(
  date: Date,
  locale: string,
  name: TranslationType,
  width: TranslationWidth,
  form: FormStyle,
  extended: boolean,
)

Source from the content-addressed store, hash-verified

385 * Returns the locale translation of a date for a given form, type and width
386 */
387function getDateTranslation(
388 date: Date,
389 locale: string,
390 name: TranslationType,
391 width: TranslationWidth,
392 form: FormStyle,
393 extended: boolean,
394) {
395 switch (name) {
396 case TranslationType.Months:
397 return getLocaleMonthNames(locale, form, width)[date.getMonth()];
398 case TranslationType.Days:
399 return getLocaleDayNames(locale, form, width)[date.getDay()];
400 case TranslationType.DayPeriods:
401 const currentHours = date.getHours();
402 const currentMinutes = date.getMinutes();
403 if (extended) {
404 const rules = getLocaleExtraDayPeriodRules(locale);
405 const dayPeriods = getLocaleExtraDayPeriods(locale, form, width);
406 const index = rules.findIndex((rule) => {
407 if (Array.isArray(rule)) {
408 // morning, afternoon, evening, night
409 const [from, to] = rule;
410 const afterFrom = currentHours >= from.hours && currentMinutes >= from.minutes;
411 const beforeTo =
412 currentHours < to.hours || (currentHours === to.hours && currentMinutes < to.minutes);
413 // We must account for normal rules that span a period during the day (e.g. 6am-9am)
414 // where `from` is less (earlier) than `to`. But also rules that span midnight (e.g.
415 // 10pm - 5am) where `from` is greater (later!) than `to`.
416 //
417 // In the first case the current time must be BOTH after `from` AND before `to`
418 // (e.g. 8am is after 6am AND before 10am).
419 //
420 // In the second case the current time must be EITHER after `from` OR before `to`
421 // (e.g. 4am is before 5am but not after 10pm; and 11pm is not before 5am but it is
422 // after 10pm).
423 if (from.hours < to.hours) {
424 if (afterFrom && beforeTo) {
425 return true;
426 }
427 } else if (afterFrom || beforeTo) {
428 return true;
429 }
430 } else {
431 // noon or midnight
432 if (rule.hours === currentHours && rule.minutes === currentMinutes) {
433 return true;
434 }
435 }
436 return false;
437 });
438 if (index !== -1) {
439 return dayPeriods[index];
440 }
441 }
442 // if no rules for the day periods, we use am/pm by default
443 return getLocaleDayPeriods(locale, form, <TranslationWidth>width)[currentHours < 12 ? 0 : 1];
444 case TranslationType.Eras:

Callers 1

dateStrGetterFunction · 0.85

Calls 7

getLocaleMonthNamesFunction · 0.90
getLocaleDayNamesFunction · 0.90
getLocaleExtraDayPeriodsFunction · 0.90
getLocaleDayPeriodsFunction · 0.90
getLocaleEraNamesFunction · 0.90
isArrayMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…