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
19 changes: 19 additions & 0 deletions apps/chrome-extension/src/shared/format-duration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from "vitest";
import { formatDuration, formatRecordedDuration } from "./format-duration";

describe("recording durations", () => {
it("keeps M:SS under an hour", () => {
expect(formatDuration(0)).toBe("0:00");
expect(formatDuration(95_700)).toBe("1:35");
expect(formatDuration(3_599_999)).toBe("59:59");
expect(formatRecordedDuration(400)).toBe("0:01");
expect(formatRecordedDuration(95_500)).toBe("1:36");
});

it("shows hours from an hour on instead of counting minutes past 59", () => {
expect(formatDuration(3_600_000)).toBe("1:00:00");
expect(formatDuration(3_905_900)).toBe("1:05:05");
expect(formatRecordedDuration(3_599_600)).toBe("1:00:00");
expect(formatRecordedDuration(3_905_000)).toBe("1:05:05");
});
});
25 changes: 13 additions & 12 deletions apps/chrome-extension/src/shared/format-duration.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const formatClock = (totalSeconds: number) => {
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor(totalSeconds / 60) % 60;
const seconds = (totalSeconds % 60).toString().padStart(2, "0");
return hours > 0
? `${hours}:${minutes.toString().padStart(2, "0")}:${seconds}`
: `${minutes}:${seconds}`;
};

// Live timers (recording bars) count up from 0:00 and truncate so the
// display never runs ahead of the actual elapsed time.
export const formatDuration = (durationMs: number) => {
const totalSeconds = Math.max(0, Math.floor(durationMs / 1000));
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes}:${seconds.toString().padStart(2, "0")}`;
};
export const formatDuration = (durationMs: number) =>
formatClock(Math.max(0, Math.floor(durationMs / 1000)));

// Finished recordings round to the nearest second and never show 0:00 — a
// sub-second capture still represents real data.
export const formatRecordedDuration = (durationMs: number) => {
const totalSeconds = Math.max(1, Math.round(durationMs / 1000));
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes}:${seconds.toString().padStart(2, "0")}`;
};
export const formatRecordedDuration = (durationMs: number) =>
formatClock(Math.max(1, Math.round(durationMs / 1000)));
12 changes: 2 additions & 10 deletions apps/mobile/app/caps/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { GlassSurface } from "@/components/GlassSurface";
import { ReactionBar, type ReactionCount } from "@/components/ReactionBar";
import { Screen } from "@/components/Screen";
import { colors, fonts, radius, squircle } from "@/theme";
import { formatRelativeDate } from "@/utils/format";
import { formatChapterTime, formatRelativeDate } from "@/utils/format";

const showPhotosSettingsAlert = () => {
if (Platform.OS === "ios") {
Expand Down Expand Up @@ -106,14 +106,6 @@ const sectionEntrance = (order: number) =>
.stiffness(220)
.delay(order * 55);

const formatChapterTime = (start: number) => {
const minutes = Math.floor(start / 60);
const seconds = Math.floor(start % 60)
.toString()
.padStart(2, "0");
return `${minutes}:${seconds}`;
};

type CapDetailOperation = "comment" | "safety" | "save" | "visibility";

type AnalyticsMetricProps = {
Expand Down Expand Up @@ -1691,7 +1683,7 @@ const styles = StyleSheet.create({
borderColor: colors.gray5,
},
chapterTime: {
width: 44,
minWidth: 44,
fontFamily: fonts.medium,
fontSize: 13,
color: colors.blue11,
Expand Down
15 changes: 14 additions & 1 deletion apps/mobile/src/utils/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { describe, expect, it } from "vitest";
import { formatDuration, formatFileSize, formatRelativeDate } from "./format";
import {
formatChapterTime,
formatDuration,
formatFileSize,
formatRelativeDate,
} from "./format";

describe("mobile formatters", () => {
it("formats card dates like Cap web", () => {
Expand All @@ -24,6 +29,14 @@ describe("mobile formatters", () => {
expect(formatDuration(7200)).toBe("2 hrs");
});

it("formats chapter times with hours from an hour on", () => {
expect(formatChapterTime(0)).toBe("0:00");
expect(formatChapterTime(95.7)).toBe("1:35");
expect(formatChapterTime(3599.9)).toBe("59:59");
expect(formatChapterTime(3600)).toBe("1:00:00");
expect(formatChapterTime(3905)).toBe("1:05:05");
});

it("formats native upload file sizes", () => {
expect(formatFileSize(null)).toBeNull();
expect(formatFileSize(0)).toBeNull();
Expand Down
10 changes: 10 additions & 0 deletions apps/mobile/src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export const formatDuration = (seconds: number | null) => {
return "< 1 sec";
};

export const formatChapterTime = (seconds: number) => {
const total = Math.max(0, Math.floor(seconds));
const hours = Math.floor(total / 3600);
const minutes = Math.floor(total / 60) % 60;
const secs = (total % 60).toString().padStart(2, "0");
return hours > 0
? `${hours}:${minutes.toString().padStart(2, "0")}:${secs}`
: `${minutes}:${secs}`;
};

export const formatFileSize = (bytes: number | null | undefined) => {
if (bytes === null || bytes === undefined || !Number.isFinite(bytes)) {
return null;
Expand Down
Loading