Skip to content

Commit 0a85dcd

Browse files
committed
Cleanup
1 parent 7ed8800 commit 0a85dcd

8 files changed

Lines changed: 59 additions & 22 deletions

File tree

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Output (example):
2222
2025-10-04T14:23:00
2323
```
2424

25-
You can also parse expressions relative to a specific `DateTime` using `NaturalDateTime.From`.
25+
You can parse expressions relative to a specific `DateTime` using `NaturalDateTime.From`.
2626

2727
```csharp
2828
using STYME;
@@ -39,9 +39,29 @@ Output:
3939
2020-02-01T00:00:00
4040
```
4141

42+
### Add
43+
44+
You can add time using the `add` keyword.
45+
46+
```csharp
47+
using STYME;
48+
49+
var baseTime = new DateTime(2020, 3, 1, 12, 0, 0);
50+
var parser = NaturalDateTime.From(baseTime);
51+
var result = parser.Parse("add 1 year");
52+
Console.WriteLine(result);
53+
```
54+
55+
Output:
56+
57+
```
58+
2021-03-01T00:00:00
59+
```
60+
61+
4262
### Deduct / Subtract
4363

44-
You can subtract time using the `deduct` (or `subtract`) keyword. The expression works the same as `add` but shifts the base time backwards.
64+
You can subtract time using the `deduct` (or `subtract`) keyword.
4565

4666
```csharp
4767
using STYME;
@@ -83,6 +103,13 @@ Console.WriteLine(parser.Parse("add 2 decades")); // 2020-01-01
83103
Console.WriteLine(parser.Parse("deduct 1 century")); // 1900-01-01
84104
```
85105

106+
### Todo
107+
108+
[ ] Add month support (set DateTime to specific month)
109+
[ ] Add day support (i.e. "next friday")
110+
[ ] Add complex time support (i.e. "quarter past five")
111+
[ ] Add multiple complex operations (i.e. "add one year then next friday")
112+
86113
## Support
87114

88115
* .NET 8.0 and later

src/STYME/Expressions/AddExpression.cs renamed to src/STYME/Expressions/Implementations/AddExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using STYME.Handling;
22
using STYME.Values.TimeShifts;
33

4-
namespace STYME.Expressions;
4+
namespace STYME.Expressions.Implementations;
55

66
internal sealed class AddExpression : IExpression
77
{

src/STYME/Expressions/DeductExpression.cs renamed to src/STYME/Expressions/Implementations/DeductExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using STYME.Handling;
22
using STYME.Values.TimeShifts;
33

4-
namespace STYME.Expressions;
4+
namespace STYME.Expressions.Implementations;
55

66
internal sealed class DeductExpression : IExpression
77
{

src/STYME/Expressions/TimeShiftExpression.cs renamed to src/STYME/Expressions/Implementations/TimeShiftExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using STYME.Handling;
22
using STYME.Values.TimeShifts;
33

4-
namespace STYME.Expressions;
4+
namespace STYME.Expressions.Implementations;
55

66
internal sealed class TimeShiftExpression : IExpression
77
{

src/STYME/Locale/English/EnglishParserRules.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ internal sealed class EnglishParserRules : ParserRules
1111
public EnglishParserRules()
1212
{
1313
_timeUnitMap = new(StringComparer.OrdinalIgnoreCase);
14-
void AddTimeUnitAliases(Func<double, TimeSpan> converter, params string[] names)
15-
{
16-
foreach (var name in names)
17-
{
18-
if (string.IsNullOrWhiteSpace(name)) continue;
19-
_timeUnitMap[name.Trim()] = converter;
20-
}
21-
}
22-
2314
AddTimeUnitAliases(TimeSpan.FromSeconds, "second", "seconds");
2415
AddTimeUnitAliases(TimeSpan.FromMinutes, "minute", "minutes");
2516
AddTimeUnitAliases(TimeSpan.FromHours, "hour", "hours");
@@ -32,17 +23,33 @@ void AddTimeUnitAliases(Func<double, TimeSpan> converter, params string[] names)
3223
AddTimeUnitAliases(v => TimeSpan.FromDays(v * 365 * 1000), "millennium", "millennia", "millenium");
3324

3425
_constructors = new(StringComparer.OrdinalIgnoreCase);
35-
void AddConstructorAliases(IExpressionConstructor constructor, params string[] names)
26+
AddConstructorAliases(new AddExpressionConstructor(), "add");
27+
AddConstructorAliases(new DeductExpressionConstructor(), "deduct", "subtract");
28+
}
29+
30+
private void AddConstructorAliases(IExpressionConstructor constructor, params string[] names)
31+
{
32+
foreach (var name in names)
3633
{
37-
foreach (var name in names)
34+
if (string.IsNullOrWhiteSpace(name))
3835
{
39-
if (string.IsNullOrWhiteSpace(name)) continue;
40-
_constructors[name.Trim()] = constructor;
36+
continue;
4137
}
38+
39+
_constructors[name.Trim()] = constructor;
4240
}
41+
}
42+
private void AddTimeUnitAliases(Func<double, TimeSpan> converter, params string[] names)
43+
{
44+
foreach (var name in names)
45+
{
46+
if (string.IsNullOrWhiteSpace(name))
47+
{
48+
continue;
49+
}
4350

44-
AddConstructorAliases(new AddExpressionConstructor(), "add");
45-
AddConstructorAliases(new DeductExpressionConstructor(), "deduct", "subtract");
51+
_timeUnitMap[name.Trim()] = converter;
52+
}
4653
}
4754

4855
public bool TryCreateTimeSpan(double amount, string unit, out TimeSpan value)

src/STYME/Parser/ExpressionParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using STYME.Expressions;
2+
using STYME.Expressions.Implementations;
23
using STYME.Locale.English;
34
using STYME.Values.TimeShifts;
45

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using STYME.Expressions;
2+
using STYME.Expressions.Implementations;
23

34
namespace STYME.Parser.Expressions.Constructors;
45

56
internal sealed class AddExpressionConstructor : IExpressionConstructor
67
{
78
public IExpression Construct(string token, Queue<string> tokens, IExpressionParser parser)
89
{
9-
var right = parser.ParseExpressionTree(tokens) ?? throw new InvalidOperationException("Expected expression after keyword.");
10+
var right = parser.ParseExpressionTree(tokens) ?? throw new InvalidOperationException($"Expected expression after keyword '{token}'.");
1011
return new AddExpression(right);
1112
}
1213
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using STYME.Expressions;
2+
using STYME.Expressions.Implementations;
23

34
namespace STYME.Parser.Expressions.Constructors;
45

56
internal sealed class DeductExpressionConstructor : IExpressionConstructor
67
{
78
public IExpression Construct(string token, Queue<string> tokens, IExpressionParser parser)
89
{
9-
var right = parser.ParseExpressionTree(tokens) ?? throw new InvalidOperationException("Expected expression after keyword.");
10+
var right = parser.ParseExpressionTree(tokens) ?? throw new InvalidOperationException($"Expected expression after keyword '{token}'.");
1011
return new DeductExpression(right);
1112
}
1213
}

0 commit comments

Comments
 (0)