Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/XrmMockup365/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ internal async System.Threading.Tasks.Task ExecuteFormulaFields(EntityMetadata e
continue;
}

entity[attr.LogicalName] = await FormulaFieldEvaluator.Evaluate(definition, entity);
entity[attr.LogicalName] = await FormulaFieldEvaluator.Evaluate(definition, entity, TimeOffset);
}
}

Expand Down
45 changes: 45 additions & 0 deletions src/XrmMockup365/CustomFunction/ISOWeekNumFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.PowerFx;
using Microsoft.PowerFx.Interpreter;
using Microsoft.PowerFx.Types;
using System;
using System.Globalization;

namespace DG.Tools.XrmMockup.CustomFunction
{
internal class ISOWeekNumFunction : ReflectionFunction
{
private readonly TimeSpan timeOffset;

public ISOWeekNumFunction(TimeSpan timeOffset)
: base("ISOWeekNum", FormulaType.Decimal, FormulaType.DateTime)
{
this.timeOffset = timeOffset;
}
public DecimalValue Execute(DateTimeValue date)
{
var utcDate = date?.GetConvertedValue(TimeZoneInfo.Utc);
if (utcDate == null || utcDate.Value <= DateTime.MinValue)
throw new CustomFunctionErrorException("Invalid date or time value", ErrorKind.InvalidArgument);

utcDate = utcDate.Value.Add(timeOffset);

#if DATAVERSE_SERVICE_CLIENT
var weekNumber = ISOWeek.GetWeekOfYear(utcDate.Value);
#else
// .NET Framework does not have ISOWeek class
// Implementing ISO 8601 week date algorithm
// https://learn.microsoft.com/en-us/archive/blogs/shawnste/iso-8601-week-of-year-format-in-microsoft-net

var dayOfWeek = utcDate.Value.DayOfWeek;
if (dayOfWeek >= DayOfWeek.Monday && dayOfWeek <= DayOfWeek.Wednesday)
{
utcDate = utcDate.Value.AddDays(3);
}

var weekNumber = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(utcDate.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
#endif

return FormulaValue.New(weekNumber);
}
}
}
27 changes: 27 additions & 0 deletions src/XrmMockup365/CustomFunction/IsUTCTodayFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.PowerFx;
using Microsoft.PowerFx.Interpreter;
using Microsoft.PowerFx.Types;
using System;

namespace DG.Tools.XrmMockup.CustomFunction
{
internal class IsUTCTodayFunction : ReflectionFunction
{
private readonly TimeSpan timeOffset;

public IsUTCTodayFunction(TimeSpan timeOffset)
: base("IsUTCToday", FormulaType.Boolean, FormulaType.DateTime)
{
this.timeOffset = timeOffset;
}
public BooleanValue Execute(DateTimeValue date)
{
var utcDate = date?.GetConvertedValue(TimeZoneInfo.Utc);
if (utcDate == null || utcDate <= DateTime.MinValue)
throw new CustomFunctionErrorException("Invalid date or time value", ErrorKind.InvalidArgument);

var utcToday = DateTime.UtcNow.Add(timeOffset).Date;
return FormulaValue.New(utcDate.Value.Date == utcToday);
}
}
}
22 changes: 22 additions & 0 deletions src/XrmMockup365/CustomFunction/UTCNowFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.PowerFx;
using Microsoft.PowerFx.Types;
using System;

namespace DG.Tools.XrmMockup.CustomFunction
{
internal class UTCNowFunction : ReflectionFunction
{
private readonly TimeSpan timeOffset;

public UTCNowFunction(TimeSpan timeOffset)
: base("UTCNow", FormulaType.DateTime)
{
this.timeOffset = timeOffset;
}
public DateTimeValue Execute()
{
var utcNow = DateTime.UtcNow.Add(timeOffset);
return FormulaValue.New(utcNow);
}
}
}
23 changes: 23 additions & 0 deletions src/XrmMockup365/CustomFunction/UTCTodayFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.PowerFx;
using Microsoft.PowerFx.Types;
using System;

namespace DG.Tools.XrmMockup.CustomFunction
{
internal class UTCTodayFunction : ReflectionFunction
{
private readonly TimeSpan timeOffset;

public UTCTodayFunction(TimeSpan timeOffset)
: base("UTCToday", FormulaType.DateTime)
{
this.timeOffset = timeOffset;
}

public DateTimeValue Execute()
{
var utcNow = DateTime.UtcNow.Add(timeOffset);
return FormulaValue.New(utcNow.Date);
}
}
}
20 changes: 15 additions & 5 deletions src/XrmMockup365/FormulaFieldEvaluator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using Microsoft.PowerFx.Dataverse;
using DG.Tools.XrmMockup.CustomFunction;
using Microsoft.PowerFx;
using Microsoft.PowerFx.Dataverse;
using Microsoft.Xrm.Sdk;
using System.Threading.Tasks;
using System.Threading;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

namespace DG.Tools.XrmMockup
{
Expand All @@ -18,11 +21,18 @@ public FormulaFieldEvaluator(IOrganizationServiceFactory serviceFactory)
_dataverseConnection = SingleOrgPolicy.New(_organizationService);
}

public async Task<object> Evaluate(string formula, Entity thisEntity)
public async Task<object> Evaluate(string formula, Entity thisEntity, TimeSpan timeOffset)
{
var rowScopeSymbols = _dataverseConnection.GetRowScopeSymbols(thisEntity.LogicalName, true);

var engine = new RecalcEngine();
var config = new PowerFxConfig();
config.AddFunction(new UTCTodayFunction(timeOffset));
config.AddFunction(new UTCNowFunction(timeOffset));
config.AddFunction(new IsUTCTodayFunction(timeOffset));
config.AddFunction(new ISOWeekNumFunction(timeOffset));

var engine = new RecalcEngine(config);

var combinedSymbols = ReadOnlySymbolTable.Compose(rowScopeSymbols, _dataverseConnection.Symbols);
var checkResult = engine.Check(formula, new ParserOptions(CultureInfo.InvariantCulture), combinedSymbols);
checkResult.ThrowOnErrors();
Expand Down
Loading