Skip to content

Commit

Permalink
Add UTC DateTime Token Transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
flipbit committed May 4, 2019
1 parent 8f30147 commit 71fc9e4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Tokenizer.Tests/Transformers/ToDateTimeTransformerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public void SetUp()
[Test]
public void TestParseDate()
{
var result = @operator.Transform("2014-01-01", "yyyy-MM-dd");
var result = (DateTime) @operator.Transform("2014-01-01", "yyyy-MM-dd");

Assert.AreEqual(new DateTime(2014, 1, 1), result);
Assert.AreEqual(DateTimeKind.Unspecified, result.Kind);
}

[Test]
Expand Down
26 changes: 26 additions & 0 deletions Tokenizer.Tests/Transformers/ToDateTimeUtcTransformerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using NUnit.Framework;

namespace Tokens.Transformers
{
[TestFixture]
public class ToDateTimeUtcTransformerTest
{
private ToDateTimeUtcTransformer @operator;

[SetUp]
public void SetUp()
{
@operator = new ToDateTimeUtcTransformer();
}

[Test]
public void TestParseDateSetsKindToUtc()
{
var result = (DateTime) @operator.Transform("2014-01-01", "yyyy-MM-dd");

Assert.AreEqual(new DateTime(2014, 1, 1), result);
Assert.AreEqual(DateTimeKind.Utc, result.Kind);
}
}
}
1 change: 1 addition & 0 deletions Tokenizer/Parsers/TokenParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public TokenParser(TokenizerOptions options)

// Add default transformers/validators
RegisterTransformer<ToDateTimeTransformer>();
RegisterTransformer<ToDateTimeUtcTransformer>();
RegisterTransformer<ToLowerTransformer>();
RegisterTransformer<ToUpperTransformer>();
RegisterTransformer<TrimTransformer>();
Expand Down
2 changes: 1 addition & 1 deletion Tokenizer/Tokenizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0.5.0</Version>
<Version>2.0.6.0</Version>
<Authors>Chris Wood</Authors>
<Company>flipbit.co.uk</Company>
<PackageProjectUrl>http://github.com/flipbit/tokenizer</PackageProjectUrl>
Expand Down
19 changes: 12 additions & 7 deletions Tokenizer/Transformers/ToDateTimeTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,33 @@ public object Transform(object value, params string[] args)
{
if (value == null) return string.Empty;

DateTime result;
return ToDateTime(value, args, out var result) ? result : value;
}

if (args.Length == 0 || string.IsNullOrEmpty(args[0]))
public static bool ToDateTime(object value, string[] formats, out DateTime result)
{
if (formats == null || formats.Length == 0 || string.IsNullOrEmpty(formats[0]))
{
if (DateTime.TryParse(value.ToString(), out result))
{
return result;
return true;
}
}
else
{

foreach (var arg in args)
foreach (var format in formats)
{
if (DateTime.TryParseExact(value.ToString(), arg, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
if (DateTime.TryParseExact(value.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
return result;
return true;
}
}
}

return value;
result = default(DateTime);

return false;
}
}
}
22 changes: 22 additions & 0 deletions Tokenizer/Transformers/ToDateTimeUtcTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace Tokens.Transformers
{
/// <summary>
/// Converts the token value to a <see cref="DateTime"/> in UTC format
/// </summary>
public class ToDateTimeUtcTransformer : ITokenTransformer
{
public object Transform(object value, params string[] args)
{
if (value == null) return string.Empty;

if (ToDateTimeTransformer.ToDateTime(value, args, out var result))
{
value = DateTime.SpecifyKind(result, DateTimeKind.Utc);
}

return value;
}
}
}

0 comments on commit 71fc9e4

Please sign in to comment.