Skip to content

Commit dccd781

Browse files
committed
#34 - DateString parser.
1 parent 6fb0fc4 commit dccd781

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ https://github.com/Apress/beg-sql-server-reporting-services/blob/master/Beginnin
284284
| Date & Time | DateDiff | Complete | |
285285
| Date & Time | DatePart | Partial | Not yet tested with all time intervals |
286286
| Date & Time | DateSerial | Complete | |
287-
| Date & Time | DateString | Not started | |
287+
| Date & Time | DateString | Complete | |
288288
| Date & Time | DateValue | Complete | |
289289
| Date & Time | Day | Complete | |
290290
| Date & Time | FormatDateTime | Complete | |
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using ReportViewer.NET.DataObjects;
2+
using ReportViewer.NET.Extensions;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Text.RegularExpressions;
8+
using System.Threading.Tasks;
9+
10+
namespace ReportViewer.NET.Parsers.DateAndTime
11+
{
12+
public class DateStringParser : BaseParser
13+
{
14+
public static Regex DateStringRegex = RegexCommon.GenerateParserRegex("DateString");
15+
16+
public DateStringParser(
17+
string currentString,
18+
ExpressionFieldOperator op,
19+
ReportExpression currentExpression,
20+
IEnumerable<IDictionary<string, object>> dataSetResults,
21+
IDictionary<string, object> values,
22+
int currentRowNumber,
23+
IEnumerable<DataSet> dataSets,
24+
DataSet activeDataset,
25+
ReportRDL report
26+
) : base(currentString, op, currentExpression, dataSetResults, values, currentRowNumber, dataSets, activeDataset, DateStringRegex, report)
27+
{
28+
}
29+
30+
public override (Type, object) ExtractExpressionValue(string fieldName, string dataSetName)
31+
{
32+
throw new NotImplementedException();
33+
}
34+
35+
public override void Parse()
36+
{
37+
var match = DateStringRegex.Match(CurrentString);
38+
39+
CurrentExpression.Index = match.Index;
40+
CurrentExpression.ResolvedType = typeof(DateTime);
41+
CurrentExpression.Value = DateTime.Now;
42+
}
43+
}
44+
}

src/ReportViewer.NET/Parsers/ExpressionFieldOperator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public enum ExpressionFieldOperator
5757
DateDiff,
5858
DatePart,
5959
DateSerial,
60+
DateString,
6061
Day,
6162
Now,
6263
DateValue,

src/ReportViewer.NET/Parsers/ExpressionParser.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,15 @@ ref string proposedString
524524
proposedString = dateSerialParser.GetProposedString();
525525
}
526526

527+
if (DateStringParser.DateStringRegex.IsMatch(currentString) &&
528+
(currentExpression.Operator == ExpressionFieldOperator.None || DateStringParser.DateStringRegex.Match(currentString).Index < currentExpression.Index)
529+
)
530+
{
531+
var dateStringParser = new DateStringParser(currentString, ExpressionFieldOperator.DateString, currentExpression, dataSetResults, values, currentRowNumber, dataSets, activeDataset, _report);
532+
dateStringParser.Parse();
533+
proposedString = dateStringParser.GetProposedString();
534+
}
535+
527536
if (DayParser.DayRegex.IsMatch(currentString) &&
528537
((WeekdayParser.WeekdayRegex.IsMatch(currentString) && DayParser.DayRegex.Match(currentString).Index < WeekdayParser.WeekdayRegex.Match(currentString).Index) || !WeekdayParser.WeekdayRegex.IsMatch(currentString)) &&
529538
(currentExpression.Operator == ExpressionFieldOperator.None || DayParser.DayRegex.Match(currentString).Index < currentExpression.Index)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using ReportViewer.NET.DataObjects;
3+
using ReportViewer.NET.Extensions;
4+
using ReportViewer.NET.Parsers;
5+
using System.Globalization;
6+
7+
namespace ReportViewer.NET.Tests.Parsers.DateAndTime
8+
{
9+
[TestClass]
10+
public class DateStringParserTests
11+
{
12+
private ExpressionParser _expressionParser;
13+
private ReportRDL _report;
14+
15+
public DateStringParserTests()
16+
{
17+
var report = TestHelper.PrimeReport();
18+
19+
_report = report;
20+
_expressionParser = new ExpressionParser(report);
21+
}
22+
23+
[TestMethod]
24+
public void DateString_Return_String()
25+
{
26+
// Arrange
27+
var expr = "=FormatDateTime(DateString(), DateFormat.ShortDate)";
28+
29+
// Act
30+
var cultureInfo = new CultureInfo("en-GB");
31+
CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
32+
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
33+
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
34+
35+
var now = DateTime.Now;
36+
var result = _expressionParser.ParseReportExpressionString(
37+
expr,
38+
null,
39+
null,
40+
1,
41+
_report.DataSets,
42+
null,
43+
null
44+
).ExpressionAsString();
45+
46+
// Assert
47+
Assert.AreEqual(now.ToShortDateString(), result);
48+
49+
CultureInfo.DefaultThreadCurrentCulture = originalCulture;
50+
CultureInfo.DefaultThreadCurrentUICulture = originalCulture;
51+
}
52+
53+
[TestMethod]
54+
public void DateString_Return_String_Nested_DatePart()
55+
{
56+
// Arrange
57+
var now = DateTime.Now;
58+
var expr = "=MonthName(DatePart(\"m\", DateString()))";
59+
60+
// Act
61+
var result = _expressionParser.ParseReportExpressionString(
62+
expr,
63+
null,
64+
null,
65+
1,
66+
_report.DataSets,
67+
null,
68+
null
69+
).ExpressionAsString();
70+
71+
// Assert
72+
Assert.AreEqual(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(now.Month), result);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)