Skip to content
Merged
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
51 changes: 0 additions & 51 deletions Startwatch.Library/ExtensionMethods.cs

This file was deleted.

65 changes: 65 additions & 0 deletions Startwatch.Library/Library.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace Startwatch.Library

open System
open System.Diagnostics

module Logic =
let format (timespan: TimeSpan) =
match timespan with
| t when t.Ticks < 0 ->
raise <| NotSupportedException("Negative TimeSpans are currently not supported.")
| t when t.Ticks = 0 ->
"no time"
| t when t.TotalMilliseconds < 0.01 ->
sprintf "%s (%s)"
(t.TotalMilliseconds.ToString("#,##0.#####ms"))
(t.TotalNanoseconds.ToString("#,##0ns"))
| t when t.TotalMilliseconds < 1 ->
sprintf "%s" (t.TotalMilliseconds.ToString("#,##0.#####ms"))
| t when t.TotalMilliseconds < 1000 ->
sprintf "%s" (t.TotalMilliseconds.ToString("#,##0ms"))
| t ->
let days = t.Days
let hours = t.Hours
let mins = t.Minutes
let secs = t.Seconds

let prependText = if secs = 0 then "exactly " else String.Empty

let hourText =
match (days, hours) with
| d, _ when d > 0 -> sprintf "%s" ((hours + (days * 24)).ToString("#,##0h"))
| _, h when h > 0 -> sprintf "%s" ((hours + (days * 24)).ToString("#,##0h"))
| _ -> String.Empty

let minText =
match (hours, mins) with
| h, m when h > 0 && m > 0 -> sprintf "%s" (m.ToString("00m"))
| _, m when m > 0 -> sprintf "%s" (m.ToString("0m"))
| _ -> String.Empty

let secText =
match (days, hours, mins, secs) with
| d, _, 0, s when d > 0 && s > 0 -> sprintf "%ss" (t.ToString("ss"))
| _, 0, 0, s when s > 0 -> sprintf "%ss" (t.ToString("s\\.ff"))
| _, h, _, s when h > 0 && s > 0 -> sprintf "%ss" (t.ToString("ss"))
| _, _, m, s when m > 0 && s > 0 -> sprintf "%ss" (t.ToString("ss"))
| _, _, _, s when s > 0 -> sprintf "%s" (t.ToString("s"))
| _ -> String.Empty

$"{prependText}{hourText}{minText}{secText}"

open Logic

/// A simple wrapper for `System.Diagnostic.Stopwatch` class saves its start time
/// and provides friendly representations of the elapsed time.
type Watch() =
let mutable startedAt = Stopwatch.GetTimestamp()

/// Returns a formatted "friendly" version of the elapsed time.
member _.ElapsedFriendly =
format <| Stopwatch.GetElapsedTime(startedAt)

/// Sets the start time for this instance to the current time.
member _.Restart =
startedAt <- Stopwatch.GetTimestamp()
20 changes: 0 additions & 20 deletions Startwatch.Library/Startwatch.Library.csproj

This file was deleted.

30 changes: 30 additions & 0 deletions Startwatch.Library/Startwatch.Library.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>CodeConscious.Startwatch</PackageId>
<Version>1.0.0</Version>
<Authors>CodeConscious</Authors>
<Company>CodeConscious</Company>
<Description>A simple wrapper for System.Diagnostics.Stopwatch.</Description>
<PackageProjectUrl>https://github.com/codeconscious/startwatch</PackageProjectUrl>
<RepositoryUrl>https://github.com/codeconscious/startwatch</RepositoryUrl>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReleaseNotes>Rewrote in F# and changed the display of very short times.</PackageReleaseNotes>
<PackageTags>startwatch stopwatch timer fsharp codeconscious</PackageTags>
<RootNamespace>Startwatch.Library</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Compile Include="Library.fs"/>
</ItemGroup>

<ItemGroup>
<None Include="docs\readme.md" Pack="true" PackagePath="\" />
</ItemGroup>

</Project>
42 changes: 0 additions & 42 deletions Startwatch.Library/Watch.cs

This file was deleted.

6 changes: 3 additions & 3 deletions Startwatch.Library/docs/readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Startwatch

This is a very simple wrapper around `System.Diagnostics.Stopwatch`. Since its stopwatch starts automatically, its name is "Startwatch." ^_^
This is a very simple wrapper around the `System.Diagnostics.Stopwatch` class that just shows "friendly" versions of elapsed times via its `GetElapsedTime` property.

I largely created this to get some experience uploading a package to Nuget.org, but also to use in some of my personal projects.
Since it starts tracking time at class instantiation, its name is "Startwatch." ^_^

Note that the API of this library could change significantly at any moment.
I largely created this to get some experience uploading packages to Nuget.org, but I also use it in some of my personal projects.
4 changes: 4 additions & 0 deletions Startwatch.Tests/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Program

[<EntryPoint>]
let main _ = 0
24 changes: 0 additions & 24 deletions Startwatch.Tests/Startwatch.Tests.csproj

This file was deleted.

25 changes: 25 additions & 0 deletions Startwatch.Tests/Startwatch.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="Tests.fs"/>
<Compile Include="Program.fs"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageReference Include="xunit" Version="2.9.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Startwatch.Library\Startwatch.Library.fsproj" />
</ItemGroup>

</Project>
Loading
Loading