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

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion ironfish-cli/src/commands/wallet/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { DEFAULT_UNLOCK_TIMEOUT_MS, RpcRequestError } from '@ironfish/sdk'
import { TimeUtils } from '@ironfish/sdk'
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
Expand Down Expand Up @@ -59,7 +60,8 @@ export class UnlockCommand extends IronfishCommand {
'Unlocked the wallet. Call wallet:lock or stop the node to lock the wallet again.',
)
} else {
this.log(`Unlocked the wallet for ${timeout}ms`)
const timeoutDuration = TimeUtils.renderSpan(timeout)
this.log(`Unlocked the wallet for ${timeoutDuration}.`)
}

this.exit(0)
Expand Down
33 changes: 33 additions & 0 deletions ironfish/src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { tr } from 'date-fns/locale'
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 = 12 * 30 * 24 * 60.0 * 60.0 * 1000.0

/**
*
Expand All @@ -31,6 +35,9 @@ const renderEstimate = (done: number, total: number, speed: number): string => {
forceSecond: true,
forceMinute: true,
forceHour: true,
forceDay: true,
forceMonth: true,
forceYear: true,
hideMilliseconds: true,
})
}
Expand All @@ -41,6 +48,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 +68,29 @@ const renderSpan = (

const parts = []
let magnitude = 0
// Do we want to include years? Is it ok to estimate at 365 days?
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)
}

// Do we want to include months? Is it ok to estimate at 30 days?
// Should we update the abbreviation for months and minutes (both currently m) to mo and min? Do this for all time periods?
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
Loading