Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/util/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,29 @@ export function get_short_month_labels(locale?: string | string[]) {
new Intl.DateTimeFormat(locale, { month: 'short' }).format(new Date(2020, month, 1, 12))
);
}

// Format a date as locale-aware short date (e.g. "Jun 15" in en-US, "15 jun" in sv-SE)
export function format_date_short(date: Date | string, locale?: string | string[]) {
const d = date instanceof Date ? date : new Date(date);
return new Intl.DateTimeFormat(locale, { month: 'short', day: 'numeric' }).format(d);
}

// Format a date with weekday prefix (e.g. "Mon, Jun 15" in en-US, "mån 15 jun" in sv-SE)
export function format_date_with_weekday(date: Date | string, locale?: string | string[]) {
const d = date instanceof Date ? date : new Date(date);
return new Intl.DateTimeFormat(locale, {
weekday: 'short',
month: 'short',
day: 'numeric',
}).format(d);
}

// Format a time-of-day string locale-aware (e.g. "14:30:05" or "2:30:05 PM" per browser locale)
export function format_time_of_day(date: Date | string, locale?: string | string[]) {
const d = date instanceof Date ? date : new Date(date);
return new Intl.DateTimeFormat(locale, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
}).format(d);
Comment thread
TimeToBuildBob marked this conversation as resolved.
}
14 changes: 8 additions & 6 deletions src/views/Trends.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ div

<script lang="ts">
import moment from 'moment';
import { get_today_with_offset } from '~/util/time';
import { get_today_with_offset, format_date_short, format_date_with_weekday } from '~/util/time';
import { buildBarchartDataset } from '~/util/datasets';
import { canonicalEvents } from '~/queries';
import { getClient } from '~/util/awclient';
Expand Down Expand Up @@ -161,14 +161,16 @@ export default {
},

currentRangeLabel(): string {
const start = this.currentStart.format('MMM D');
const end = moment(this.today).format('MMM D');
const start = format_date_short(this.currentStart.toDate());
const end = format_date_short(moment(this.today).toDate());
return `${start} – ${end}`;
},

previousRangeLabel(): string {
const start = this.currentStart.clone().subtract(this.periodDays, 'days').format('MMM D');
const end = moment(this.today).subtract(this.periodDays, 'days').format('MMM D');
const start = format_date_short(
this.currentStart.clone().subtract(this.periodDays, 'days').toDate()
);
const end = format_date_short(moment(this.today).subtract(this.periodDays, 'days').toDate());
return `${start} – ${end}`;
},

Expand Down Expand Up @@ -210,7 +212,7 @@ export default {
const total = evts.reduce((a, e) => a + (e.duration || 0), 0);
if (!best || total > best.duration) {
const start = period.split('/')[0];
best = { label: moment(start).format('ddd, MMM D'), duration: total };
best = { label: format_date_with_weekday(new Date(start)), duration: total };
}
}
return best && best.duration > 0 ? best : null;
Expand Down
4 changes: 2 additions & 2 deletions src/visualizations/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import _ from 'lodash';
import moment from 'moment';

import { getColorFromString } from '../util/color';
import { seconds_to_duration } from '../util/time';
import { seconds_to_duration, format_time_of_day } from '../util/time';
import { IEvent } from '../util/interfaces';

function show_info(container: HTMLElement, elem_id: string): void {
Expand Down Expand Up @@ -148,7 +148,7 @@ function update(
_.each(e.data.subevents, function (t: IEvent) {
const inforow = infolist.append('tr');
// Clocktime
const clocktime = moment(t.timestamp).format('HH:mm:ss');
const clocktime = format_time_of_day(new Date(t.timestamp));
inforow.append('td').text(clocktime);
// Duration
const duration = seconds_to_duration(t.duration);
Expand Down
Loading