Skip to content

Commit

Permalink
docs: upsate documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicegginton committed Mar 18, 2023
1 parent 07cf463 commit 34318d7
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 48 deletions.
209 changes: 168 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![](./assets/cover.svg)
# Nanoseconds

Simple high resolution time stamps for swift developers, along with helpful tools to work with them.

Expand All @@ -20,55 +20,182 @@ Install via the [**Swift Package Manger**](https://swift.org/package-manager/) b

Remember to add **Nanoseconds** to your target as a dependency.

## Documentation

#### Creating Timestamps
Creating high resolution timestamps with nanosecond accuracy is easy, simply create a instant of `Now` struct

``` swift
let foo = Now()
```

#### Creating Time Intervals

Use the built-in operators overloads to easily calculate and initialize [**TimeInterval**](https://developer.apple.com/documentation/foundation/timeinterval):
## Getting Started

``` swift
import Foundation
import Nanoseconds

let start = Now()
sleep(1)
let end = Now()
let duration = end - start
print(duration) //=> 1004222113.0
```

#### Convert Time Intervals from Nanoseconds to Other Time Units

To convert [**TimeInterval**](https://developer.apple.com/documentation/foundation/timeinterval) objects from nanosecond to a different unit simply use a provided extension:

- nanoseconds
- microseconds
- milliseconds
- seconds
- minutes
- hours

``` swift
print(duration.milliseconds) //=> 1004.22211
print(duration.seconds) //=> 1.004222113
```
#### Create TimeIntervals Objects from any time unit

Use a provided init method to create a [**TimeInterval**](https://developer.apple.com/documentation/foundation/timeinterval) object from any time unit
## Documentation

``` swift
let duration = TimeInterval(seconds: 8)
#### Now

```swift
/**
A type that represents a point in time with nanosecond precision.
*/
public struct Now {
/**
Creates a new `Now` instance.
*/
public init()

/**
Compares two `Now` instances.
- Parameters:
- lhs: The first `Now` instance.
- rhs: The second `Now` instance.
- Returns: `true` if the first `Now` instance is less than the second `Now` instance.
*/
public static func < (lhs: Now, rhs: Now) -> Bool

/**
Compares two `Now` instances.
- Parameters:
- lhs: The first `Now` instance.
- rhs: The second `Now` instance.
- Returns: `true` if the first `Now` instance is less than or equal to the second `Now` instance.
*/
public static func <= (lhs: Now, rhs: Now) -> Bool

/**
Compares two `Now` instances.
- Parameters:
- lhs: The first `Now` instance.
- rhs: The second `Now` instance.
- Returns: `true` if the first `Now` instance is greater than the second `Now` instance.
*/
public static func > (lhs: Now, rhs: Now) -> Bool

/**
Compares two `Now` instances.
- Parameters:
- lhs: The first `Now` instance.
- rhs: The second `Now` instance.
- Returns: `true` if the first `Now` instance is greater than or equal to the second `Now` instance.
*/
public static func >= (lhs: Now, rhs: Now) -> Bool

/**
Compares two `Now` instances.
- Parameters:
- lhs: The first `Now` instance.
- rhs: The second `Now` instance.
- Returns: `true` if the first `Now` instance is equal to the second `Now` instance.
*/
public static func == (lhs: Now, rhs: Now) -> Bool

/**
Compares two `Now` instances.
- Parameters:
- lhs: The first `Now` instance.
- rhs: The second `Now` instance.
- Returns: `true` if the first `Now` instance is not equal to the second `Now` instance.
*/
public static func != (lhs: Now, rhs: Now) -> Bool

/**
Subtract a `Now` instance from another `Now` instance.
- Parameters:
- lhs: The first `Now` instance.
- rhs: The second `Now` instance.
- Returns: The difference between the two `Now` instances.
*/
public static func - (lhs: Now, rhs: Now) -> TimeInterval

/**
Add a `Now` instance to another `Now` instance.
- Parameters:
- lhs: The first `Now` instance.
- rhs: The second `Now` instance.
- Returns: The sum of the two `Now` instances.
*/
public static func + (lhs: Now, rhs: Now) -> TimeInterval
}
```

#### Covert TimeIntervals Objects to human readable strings

Use the provided `timeString` variable to convert [**TimeInterval**](https://developer.apple.com/documentation/foundation/timeinterval) objects into a human readable format:

``` swift
let duration = TimeInterval(hours: 1) + TimeInterval(seconds: 120)
print(duration.timeString) //=> 1h 1m 30s
```
#### TimeInterval

```swift
extension TimeInterval {
/**
Creates a new `TimeInterval` from the given number of nanoseconds.
- Parameter nanoseconds: The number of nanoseconds.
*/
public init (nanoseconds: Double)

/**
Creates a new `TimeInterval` from the given number of microseconds.
- Parameter microseconds: The number of microseconds.
*/
public init (microseconds: Double)

/**
Creates a new `TimeInterval` from the given number of milliseconds.
- Parameter milliseconds: The number of milliseconds.
*/
public init (milliseconds: Double)

/**
Creates a new `TimeInterval` from the given number of seconds.
- Parameter seconds: The number of seconds.
*/
public init (seconds: Double)

/**
Creates a new `TimeInterval` from the given number of minutes.
- Parameter minutes: The number of minutes.
*/
public init (minutes: Double)

/**
Creates a new `TimeInterval` from the given number of hours.
- Parameter hours: The number of hours.
*/
public init (hours: Double)

/**
The number of nanoseconds in the `TimeInterval`.
*/
public var nanoseconds: Double

/**
The number of microseconds in the `TimeInterval`.
*/
public var microseconds: Double

/**
The number of milliseconds in the `TimeInterval`.
*/
public var milliseconds: Double

/**
The number of seconds in the `TimeInterval`.
*/
public var seconds: Double

/**
The number of minutes in the `TimeInterval`.
*/
public var minutes: Double

/**
The number of hours in the `TimeInterval`.
*/
public var hours: Double

/**
Covert the `TimeInterval` to a string.
The string will be in the format `[h]h [m]m [s]s`.
Only the largest unit of time will be displayed.
*/
public var timeString: String
}
```
7 changes: 0 additions & 7 deletions assets/cover.svg

This file was deleted.

0 comments on commit 34318d7

Please sign in to comment.