MOBILE-2754 core: Fix words in PHP time formats

main
Dani Palou 2018-12-21 13:48:22 +01:00
parent abddc4766d
commit 78636f2e97
3 changed files with 41 additions and 10 deletions

View File

@ -38,7 +38,7 @@
</ion-item> </ion-item>
<ion-item text-wrap *ngIf="compilationTime || lastCommit"> <ion-item text-wrap *ngIf="compilationTime || lastCommit">
<h2>{{ 'core.settings.compilationinfo' | translate }}</h2> <h2>{{ 'core.settings.compilationinfo' | translate }}</h2>
<p *ngIf="compilationTime">{{ compilationTime | coreFormatDate: "LLL Z" }}</p> <p *ngIf="compilationTime">{{ compilationTime | coreFormatDate: "LLL Z": false }}</p>
<p *ngIf="lastCommit">{{ lastCommit }}</p> <p *ngIf="lastCommit">{{ lastCommit }}</p>
</ion-item> </ion-item>
<ion-item text-wrap *ngIf="fileSystemRoot"> <ion-item text-wrap *ngIf="fileSystemRoot">

View File

@ -35,9 +35,10 @@ export class CoreFormatDatePipe implements PipeTransform {
* @param {string|number} timestamp Timestamp to format (in milliseconds). If not defined, use current time. * @param {string|number} timestamp Timestamp to format (in milliseconds). If not defined, use current time.
* @param {string} [format] Format to use. It should be a string code to handle i18n (e.g. core.strftimetime). * @param {string} [format] Format to use. It should be a string code to handle i18n (e.g. core.strftimetime).
* Defaults to strftimedaydatetime. * Defaults to strftimedaydatetime.
* @param {boolean} [convert] If true, convert the format from PHP to Moment. Set it to false for Moment formats.
* @return {string} Formatted date. * @return {string} Formatted date.
*/ */
transform(timestamp: string | number, format?: string): string { transform(timestamp: string | number, format?: string, convert?: boolean): string {
timestamp = timestamp || Date.now(); timestamp = timestamp || Date.now();
format = format || 'strftimedaydatetime'; format = format || 'strftimedaydatetime';
@ -57,6 +58,11 @@ export class CoreFormatDatePipe implements PipeTransform {
format = 'core.' + format; format = 'core.' + format;
} }
return this.timeUtils.userDate(timestamp, format); if (typeof convert == 'undefined') {
// Initialize convert param. Set it to false if it's a core.df format, set it to true otherwise.
convert = format.indexOf('core.df') != 0;
}
return this.timeUtils.userDate(timestamp, format, convert);
} }
} }

View File

@ -81,16 +81,38 @@ export class CoreTimeUtilsProvider {
return ''; return '';
} }
let converted = ''; let converted = '',
escaping = false;
for (let i = 0; i < format.length; i++) { for (let i = 0; i < format.length; i++) {
let char = format[i]; let char = format[i];
if (char == '%') { if (char == '%') {
// It's a PHP format, try to convert it.
i++; i++;
char += format[i] || ''; char += format[i] || '';
}
converted += typeof this.FORMAT_REPLACEMENTS[char] != 'undefined' ? this.FORMAT_REPLACEMENTS[char] : char; if (escaping) {
// We were escaping some characters, stop doing it now.
escaping = false;
converted += ']';
}
converted += typeof this.FORMAT_REPLACEMENTS[char] != 'undefined' ? this.FORMAT_REPLACEMENTS[char] : char;
} else {
// Not a PHP format. We need to escape them, otherwise the letters could be confused with Moment formats.
if (!escaping) {
escaping = true;
converted += '[';
}
converted += char;
}
}
if (escaping) {
// Finish escaping.
converted += ']';
} }
return converted; return converted;
@ -230,23 +252,26 @@ export class CoreTimeUtilsProvider {
* *
* @param {number} timestamp Timestamp in milliseconds. * @param {number} timestamp Timestamp in milliseconds.
* @param {string} [format] The format to use (lang key). Defaults to core.strftimedaydatetime. * @param {string} [format] The format to use (lang key). Defaults to core.strftimedaydatetime.
* @param {boolean} [convert=true] If true (default), convert the format from PHP to Moment. Set it to false for Moment formats.
* @param {boolean} [fixDay=true] If true (default) then the leading zero from %d is removed. * @param {boolean} [fixDay=true] If true (default) then the leading zero from %d is removed.
* @param {boolean} [fixHour=true] If true (default) then the leading zero from %I is removed. * @param {boolean} [fixHour=true] If true (default) then the leading zero from %I is removed.
* @return {string} Readable date. * @return {string} Readable date.
*/ */
userDate(timestamp: number, format?: string, fixDay: boolean = true, fixHour: boolean = true): string { userDate(timestamp: number, format?: string, convert: boolean = true, fixDay: boolean = true, fixHour: boolean = true): string {
format = this.translate.instant(format ? format : 'core.strftimedaydatetime'); format = this.translate.instant(format ? format : 'core.strftimedaydatetime');
if (fixDay) { if (fixDay) {
format = format.replace(/%d/g, 'D'); format = format.replace(/%d/g, '%e');
} }
if (fixHour) { if (fixHour) {
format = format.replace('%I', 'h'); format = format.replace('%I', '%l');
} }
// Format could be in PHP format, convert it to moment. // Format could be in PHP format, convert it to moment.
format = this.convertPHPToMoment(format); if (convert) {
format = this.convertPHPToMoment(format);
}
return moment(timestamp).format(format); return moment(timestamp).format(format);
} }