Skip to content

Commit

Permalink
fix: units extend human time
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Feb 14, 2024
1 parent cdf540c commit ea9c20d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-coats-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siafoundation/units': minor
---

humanTime now supports up to days and long and abbreviated formatting.
33 changes: 33 additions & 0 deletions libs/units/src/humanUnits.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { humanTime } from './humanUnits'

describe('humanTime', () => {
test('milliseconds', () => {
expect(humanTime(500, { format: 'abbreviated' })).toBe('500ms')
expect(humanTime(500, { format: 'full' })).toBe('500 milliseconds')
})

test('seconds', () => {
expect(humanTime(15000, { format: 'abbreviated' })).toBe('15s')
expect(humanTime(15000, { format: 'full' })).toBe('15 seconds')
})

test('minutes', () => {
expect(humanTime(180000, { format: 'abbreviated' })).toBe('3m')
expect(humanTime(180000, { format: 'full' })).toBe('3 minutes')
})

test('hours', () => {
expect(humanTime(18000000, { format: 'abbreviated' })).toBe('5h')
expect(humanTime(18000000, { format: 'full' })).toBe('5 hours')
})

test('days', () => {
expect(humanTime(172800000000, { format: 'abbreviated' })).toBe('2000d')
expect(humanTime(172800000000, { format: 'full' })).toBe('2000 days')
})

test('handles zero', () => {
expect(humanTime(0, { format: 'abbreviated' })).toBe('0ms')
expect(humanTime(0, { format: 'full' })).toBe('0 milliseconds')
})
})
31 changes: 24 additions & 7 deletions libs/units/src/humanUnits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,33 @@ export function humanBytes(
return d.toFixed(fixed) + ' ' + units[digits]
}

export function humanTime(ns: number): string {
if (ns === 0) return '0ms'
type UnitOptions = 'long' | 'abbreviated'

ns /= Math.pow(1000, 2)
if (ns < 1000) return ` ${Math.floor(ns * 100) / 100}ms`
type HumanTimeOptions = {
format?: UnitOptions
}

export function humanTime(ms: number, options?: HumanTimeOptions): string {
const { format = 'abbreviated' } = options || {}
const abbreviate = format === 'abbreviated'

if (ms < 1000) {
return `${ms.toFixed(0)}${abbreviate ? 'ms' : ' milliseconds'}`
}

const seconds = ms / 1000
if (seconds < 60)
return `${seconds.toFixed(0)}${abbreviate ? 's' : ' seconds'}`

const minutes = seconds / 60
if (minutes < 60)
return `${minutes.toFixed(0)}${abbreviate ? 'm' : ' minutes'}`

ns /= 1000
if (ns < 60) return `${Math.floor(ns * 100) / 100}s`
const hours = minutes / 60
if (hours < 24) return `${hours.toFixed(0)}${abbreviate ? 'h' : ' hours'}`

return `${Math.floor((ns / 60) * 100) / 100}m`
const days = hours / 24
return `${days.toFixed(0)}${abbreviate ? 'd' : ' days'}`
}

type HumanNumberOptions = {
Expand Down

0 comments on commit ea9c20d

Please sign in to comment.