Skip to content

Commit

Permalink
Port from moment to date-fns
Browse files Browse the repository at this point in the history
moment.js has been deprecated for a while now. Move to date-fns'
formatRelative(), which is almost the same as moment's calendar().

Don't use cockpit's timeformat.js, as (1) we already have a centralized
formatting function in c-podman, and (2) timeformat.js currently does
not wrap formatRelative().
  • Loading branch information
martinpitt committed Jul 19, 2021
1 parent e5e8f7c commit 83b7933
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -54,8 +54,8 @@
"@patternfly/react-core": "4.135.0",
"@patternfly/react-styles": "4.11.0",
"@patternfly/react-table": "4.29.0",
"date-fns": "^2.22.1",
"docker-names": "1.1.1",
"moment": "2.29.1",
"prop-types": "15.7.2",
"react": "16.14.0",
"react-dom": "16.14.0",
Expand Down
3 changes: 0 additions & 3 deletions src/app.jsx
Expand Up @@ -28,16 +28,13 @@ import { ExclamationCircleIcon } from '@patternfly/react-icons';

import cockpit from 'cockpit';
import { superuser } from "superuser";
import moment from "moment";
import ContainerHeader from './ContainerHeader.jsx';
import Containers from './Containers.jsx';
import Images from './Images.jsx';
import * as client from './client.js';

const _ = cockpit.gettext;

moment.locale(cockpit.language);

class Application extends React.Component {
constructor(props) {
super(props);
Expand Down
6 changes: 4 additions & 2 deletions src/util.js
@@ -1,6 +1,7 @@
import cockpit from 'cockpit';

const moment = require('moment');
import * as dfnlocales from 'date-fns/locale';
import { formatRelative } from 'date-fns';
const _ = cockpit.gettext;

// https://github.com/containers/podman/blob/main/libpod/define/containerstate.go
Expand All @@ -17,7 +18,8 @@ export function truncate_id(id) {
}

export function localize_time(unix_timestamp) {
return moment(unix_timestamp * 1000).calendar();
const locale = (cockpit.language == "en") ? dfnlocales.enUS : dfnlocales[cockpit.language.replace('_', '')];
return formatRelative(unix_timestamp * 1000, Date.now(), { locale });
}

export function format_memory_and_limit(usage, limit) {
Expand Down
4 changes: 2 additions & 2 deletions test/check-application
Expand Up @@ -632,7 +632,7 @@ class TestApplication(testlib.MachineCase):
b.wait_not_in_text("#containers-images", "<none>:<none>")
b.click(".listing-action button:contains('Show intermediate images')")
b.wait_in_text("#containers-images", "<none>:<none>")
b.wait_in_text("#containers-images tbody:last-child td:nth-child(3)", "Today at")
b.wait_in_text("#containers-images tbody:last-child td:nth-child(3)", "today at")

b.click(".listing-action button:contains('Hide intermediate images')")
b.wait_not_in_text("#containers-images", "<none>:<none>")
Expand Down Expand Up @@ -1281,7 +1281,7 @@ class TestApplication(testlib.MachineCase):
b.wait_not_in_text('#containers-containers tr:contains("busybox:latest") dt:contains("Ports") + dd', '7001/tcp')
ports = self.execute(auth, "podman inspect --format '{{.NetworkSettings.Ports}}' busybox-with-tty")

b.wait_in_text('#containers-containers tr:contains("busybox:latest") dt:contains("Created") + dd', 'Today at')
b.wait_in_text('#containers-containers tr:contains("busybox:latest") dt:contains("Created") + dd', 'today at')

self.assertIn('5000/tcp:[{ 6000}]', ports)
self.assertIn('5001/udp:[{127.0.0.1 6001}]', ports)
Expand Down

0 comments on commit 83b7933

Please sign in to comment.