Skip to content

Commit

Permalink
renderSpan: add more time intervals (#5568)
Browse files Browse the repository at this point in the history
* wallet unlock log now uses renderspan w/ hr, min, sec

* Update unlock.ts

Fixed linting error:  added comma after forceMinute

* Removed renderSpan options from unlock

* added Year, Month, Day to renderSpan

* updated year to 365 days, updated month abbreviation to 'M', and removed unused tr import

* added tests for time util for renderSpan and renderEstimate

* changed forced intervals to hr, m, s

* added removed space
  • Loading branch information
j-s-n authored Jan 4, 2025
1 parent 56fdf55 commit 3a87bda
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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')
})
})
})
30 changes: 30 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 @@ -59,6 +68,27 @@ const renderSpan = (
const parts = []
let magnitude = 0

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)
time -= hours * MS_PER_HOUR
Expand Down

0 comments on commit 3a87bda

Please sign in to comment.