Skip to content

Commit 4c1aa3f

Browse files
committed
Added DateTime input and unix convertion
1 parent 864d057 commit 4c1aa3f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

CommonsLibrary/CommonsLibrary.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<ImplicitUsings>disable</ImplicitUsings>
66
<Title>C# Commons Library</Title>
7-
<Version>7.0.0</Version>
7+
<Version>7.1.0</Version>
88
<Authors>Alex O'Brien</Authors>
99
<PackageProjectUrl>https://github.com/alex8obrien/CommonsLibrary</PackageProjectUrl>
1010
<PackageReadmeFile>README.md</PackageReadmeFile>

CommonsLibrary/StdInp.cs

+35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23

34
namespace CommonsLibrary
45
{
@@ -79,5 +80,39 @@ public static string[] InputDelimitedArray(string msg, char delimiter)
7980
string[] array = input.Split(delimiter);
8081
return array;
8182
}
83+
84+
/// <summary>Gets formatted DateTime from the Console</summary>
85+
/// <param name="msg">The message displayed to the user</param>
86+
/// <param name="pattern">The DateTime pattern shown to the user</param>
87+
/// <param name="culture">The culture-specific information for the DateTime</param>
88+
/// <returns>A DateTime object representing the string inputted DateTime</returns>
89+
public static DateTime GetDateTime(string msg, string pattern, CultureInfo culture)
90+
{
91+
bool valid;
92+
DateTime result;
93+
94+
do
95+
{
96+
string input = Input($"{msg} ({pattern})");
97+
valid = DateTime.TryParseExact(input, pattern, culture, DateTimeStyles.AllowWhiteSpaces, out result);
98+
99+
if (!valid)
100+
Console.WriteLine("Invalid date/time format.");
101+
102+
} while (!valid);
103+
104+
return result;
105+
}
106+
107+
/// <summary>Gets user inputted DateTime and converts it to a unix timestamp</summary>
108+
/// <param name="msg">The message displayed to the user</param>
109+
/// <param name="pattern">The DateTime pattern shown to the user</param>
110+
/// <param name="culture">The culture-specific information for the DateTime</param>
111+
/// <returns>A unix timestamp representing the inputted DateTime</returns>
112+
public static long GetUnixDateTime(string msg, string pattern, CultureInfo culture)
113+
{
114+
DateTime dt = GetDateTime(msg, pattern, culture);
115+
return ((DateTimeOffset)dt).ToUnixTimeSeconds();
116+
}
82117
}
83118
}

0 commit comments

Comments
 (0)