This commit is contained in:
antelle 2020-11-27 18:38:11 +01:00
parent 22754f32ec
commit f4f2ddfb3c
No known key found for this signature in database
GPG Key ID: 63C9777AAB7C563C
12 changed files with 78 additions and 41 deletions

View File

@ -2,7 +2,7 @@
import kdbxweb from 'kdbxweb'; import kdbxweb from 'kdbxweb';
import { RuntimeInfo } from 'const/runtime-info'; import { RuntimeInfo } from 'const/runtime-info';
import { Links } from 'const/links'; import { Links } from 'const/links';
import { DateFormat } from 'comp/util/date-format'; import { DateFormat } from 'comp/i18n/date-format';
import { StringFormat } from 'util/formatting/string-format'; import { StringFormat } from 'util/formatting/string-format';
import { Locale } from 'util/locale'; import { Locale } from 'util/locale';

View File

@ -2,25 +2,25 @@ import { SettingsManager } from 'comp/settings/settings-manager';
import { StringFormat } from 'util/formatting/string-format'; import { StringFormat } from 'util/formatting/string-format';
const DateFormat = { const DateFormat = {
getMonthsForLocale() { months() {
const format = new Intl.DateTimeFormat(SettingsManager.activeLocale, { month: 'long' }); const format = new Intl.DateTimeFormat(SettingsManager.activeLocale, { month: 'long' });
const months = []; const months = [];
for (let month = 0; month < 12; month++) { for (let month = 0; month < 12; month++) {
months.push(format.format(new Date(Date.UTC(2008, month)))); months.push(format.format(new Date(2008, month)));
} }
return months; return months;
}, },
getWeekDaysForLocale() { weekDays() {
const format = new Intl.DateTimeFormat(SettingsManager.activeLocale, { weekday: 'long' }); const format = new Intl.DateTimeFormat(SettingsManager.activeLocale, { weekday: 'long' });
const weekdays = []; const weekdays = [];
for (let day = 1; day < 8; day++) { for (let day = 1; day < 8; day++) {
weekdays.push(format.format(new Date(Date.UTC(2007, 9, 6 + day)))); weekdays.push(format.format(new Date(2007, 9, 6 + day)));
} }
return weekdays; return weekdays;
}, },
getWeekdaysShortForLocale() { shortWeekDays() {
const format = new Intl.DateTimeFormat(SettingsManager.activeLocale, { weekday: 'short' }); const format = new Intl.DateTimeFormat(SettingsManager.activeLocale, { weekday: 'short' });
const weekdays = []; const weekdays = [];
for (let day = 1; day < 8; day++) { for (let day = 1; day < 8; day++) {
@ -35,12 +35,8 @@ const DateFormat = {
} }
return dt return dt
? new Intl.DateTimeFormat(SettingsManager.activeLocale, { ? new Intl.DateTimeFormat(SettingsManager.activeLocale, {
year: 'numeric', dateStyle: 'medium',
month: 'short', timeStyle: 'medium'
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}).format(dt) }).format(dt)
: ''; : '';
}, },

View File

@ -16,7 +16,7 @@ import { YubiKeyOtpModel } from 'models/external/yubikey-otp-model';
import { MenuModel } from 'models/menu/menu-model'; import { MenuModel } from 'models/menu/menu-model';
import { PluginManager } from 'plugins/plugin-manager'; import { PluginManager } from 'plugins/plugin-manager';
import { Features } from 'util/features'; import { Features } from 'util/features';
import { DateFormat } from 'comp/util/date-format'; import { DateFormat } from 'comp/i18n/date-format';
import { UrlFormat } from 'util/formatting/url-format'; import { UrlFormat } from 'util/formatting/url-format';
import { IdGenerator } from 'util/generators/id-generator'; import { IdGenerator } from 'util/generators/id-generator';
import { Locale } from 'util/locale'; import { Locale } from 'util/locale';

View File

@ -1,4 +1,4 @@
import { DateFormat } from 'comp/util/date-format'; import { DateFormat } from 'comp/i18n/date-format';
import { Locale } from 'util/locale'; import { Locale } from 'util/locale';
const EntryPresenter = function (descField, noColor, activeEntryId) { const EntryPresenter = function (descField, noColor, activeEntryId) {

View File

@ -1,6 +1,6 @@
import { Locale } from 'util/locale'; import { Locale } from 'util/locale';
import { StringFormat } from 'util/formatting/string-format'; import { StringFormat } from 'util/formatting/string-format';
import { DateFormat } from 'comp/util/date-format'; import { DateFormat } from 'comp/i18n/date-format';
import { AppModel } from 'models/app-model'; import { AppModel } from 'models/app-model';
import { FieldViewReadOnly } from 'views/fields/field-view-read-only'; import { FieldViewReadOnly } from 'views/fields/field-view-read-only';
import { FieldViewOtp } from 'views/fields/field-view-otp'; import { FieldViewOtp } from 'views/fields/field-view-otp';

View File

@ -1,7 +1,7 @@
import { View } from 'framework/views/view'; import { View } from 'framework/views/view';
import { Alerts } from 'comp/ui/alerts'; import { Alerts } from 'comp/ui/alerts';
import { Keys } from 'const/keys'; import { Keys } from 'const/keys';
import { DateFormat } from 'comp/util/date-format'; import { DateFormat } from 'comp/i18n/date-format';
import { StringFormat } from 'util/formatting/string-format'; import { StringFormat } from 'util/formatting/string-format';
import { Locale } from 'util/locale'; import { Locale } from 'util/locale';
import { Copyable } from 'framework/views/copyable'; import { Copyable } from 'framework/views/copyable';

View File

@ -1,5 +1,5 @@
import Pikaday from 'pikaday'; import Pikaday from 'pikaday';
import { DateFormat } from 'comp/util/date-format'; import { DateFormat } from 'comp/i18n/date-format';
import { FieldViewText } from 'views/fields/field-view-text'; import { FieldViewText } from 'views/fields/field-view-text';
class FieldViewDate extends FieldViewText { class FieldViewDate extends FieldViewText {
@ -29,9 +29,9 @@ class FieldViewDate extends FieldViewText {
i18n: { i18n: {
previousMonth: '', previousMonth: '',
nextMonth: '', nextMonth: '',
months: DateFormat.getMonthsForLocale(), months: DateFormat.months(),
weekdays: DateFormat.getWeekDaysForLocale(), weekdays: DateFormat.weekDays(),
weekdaysShort: DateFormat.getWeekdaysShortForLocale() weekdaysShort: DateFormat.shortWeekDays()
} }
}); });
this.picker.adjustPosition = this.adjustPickerPosition.bind(this); this.picker.adjustPosition = this.adjustPickerPosition.bind(this);

View File

@ -8,7 +8,7 @@ import { YubiKey } from 'comp/app/yubikey';
import { UsbListener } from 'comp/app/usb-listener'; import { UsbListener } from 'comp/app/usb-listener';
import { Links } from 'const/links'; import { Links } from 'const/links';
import { AppSettingsModel } from 'models/app-settings-model'; import { AppSettingsModel } from 'models/app-settings-model';
import { DateFormat } from 'comp/util/date-format'; import { DateFormat } from 'comp/i18n/date-format';
import { UrlFormat } from 'util/formatting/url-format'; import { UrlFormat } from 'util/formatting/url-format';
import { PasswordPresenter } from 'util/formatting/password-presenter'; import { PasswordPresenter } from 'util/formatting/password-presenter';
import { Locale } from 'util/locale'; import { Locale } from 'util/locale';

View File

@ -12,7 +12,7 @@ import { AppSettingsModel } from 'models/app-settings-model';
import { UpdateModel } from 'models/update-model'; import { UpdateModel } from 'models/update-model';
import { SemVer } from 'util/data/semver'; import { SemVer } from 'util/data/semver';
import { Features } from 'util/features'; import { Features } from 'util/features';
import { DateFormat } from 'comp/util/date-format'; import { DateFormat } from 'comp/i18n/date-format';
import { Locale } from 'util/locale'; import { Locale } from 'util/locale';
import { SettingsLogsView } from 'views/settings/settings-logs-view'; import { SettingsLogsView } from 'views/settings/settings-logs-view';
import { SettingsPrvView } from 'views/settings/settings-prv-view'; import { SettingsPrvView } from 'views/settings/settings-prv-view';

View File

@ -10,7 +10,7 @@ import { PluginManager } from 'plugins/plugin-manager';
import { Comparators } from 'util/data/comparators'; import { Comparators } from 'util/data/comparators';
import { SemVer } from 'util/data/semver'; import { SemVer } from 'util/data/semver';
import { Features } from 'util/features'; import { Features } from 'util/features';
import { DateFormat } from 'comp/util/date-format'; import { DateFormat } from 'comp/i18n/date-format';
import { Locale } from 'util/locale'; import { Locale } from 'util/locale';
import template from 'templates/settings/settings-plugins.hbs'; import template from 'templates/settings/settings-plugins.hbs';

View File

@ -0,0 +1,59 @@
import { expect } from 'chai';
import { DateFormat } from 'comp/i18n/date-format';
describe('DateFormat', () => {
const dt = new Date(2020, 0, 2, 3, 4, 5, 6);
it('should return months', () => {
expect(DateFormat.months()).to.eql([
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]);
});
it('should return week days', () => {
expect(DateFormat.weekDays()).to.eql([
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
]);
});
it('should return short week days', () => {
expect(DateFormat.shortWeekDays()).to.eql([
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat'
]);
});
it('should format date', () => {
expect(DateFormat.dStr(dt)).to.eql('Jan 2, 2020');
});
it('should format date and time', () => {
expect(DateFormat.dtStr(dt)).to.eql('Jan 2, 2020, 3:04:05 AM');
});
it('should format date and time in sortable format', () => {
expect(DateFormat.dtStrFs(dt)).to.eql('2020-01-02T03-04-05');
});
});

View File

@ -1,18 +0,0 @@
import { expect } from 'chai';
import { DateFormat } from 'comp/util/date-format';
describe('DateFormat', () => {
const dt = new Date(2020, 0, 2, 3, 4, 5, 6);
it('should format date', () => {
expect(DateFormat.dStr(dt)).to.eql('Jan 2, 2020');
});
it('should format date and time', () => {
expect(DateFormat.dtStr(dt)).to.eql('Jan 2, 2020, 03:04:05');
});
it('should format date and time in sortable format', () => {
expect(DateFormat.dtStrFs(dt)).to.eql('2020-01-02T03-04-05');
});
});