SGLibCS.Ms 1.0.0

dotnet add package SGLibCS.Ms --version 1.0.0
NuGet\Install-Package SGLibCS.Ms -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SGLibCS.Ms" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SGLibCS.Ms --version 1.0.0
#r "nuget: SGLibCS.Ms, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SGLibCS.Ms as a Cake Addin
#addin nuget:?package=SGLibCS.Ms&version=1.0.0

// Install SGLibCS.Ms as a Cake Tool
#tool nuget:?package=SGLibCS.Ms&version=1.0.0

SGLibCS.Ms - Port of vercel/ms for .NET

Use this package to easily convert various time formats to milliseconds.

API is a little bit different than the original version, better suited for C#.

Examples

using SGLibCS.Ms;

MsConverter.Parse("2 days");  // 172800000
MsConverter.Parse("1d");      // 86400000
MsConverter.Parse("10h");     // 36000000
MsConverter.Parse("2.5 hrs"); // 9000000
MsConverter.Parse("2h");      // 7200000
MsConverter.Parse("1m");      // 60000
MsConverter.Parse("5s");      // 5000
MsConverter.Parse("1y");      // 31557600000
MsConverter.Parse("100");     // 100
MsConverter.Parse("-3 days"); // -259200000
MsConverter.Parse("-1h");     // -3600000
MsConverter.Parse("-200");    // -200

Parse will throw

  • Parse will throw ArgumentException if input is null, empty or exceeds 100 characters
  • Parse will throw FormatException if input format is invalid.

Not throwing version

double result;
bool success = MsConverter.TryParse("2 days", out result);
// result: 172800000; success: true

double result;
bool success = MsConverter.TryParse("", out result);
// result: 0; success: false

Parsing to TimeSpan

// Throwing
TimeSpan ts = MsConverter.ParseToTimeSpan("1h");

// Not throwing
TimeSpan ts;
bool success = MsConverter.TryParseToTimeSpan("1h", out ts);

Convert from Milliseconds

MsConverter.ToString(60000);       // 1m
MsConverter.ToString(2 * 60000);   // 2m
MsConverter.ToString(-3 * 60000);  // -3m
MsConverter.ToString(
    MsConverter.Parse("10 hours")) // 10h

Time Format Written-Out

MsConverter.Options opts = new () { Long = true };
MsConverter.ToString(60000, opts);      // 1 minute
MsConverter.ToString(2 * 60000, opts);  // 2 minutes
MsConverter.ToString(-3 * 60000, opts); // -3 minutes
MsConverter.ToString(
    MsCoverter.Parse("10h"), opts);     // 10 hours

Todo

  • Data annotation attribute for MsFormat validation so someone can easy verify in Dto or Options.

LICENSE

MIT

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 146 7/26/2023