Skip to content

Commit ae4c973

Browse files
authored
Update DateTimeHelper (#117)
* Update DateTimeHelper * fix tests * .
1 parent 5e3da0a commit ae4c973

5 files changed

Lines changed: 234 additions & 265 deletions

File tree

examples/ConsoleApp/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static void Main(string[] args)
100100
"{{StartsWith \"abc\" \"!def\"}}",
101101
"{{Append \"abc\" \"!def\"}}",
102102
"{{Capitalize \"abc def\"}}",
103-
"{{Ellipsis \"abcfskdagdghsjfjd\" 5}}",
103+
"{{Ellipsis \"a very long text\" 5}}",
104104
"{{Reverse \"abc def\"}}",
105105
"{{Truncate \"abc def\" 166}}",
106106
"{{Camelcase \"abc def\"}}",
@@ -115,8 +115,10 @@ static void Main(string[] args)
115115
"{{UtcNow}}",
116116
"{{Now \"yyyy-MM-dd\"}}",
117117
"{{Format (Now) \"yyyy-MM-dd\"}}",
118-
//"{{Xeger.Generate \"[1-9]{1}\\d{3}\"}}",
119-
//"{{Xeger.Generate '{[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]}'}}",
118+
"{{DateTime.Format (Now) \"yyyy-MM-dd\"}}",
119+
"{{DateTime.Format \"2020-04-15T11:12:13\" \"yyyy-MM-dd\"}}",
120+
"{{Xeger.Generate \"[1-9]{1}\\d{3}\"}}",
121+
"{{Xeger.Generate '{[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]}'}}",
120122
"{{Random Type=\"Integer\" Min=1000 Max=9999}}",
121123
"{{GetEnvironmentVariable \"x\"}}",
122124
"{{GetEnvironmentVariable \"x\" \"User\"}}",

src/Handlebars.Net.Helpers/Helpers/DateTimeHelpers.cs

Lines changed: 36 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@
66

77
namespace HandlebarsDotNet.Helpers.Helpers;
88

9-
internal class DateTimeHelpers : BaseHelpers, IHelpers
9+
internal class DateTimeHelpers(IHandlebars context, IDateTimeService dateTimeService) : StringHelpers(context)
1010
{
11-
private readonly IDateTimeService _dateTimeService;
12-
13-
public DateTimeHelpers(IHandlebars context, IDateTimeService dateTimeService) : base(context)
14-
{
15-
_dateTimeService = Guard.NotNull(dateTimeService);
16-
}
11+
private readonly IDateTimeService _dateTimeService = Guard.NotNull(dateTimeService);
1712

1813
[HandlebarsWriter(WriterType.Value)]
1914
public object Now(string? format = null)
@@ -29,91 +24,61 @@ public object UtcNow(string? format = null)
2924
return format is null ? utc : utc.ToString(format, Context.Configuration.FormatProvider);
3025
}
3126

32-
[HandlebarsWriter(WriterType.String)]
33-
public string Format(object value, string format)
27+
[HandlebarsWriter(WriterType.String, Name = "DateTime.Format")]
28+
public override string Format(object? value, string format)
3429
{
35-
string FormatToString(DateTime dateTime)
36-
{
37-
return dateTime.ToString(format, Context.Configuration.FormatProvider);
38-
}
39-
4030
return value switch
4131
{
42-
DateTime valueAsDateTime => FormatToString(valueAsDateTime),
43-
string valueAsString when DateTime.TryParse(valueAsString, out var parsedAsDateTime) => FormatToString(parsedAsDateTime),
32+
DateTime valueAsDateTime => base.Format(valueAsDateTime, format),
33+
string valueAsString when DateTime.TryParse(valueAsString, out var parsedAsDateTime) => base.Format(parsedAsDateTime, format),
4434
_ => string.Empty
4535
};
4636
}
4737

48-
[HandlebarsWriter(WriterType.Value)]
49-
public bool Compare(object? value1, string operation, object? value2, string? format = null)
38+
[HandlebarsWriter(WriterType.Value, Name = "DateTime.Parse")]
39+
public DateTime Parse(string value)
5040
{
51-
Guard.NotNullOrEmpty(operation);
52-
53-
var dateTime1 = GetDatetime(value1, format);
54-
var dateTime2 = GetDatetime(value2, format);
41+
return DateTime.Parse(value, Context.Configuration.FormatProvider);
42+
}
5543

56-
switch(operation)
57-
{
58-
case ">": return dateTime1 > dateTime2;
59-
case "<": return dateTime1 < dateTime2;
60-
case "==": return dateTime1 == dateTime2;
61-
case "!=": return dateTime1 != dateTime2;
62-
case ">=": return dateTime1 >= dateTime2;
63-
case "<=": return dateTime1 <= dateTime2;
64-
default: throw new ArgumentException("Invalid comparison operator.");
65-
};
44+
[HandlebarsWriter(WriterType.Value, Name = "DateTime.ParseExact")]
45+
public DateTime ParseExact(string value, string format)
46+
{
47+
return DateTime.ParseExact(value, format, Context.Configuration.FormatProvider);
6648
}
6749

68-
[HandlebarsWriter(WriterType.Value)]
50+
[HandlebarsWriter(WriterType.Value, Name = "DateTime.Add")]
6951
public DateTime Add(object value, int increment, string datePart, string? format = null)
7052
{
53+
Guard.NotNull(value);
7154
Guard.NotNullOrEmpty(datePart);
7255

73-
if (value is null) throw new ArgumentNullException(nameof(value));
74-
75-
var dateTime = Guard.NotNull(GetDatetime(value, format));
76-
77-
if (dateTime is null) throw new NullReferenceException(nameof(dateTime));
56+
var dateTime = Guard.NotNull(GetDatetime(value, format))!;
7857

79-
switch (datePart)
58+
return datePart switch
8059
{
81-
case "year": return dateTime.Value.AddYears(increment);
82-
case "month": return dateTime.Value.AddMonths(increment);
83-
case "day": return dateTime.Value.AddDays(increment);
84-
case "hour": return dateTime.Value.AddHours(increment);
85-
case "minute": return dateTime.Value.AddMinutes(increment);
86-
case "second": return dateTime.Value.AddSeconds(increment);
87-
case "millisecond": return dateTime.Value.AddMilliseconds(increment);
88-
default: throw new ArgumentException("Invalid date part. It must be one of: [year, month, day, hour, minute, second, millisecond].");
60+
"years" => dateTime.Value.AddYears(increment),
61+
"months" => dateTime.Value.AddMonths(increment),
62+
"days" => dateTime.Value.AddDays(increment),
63+
"hours" => dateTime.Value.AddHours(increment),
64+
"minutes" => dateTime.Value.AddMinutes(increment),
65+
"seconds" => dateTime.Value.AddSeconds(increment),
66+
"milliseconds" => dateTime.Value.AddMilliseconds(increment),
67+
"ticks" => dateTime.Value.AddTicks(increment),
68+
_ => throw new ArgumentException("Invalid date part. It must be one of: [years, months, days, hours, minutes, seconds, milliseconds or ticks].")
8969
};
9070
}
9171

92-
[HandlebarsWriter(WriterType.Value)]
93-
public DateTime Parse(string value)
94-
{
95-
return DateTime.Parse(value, Context.Configuration.FormatProvider);
96-
}
97-
98-
[HandlebarsWriter(WriterType.Value)]
99-
public DateTime ParseExact(string value, string format)
72+
private DateTime? GetDatetime(object? value, string? format)
10073
{
101-
Guard.NotNullOrEmpty(format);
102-
103-
return DateTime.ParseExact(value, format, Context.Configuration.FormatProvider);
104-
}
105-
106-
private DateTime? GetDatetime(object? value, string? format = null)
107-
{
108-
if (value == null) return null;
109-
110-
if (value is DateTime dateTimeValue) return dateTimeValue;
111-
112-
if (value is string stringValue)
113-
return string.IsNullOrEmpty(format) ? Parse(stringValue) : ParseExact(stringValue, format);
114-
115-
return GetDatetime(value.ToString(), format);
74+
return value switch
75+
{
76+
null => null,
77+
DateTime dateTimeValue => dateTimeValue,
78+
string stringValue => string.IsNullOrEmpty(format) ? Parse(stringValue) : ParseExact(stringValue, format),
79+
_ => GetDatetime(value.ToString(), format)
80+
};
11681
}
11782

118-
public Category Category => Category.DateTime;
83+
public override Category Category => Category.DateTime;
11984
}

src/Handlebars.Net.Helpers/Helpers/StringHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public bool Equals(string value, string test, object? stringComparisonType = nul
148148
}
149149

150150
[HandlebarsWriter(WriterType.String)]
151-
public string Format(object? value, string format)
151+
public virtual string Format(object? value, string format)
152152
{
153153
var formatProvider = Context.Configuration.FormatProvider;
154154

@@ -455,5 +455,5 @@ public WrappedString ToWrappedString(object? value)
455455
return FormatAsString(value);
456456
}
457457

458-
public Category Category => Category.String;
458+
public virtual Category Category => Category.String;
459459
}

0 commit comments

Comments
 (0)