12 changed files with 167 additions and 58 deletions
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
/* |
||||
* Created by SharpDevelop. |
||||
* User: Peter Forstmeier |
||||
* Date: 06.02.2013 |
||||
* Time: 20:18 |
||||
* |
||||
* To change this template use Tools | Options | Coding | Edit Standard Headers. |
||||
*/ |
||||
using System; |
||||
using System.Text.RegularExpressions; |
||||
using SimpleExpressionEvaluator.Utilities; |
||||
|
||||
namespace SimpleExpressionEvaluator.Compilation.Functions |
||||
{ |
||||
/// <summary>
|
||||
/// DateSubtract (endDate,startDate)
|
||||
/// </summary>
|
||||
public class DateSubtract: Function<TimeSpan> |
||||
{ |
||||
private static readonly Regex VALIDATOR = new Regex("[dDwWmMyY]", RegexOptions.Compiled); |
||||
|
||||
protected override int ExpectedArgumentCount |
||||
{ |
||||
get |
||||
{ |
||||
return 2; |
||||
} |
||||
} |
||||
|
||||
private static void ValidatePeriodCode(string code) |
||||
{ |
||||
if (String.IsNullOrEmpty(code)) |
||||
throw new Exception( |
||||
"The second argument in DateAdd was not provided. It must be one of the following (case insensitive): d,w,m,y"); |
||||
|
||||
if (code.Length != 1 || !VALIDATOR.IsMatch(code)) |
||||
{ |
||||
if (String.IsNullOrEmpty(code)) |
||||
throw new Exception( |
||||
"The second argument in DateAdd was not provided. It must be one of the following (case insensitive): d,w,m,y"); |
||||
} |
||||
} |
||||
|
||||
protected override TimeSpan EvaluateFunction(object[] args) |
||||
{ |
||||
|
||||
args[0] = args[0] == null ? DateTime.Today : TypeNormalizer.EnsureType(args[0], typeof (DateTime)); |
||||
|
||||
args[1] = args[1] == null ? DateTime.Today : TypeNormalizer.EnsureType(args[1], typeof (DateTime)); |
||||
|
||||
var endDate = (DateTime) args[0]; |
||||
|
||||
var startDate = (DateTime) args[1]; |
||||
if (startDate == DateTime.MinValue) |
||||
startDate = DateTime.Today; |
||||
|
||||
|
||||
Console.WriteLine(endDate.Subtract(startDate)); |
||||
var s = endDate.Subtract(startDate); |
||||
return endDate.Subtract(startDate); |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
using ICSharpCode.Reports.Expressions.ReportingLanguage; |
||||
using NUnit.Framework; |
||||
using SimpleExpressionEvaluator; |
||||
|
||||
namespace ICSharpCode.Reports.Core.Test.ReportingLanguage.LanguageTests |
||||
{ |
||||
[TestFixture] |
||||
public class DateTimeFunctionsFixture |
||||
{ |
||||
|
||||
[Test] |
||||
public void Can_Compile_Function() |
||||
{ |
||||
const string expression = "today()"; |
||||
IExpression expr = CreateExpression(expression); |
||||
Assert.That(expr.Evaluate(null), Is.EqualTo(DateTime.Today)); |
||||
} |
||||
|
||||
|
||||
[Test] |
||||
public void Can_Compile_Expr_With_Generic_Return() |
||||
{ |
||||
const string expression = "Today()"; |
||||
IExpression expr = CreateExpression(expression); |
||||
Assert.That(expr.Evaluate(null), Is.EqualTo(DateTime.Today)); |
||||
} |
||||
|
||||
[Test] |
||||
public void Can_Compile_DateAdd() |
||||
{ |
||||
const string expression = "dateAdd(today(),d,1)"; |
||||
IExpression expr = CreateExpression(expression); |
||||
Assert.That(expr.Evaluate(null), Is.EqualTo(DateTime.Today.AddDays(1))); |
||||
} |
||||
|
||||
[Test] |
||||
public void Can_Compile_DateDiff () |
||||
{ |
||||
const string expression = "dateSubtract('10.02.2013','09.02.2013')"; |
||||
IExpression expr = CreateExpression(expression); |
||||
Assert.That(expr.Evaluate(null), Is.EqualTo(new TimeSpan(1,0,0,0).Duration())); |
||||
} |
||||
|
||||
|
||||
[Test] |
||||
public void Can_Compile_DateDiff_Start_Greater_End () |
||||
{ |
||||
const string expression = "dateSubtract('09.02.2013','10.02.2013')"; |
||||
IExpression expr = CreateExpression(expression); |
||||
Assert.That(expr.Evaluate(null), Is.EqualTo(new TimeSpan(1,0,0,0))); |
||||
} |
||||
|
||||
[Test] |
||||
public void Can_Compile_DateDiff_Include_Hours_Minutes() |
||||
{ |
||||
|
||||
// Example found at:
|
||||
// http://msdn.microsoft.com/en-us/library/ae6246z1.aspx
|
||||
|
||||
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0); |
||||
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0); |
||||
// diff1 gets 185 days, 14 hours, and 47 minutes.
|
||||
System.TimeSpan diff1 = date2.Subtract(date1); |
||||
|
||||
const string expression = "dateSubtract('1996.12.6 13:2:0','1996.6.3 22:15:0')"; |
||||
|
||||
IExpression expr = CreateExpression(expression); |
||||
Assert.That(expr.Evaluate(null), Is.EqualTo(diff1)); |
||||
} |
||||
|
||||
|
||||
private static IExpression CreateExpression (string expression) |
||||
{ |
||||
var compiler = new ReportingLanguageCompiler(); |
||||
return compiler.CompileExpression<object>(expression); |
||||
} |
||||
} |
||||
} |
@ -1,43 +0,0 @@
@@ -1,43 +0,0 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
using ICSharpCode.Reports.Expressions.ReportingLanguage; |
||||
using NUnit.Framework; |
||||
using SimpleExpressionEvaluator; |
||||
|
||||
namespace ICSharpCode.Reports.Core.Test.ReportingLanguage.LanguageTests |
||||
{ |
||||
[TestFixture] |
||||
public class FunctionsFixture |
||||
{ |
||||
|
||||
[Test] |
||||
public void Can_Compile_Function() |
||||
{ |
||||
const string expression = "today()"; |
||||
var compiler = new ReportingLanguageCompiler(); |
||||
IExpression expr = compiler.CompileExpression<DateTime>(expression); |
||||
Assert.That(expr.Evaluate(null), Is.EqualTo(DateTime.Today)); |
||||
} |
||||
|
||||
|
||||
[Test] |
||||
public void Can_Compile_Expr_With_Generic_Return() |
||||
{ |
||||
const string expression = "Today()"; |
||||
var compiler = new ReportingLanguageCompiler(); |
||||
IExpression expr = compiler.CompileExpression<object>(expression); |
||||
Assert.That(expr.Evaluate(null), Is.EqualTo(DateTime.Today)); |
||||
} |
||||
|
||||
[Test] |
||||
public void Can_Compile_DateAdd() |
||||
{ |
||||
const string expression = "dateAdd(today(),d,1)"; |
||||
var compiler = new ReportingLanguageCompiler(); |
||||
IExpression expr = compiler.CompileExpression<object>(expression); |
||||
Assert.That(expr.Evaluate(null), Is.EqualTo(DateTime.Today.AddDays(1))); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue