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

renderSpan: add more time intervals #5568

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
11 changes: 11 additions & 0 deletions ironfish/src/utils/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('TimeUtils', () => {
expect(TimeUtils.renderEstimate(50, 100, 20)).toEqual('2s')
expect(TimeUtils.renderEstimate(50, 200, 1)).toEqual('2m 30s')
expect(TimeUtils.renderEstimate(10, 10000, 1)).toEqual('2h 46m 30s')
expect(TimeUtils.renderEstimate(10, 198010, 1)).toEqual('2d 7h')
expect(TimeUtils.renderEstimate(10, 7689610, 1)).toEqual('2M 29d')
expect(TimeUtils.renderEstimate(10, 73699210, 1)).toEqual('2y 4M 72h')
})

it('should render time spans', () => {
Expand All @@ -21,6 +24,10 @@ describe('TimeUtils', () => {
expect(TimeUtils.renderSpan(1150)).toEqual('1s 150ms')
expect(TimeUtils.renderSpan(330000)).toEqual('5m 30s')
expect(TimeUtils.renderSpan(7530000)).toEqual('2h 5m')
expect(TimeUtils.renderSpan(90000000)).toEqual('1d 1h')
expect(TimeUtils.renderSpan(7775940000)).toEqual('2M 29d')
expect(TimeUtils.renderSpan(31622400000)).toEqual('1y')
expect(TimeUtils.renderSpan(71193600000)).toEqual('2y 3M')
})

it('should render negative times', () => {
Expand All @@ -31,6 +38,10 @@ describe('TimeUtils', () => {
expect(TimeUtils.renderSpan(-1150)).toEqual('-1s 150ms')
expect(TimeUtils.renderSpan(-330000)).toEqual('-5m 30s')
expect(TimeUtils.renderSpan(-7530000)).toEqual('-2h 5m')
expect(TimeUtils.renderSpan(-90000000)).toEqual('-1d 1h')
expect(TimeUtils.renderSpan(-7775940000)).toEqual('-2M 29d')
expect(TimeUtils.renderSpan(-31622400000)).toEqual('-1y')
expect(TimeUtils.renderSpan(-71193600000)).toEqual('-2y 3M')
})
})
})
29 changes: 29 additions & 0 deletions ironfish/src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { MathUtils } from './math'
const MS_PER_SEC = 1000.0
const MS_PER_MIN = 60.0 * 1000.0
const MS_PER_HOUR = 60.0 * 60.0 * 1000.0
const MS_PER_DAY = 24 * 60.0 * 60.0 * 1000.0
const MS_PER_MONTH = 30 * 24 * 60.0 * 60.0 * 1000.0
const MS_PER_YEAR = 365 * 24 * 60.0 * 60.0 * 1000.0

/**
*
Expand All @@ -31,6 +34,9 @@ const renderEstimate = (done: number, total: number, speed: number): string => {
forceSecond: true,
forceMinute: true,
forceHour: true,
forceDay: false,
forceMonth: false,
forceYear: false,
hideMilliseconds: true,
})
}
Expand All @@ -41,6 +47,9 @@ const renderEstimate = (done: number, total: number, speed: number): string => {
const renderSpan = (
time: number,
options?: {
forceYear?: boolean
forceMonth?: boolean
forceDay?: boolean
forceHour?: boolean
forceMinute?: boolean
forceSecond?: boolean
Expand All @@ -58,6 +67,26 @@ const renderSpan = (

const parts = []
let magnitude = 0
NullSoldier marked this conversation as resolved.
Show resolved Hide resolved
if (time >= MS_PER_YEAR && (magnitude <= 8 || options?.forceYear)) {
const years = Math.floor(time / MS_PER_YEAR)
time -= years * MS_PER_YEAR
parts.push(`${years.toFixed(0)}y`)
magnitude = Math.max(magnitude, 7)
}

if (time >= MS_PER_MONTH && (magnitude <= 7 || options?.forceMonth)) {
const months = Math.floor(time / MS_PER_MONTH)
time -= months * MS_PER_MONTH
parts.push(`${months.toFixed(0)}M`)
magnitude = Math.max(magnitude, 6)
}

if (time >= MS_PER_DAY && (magnitude <= 6 || options?.forceDay)) {
const days = Math.floor(time / MS_PER_DAY)
time -= days * MS_PER_DAY
parts.push(`${days.toFixed(0)}d`)
magnitude = Math.max(magnitude, 5)
}

if (time >= MS_PER_HOUR && (magnitude <= 5 || options?.forceHour)) {
const hours = Math.floor(time / MS_PER_HOUR)
Expand Down