Implemented DateFilter, added License
This commit is contained in:
13
LICENSE.txt
Normal file
13
LICENSE.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
Copyright 2014 Vistaprint
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace TimberWinR.Filters
|
||||
{
|
||||
@@ -10,11 +13,51 @@ namespace TimberWinR.Filters
|
||||
public string Field { get; private set; }
|
||||
public string Target { get; private set; }
|
||||
public bool ConvertToUTC { get; private set; }
|
||||
public List<string> Pattern { get; private set; }
|
||||
public List<string> Patterns { get; private set; }
|
||||
|
||||
public override void Apply(Newtonsoft.Json.Linq.JObject json)
|
||||
public DateFilter()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
Patterns = new List<string>();
|
||||
}
|
||||
|
||||
public override void Apply(JObject json)
|
||||
{
|
||||
JToken token = null;
|
||||
if (json.TryGetValue(Field, StringComparison.OrdinalIgnoreCase, out token))
|
||||
{
|
||||
string text = token.ToString();
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
DateTime ts;
|
||||
if (Patterns == null || Patterns.Count == 0)
|
||||
{
|
||||
if (DateTime.TryParse(text, out ts))
|
||||
{
|
||||
if (ConvertToUTC)
|
||||
ts = ts.ToUniversalTime();
|
||||
|
||||
if (json[Target] == null)
|
||||
json.Add(Target, ts);
|
||||
else
|
||||
json[Target] = ts;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (DateTime.TryParseExact(text, Patterns.ToArray(), CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None, out ts))
|
||||
{
|
||||
if (ConvertToUTC)
|
||||
ts = ts.ToUniversalTime();
|
||||
|
||||
if (json[Target] == null)
|
||||
json.Add(Target, ts);
|
||||
else
|
||||
json[Target] = ts;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,24 +60,24 @@ namespace TimberWinR.Filters
|
||||
foreach (string fieldName in namedCaptures.Keys)
|
||||
{
|
||||
|
||||
if (fieldName == "timestamp")
|
||||
{
|
||||
string value = namedCaptures[fieldName];
|
||||
DateTime ts;
|
||||
if (DateTime.TryParse(value, out ts))
|
||||
json.Add(fieldName, ts.ToUniversalTime());
|
||||
else if (DateTime.TryParseExact(value, new string[]
|
||||
{
|
||||
"MMM dd hh:mm:ss",
|
||||
"MMM dd HH:mm:ss",
|
||||
"MMM dd h:mm",
|
||||
"MMM dd hh:mm",
|
||||
}, CultureInfo.InvariantCulture, DateTimeStyles.None, out ts))
|
||||
json.Add(fieldName, ts.ToUniversalTime());
|
||||
else
|
||||
json.Add(fieldName, (JToken) namedCaptures[fieldName]);
|
||||
}
|
||||
else
|
||||
//if (fieldName == "timestamp")
|
||||
//{
|
||||
// string value = namedCaptures[fieldName];
|
||||
// DateTime ts;
|
||||
// if (DateTime.TryParse(value, out ts))
|
||||
// json.Add(fieldName, ts.ToUniversalTime());
|
||||
// else if (DateTime.TryParseExact(value, new string[]
|
||||
// {
|
||||
// "MMM dd hh:mm:ss",
|
||||
// "MMM dd HH:mm:ss",
|
||||
// "MMM dd h:mm",
|
||||
// "MMM dd hh:mm",
|
||||
// }, CultureInfo.InvariantCulture, DateTimeStyles.None, out ts))
|
||||
// json.Add(fieldName, ts.ToUniversalTime());
|
||||
// else
|
||||
// json.Add(fieldName, (JToken) namedCaptures[fieldName]);
|
||||
//}
|
||||
//else
|
||||
json.Add(fieldName, (JToken) namedCaptures[fieldName]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user