Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace uses of MM/DD/YYYY #27

Merged
merged 8 commits into from
Feb 8, 2023
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
2 changes: 1 addition & 1 deletion web/app/components/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export default class Sidebar extends Component {
}
}

get shortLinkBaseURL() {
get shortLinkBaseURL() {
return this.configSvc.config.short_link_base_url;
}

Expand Down
11 changes: 8 additions & 3 deletions web/app/helpers/parse-date.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { helper } from "@ember/component/helper";
import parseDate from "hermes/utils/parse-date";

export default helper(([time]: [string | number | Date | undefined]) => {
return parseDate(time);
});
export default helper(
([time, monthFormat = "short"]: [
string | number | Date | undefined,
"short" | "long"
]) => {
return parseDate(time, monthFormat);
}
);
4 changes: 1 addition & 3 deletions web/app/routes/authenticated/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ export default class DocumentRoute extends Route {
}
}
if (!!doc.createdTime) {
doc.createdDate =
parseDate(doc.createdTime * 1000) +
` (${timeAgo(new Date(doc.createdTime * 1000))})`;
doc.createdDate = parseDate(doc.createdTime * 1000, "long");
}

// Build strings for created and last-modified.
Expand Down
21 changes: 12 additions & 9 deletions web/app/utils/parse-date.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
/**
* Converts a valid Date/timestamp into a "MM/DD/YYYY"-formatted string.
* Converts a valid Date/timestamp into a string formatted like
* "21 Dec. 2023" or "21 December 2023" (if monthFormat is "long").
* Returns null if the time parameter is invalid.
*/

export default function parseDate(
time?: string | number | Date
time?: string | number | Date,
monthFormat: "short" | "long" = "short"
): string | null {
if (!time) {
return null;
}
const date = new Date(time);

// `getMonth()` returns a zero-indexed month so we add 1
let month = date.getMonth() + 1;
if (isNaN(date.getTime())) {
return null;
}

let day = date.getDate();
let year = date.getFullYear();
let month = date.toLocaleString("default", { month: monthFormat });

// Nullify invalid timestamps
if (isNaN(month) || isNaN(day) || isNaN(year)) {
return null;
if (monthFormat === "short") {
month += ".";
}

return `${month}/${day}/${year}`;
return `${day} ${month} ${year}`;
}
8 changes: 7 additions & 1 deletion web/tests/integration/helpers/parse-date-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ module("Integration | Helper | parse-date", function (hooks) {
<div class="valid">
{{parse-date 628021800000}}
</div>
<div class="long">
{{parse-date 628021800000 "long"}}
</div>
<div class="invalid">
{{or (parse-date undefined) "Unknown"}}
</div>
`);

assert
.dom(".valid")
.hasText("11/25/1989", "A valid date renders correctly");
.hasText("25 Nov. 1989", "A valid date renders correctly");
assert
.dom(".long")
.hasText("25 November 1989", "The long format renders correctly");
assert.dom(".invalid").hasText("Unknown", "An invalid date returns null");
});
});
16 changes: 8 additions & 8 deletions web/tests/unit/utils/parse-date-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import MockDate from "mockdate";

module("Unit | Utility | parse-date", function () {
// Make sure the date is always the same
// TODO: Freeze timezone
MockDate.set("2000-01-01T06:00:00.000-07:00");

test("it parses dates", function (assert) {
// Valid
assert.equal(parseDate(1), "12/31/1969");
assert.equal(parseDate(628021800000), "11/25/1989");
assert.equal(parseDate(628021800000 * 100), "2/14/3960");
assert.equal(parseDate("12/23/20"), "12/23/2020");
assert.equal(parseDate("1980/12/20"), "12/20/1980");
assert.equal(parseDate("November 21, 1963"), "11/21/1963");
assert.equal(parseDate("November 21, 1963 12:30"), "11/21/1963");
assert.equal(parseDate("2000-01-01T06:00:00.000-07:00"), "1/1/2000");
assert.equal(parseDate(628021800000), "25 Nov. 1989");
assert.equal(parseDate(628021800000 * 100), "14 Feb. 3960");
assert.equal(parseDate("12/23/20", "long"), "23 December 2020");
assert.equal(parseDate("1980/12/20"), "20 Dec. 1980");
assert.equal(parseDate("November 21, 1963", "long"), "21 November 1963");
assert.equal(parseDate("November 21, 1963 12:30"), "21 Nov. 1963");
assert.equal(parseDate("2000-01-01T06:00:00.000-07:00"), "1 Jan. 2000");

// Invalid
assert.equal(parseDate(undefined), null);
Expand Down