How to implement milliseconds in UnitDuration?

The class UnitDuration lets you use seconds, minutes and hours, but not milliseconds.


On the apple site I found this in relation to it:

@NSCopying class var milliseconds: UnitDuration { get }


How can I implement this into my code? (I am new to swift)


Thanks in advance

Accepted Reply

According to the doc, you can define your own subclass of Dimension or Unit .

Or you might write your own implementation of `UnitDuration.milliseconds`.


But I do not have enough experience about it.


First try:

extension UnitDuration {
    static let msec: UnitDuration = {
        UnitDuration(symbol: "msec", converter: UnitConverterLinear(coefficient: 0.001))
    }()
}

let timePeriod = 123.45678
let timeMesure = Measurement(value: timePeriod * 1000, unit: UnitDuration.msec)
print(timeMesure) //-> 123456.78 msec

Replies

What do you want to do exactly ?


If you have a duration and want to display as hh mn ss ms:


let duration = 125.42     // in seconds
let ti = NSInteger(duration)

let ms = Int((duration.truncatingRemainder(dividingBy: 1)) * 1000)

let seconds = ti % 60
let minutes = (ti / 60) % 60
let hours = (ti / 3600)

let tt = String(format: "%0.2d:%0.2d:%0.2d.%0.3d",hours,minutes,seconds,ms)
print(tt)


.milliseconds can be used for unit conversion (transforming minutes in ms for instance)

See here for unit conversion

h ttps://www.hackingwithswift.com/example-code/system/how-to-convert-units-using-unit-and-measurement

I am trying to return a measurement value with the units "ms", similar to here which returns in hertz (Hz):


else if length > 0 && length > (speed / 1000) {

let resultFreq = Measurement(value: (Double(100*(speed / length))).rounded()/100, unit: UnitFrequency.hertz)


for some reason UnitDuration. only lets me use seconds, minutes, hours. I'm wondering how to do a class extension to include milliseconds.

I did this at the end of previous code


let result = Measurement(value: 1000*duration, unit: UnitDuration.milliseconds)
print(result)

gives

125420.0 ms

Yes that is what I tried originally, but it gives me this error:


Type 'UnitDuration' has no member 'milliseconds'


And it's the same error if I try and do the conversion from seconds.

UnitDuration.milliseconds is available only on iOS 13+.

class var milliseconds: UnitDuration


If your project is targeting earlier iOSs, you cannot use it.

Ah I see, that'd be why then.


Is there any way I can extend the class to include it?

According to the doc, you can define your own subclass of Dimension or Unit .

Or you might write your own implementation of `UnitDuration.milliseconds`.


But I do not have enough experience about it.


First try:

extension UnitDuration {
    static let msec: UnitDuration = {
        UnitDuration(symbol: "msec", converter: UnitConverterLinear(coefficient: 0.001))
    }()
}

let timePeriod = 123.45678
let timeMesure = Measurement(value: timePeriod * 1000, unit: UnitDuration.msec)
print(timeMesure) //-> 123456.78 msec

Thanks, i'll give this a try

The simplest maybe to test for IOS availability.

If iOS 13, use it

if not, just display it "manually"