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 text-wrap *ngIf="compilationTime || lastCommit">
<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>
</ion-item>
<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} [format] Format to use. It should be a string code to handle i18n (e.g. core.strftimetime).
* 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.
*/
transform(timestamp: string | number, format?: string): string {
transform(timestamp: string | number, format?: string, convert?: boolean): string {
timestamp = timestamp || Date.now();
format = format || 'strftimedaydatetime';
@ -57,6 +58,11 @@ export class CoreFormatDatePipe implements PipeTransform {
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 '';
}
let converted = '';
let converted = '',
escaping = false;
for (let i = 0; i < format.length; i++) {
let char = format[i];
if (char == '%') {
// It's a PHP format, try to convert it.
i++;
char += format[i] || '';
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;
@ -230,23 +252,26 @@ export class CoreTimeUtilsProvider {
*
* @param {number} timestamp Timestamp in milliseconds.
* @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} [fixHour=true] If true (default) then the leading zero from %I is removed.
* @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');
if (fixDay) {
format = format.replace(/%d/g, 'D');
format = format.replace(/%d/g, '%e');
}
if (fixHour) {
format = format.replace('%I', 'h');
format = format.replace('%I', '%l');
}
// Format could be in PHP format, convert it to moment.
if (convert) {
format = this.convertPHPToMoment(format);
}
return moment(timestamp).format(format);
}