Skip to content

Commit

Permalink
1.2.12: Added extentsion methods for DateTime and string to allow eas…
Browse files Browse the repository at this point in the history
…y single line conversion between the two data types.
  • Loading branch information
pablothedolphin committed Jan 16, 2020
1 parent 966f394 commit 449fcd6
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Expand Down Expand Up @@ -30,5 +31,35 @@ public static void DestroyChildren (this Transform parent)
/// <param name="value">The current instance of the enum value.</param>
/// <returns>The enum value converted into a string.</returns>
public static string GetName<T> (this T value) where T : struct, IConvertible => Enum.GetName (typeof (T), value);

/// <summary>
/// Allows direct conversion of a <c>DateTime</c> value to a string that goes from years to seconds in decending order.
/// </summary>
/// <param name="dateTime">The value to convert.</param>
/// <returns>A new standard format string of the value.</returns>
public static string ToStandardFormat (this DateTime dateTime) => dateTime.ToString ("yyyy-MM-dd HH:mm:ss");

/// <summary>
/// Allows direct conversion of a <c>string</c> which happens to represent a time or date into a <c>DateTime</c> value.
/// </summary>
/// <param name="dateTimeString">The string to convert</param>
/// <returns>A new <c>DateTime</c> value derived from the string.</returns>
public static DateTime ToDateTime (this string dateTimeString)
{
if (DateTime.TryParseExact (dateTimeString, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime timeStampResult))
{
return timeStampResult;
}
else
{
DateTime tempDate = DateTime.Parse (dateTimeString);
string sYear = tempDate.ToString ("yyyy");
string sMonth = tempDate.Month.ToString ().PadLeft (2, '0');
string sDay = tempDate.Day.ToString ().PadLeft (2, '0');
string sTime = tempDate.TimeOfDay.ToString ();
string date = string.Format ("{0}-{1}-{2} {3}", sYear, sMonth, sDay, sTime);
return DateTime.ParseExact (date, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.open.scriptable-framework",
"displayName": "Scriptable Framework",
"author": "Jak Hussain, Dean Giddy, Conor Galvin",
"version": "1.2.11",
"version": "1.2.12",
"unity": "2018.4",
"description": "A modularity framework developed to better enable Unity developers to decouple and test their code using ScriptableObject based dependencies that can be dragged and dropped into the inspector. The API and workflow makes it easy to use advanced programming patterns and SOLID principals in your projects which promotes a higher quality of code. See GitHub for documentation.",
"keywords": [
Expand Down
90 changes: 90 additions & 0 deletions docs/api/ScriptableFramework.Extensions.html
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,96 @@ <h5 class="typeParameters">Type Parameters</h5>
</tr>
</tbody>
</table>


<a id="ScriptableFramework_Extensions_ToDateTime_" data-uid="ScriptableFramework.Extensions.ToDateTime*"></a>
<h4 id="ScriptableFramework_Extensions_ToDateTime_System_String_" data-uid="ScriptableFramework.Extensions.ToDateTime(System.String)">ToDateTime(String)</h4>
<div class="markdown level1 summary"><p>Allows direct conversion of a <code>string</code> which happens to represent a time or date into a <code>DateTime</code> value.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="decalaration">Declaration</h5>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static DateTime ToDateTime(this string dateTimeString)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Type</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="xref">System.String</span></td>
<td><span class="parametername">dateTimeString</span></td>
<td><p>The string to convert</p>
</td>
</tr>
</tbody>
</table>
<h5 class="returns">Returns</h5>
<table class="table table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="xref">DateTime</span></td>
<td><p>A new <code>DateTime</code> value derived from the string.</p>
</td>
</tr>
</tbody>
</table>


<a id="ScriptableFramework_Extensions_ToStandardFormat_" data-uid="ScriptableFramework.Extensions.ToStandardFormat*"></a>
<h4 id="ScriptableFramework_Extensions_ToStandardFormat_DateTime_" data-uid="ScriptableFramework.Extensions.ToStandardFormat(DateTime)">ToStandardFormat(DateTime)</h4>
<div class="markdown level1 summary"><p>Allows direct conversion of a <code>DateTime</code> value to a string that goes from years to seconds in decending order.</p>
</div>
<div class="markdown level1 conceptual"></div>
<h5 class="decalaration">Declaration</h5>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public static string ToStandardFormat(this DateTime dateTime)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Type</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="xref">DateTime</span></td>
<td><span class="parametername">dateTime</span></td>
<td><p>The value to convert.</p>
</td>
</tr>
</tbody>
</table>
<h5 class="returns">Returns</h5>
<table class="table table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="xref">System.String</span></td>
<td><p>A new standard format string of the value.</p>
</td>
</tr>
</tbody>
</table>
</article>
</div>

Expand Down
Loading

0 comments on commit 449fcd6

Please sign in to comment.