Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added general function for converting datetime to UTC-ZeroDateTime an… #22

Merged
merged 1 commit into from
Nov 14, 2023
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.0.10</Version>
<Version>10.0.11</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>OData Provider</Title>
<Description>The Odata Provider lets you fetch and map data from or to any OData endpoint.</Description>
Expand Down
6 changes: 3 additions & 3 deletions src/ODataSourceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@
DateTime? lastRunDateTime = _mapping.Job.LastSuccessfulRun;
if (lastRunDateTime != null)
{
DateTime dateTimeInUtc = TimeZoneInfo.ConvertTimeToUtc(lastRunDateTime.Value);
string dateTimeFilterName = "";
bool isEdmDate = false;

Expand Down Expand Up @@ -290,13 +289,14 @@

if (!string.IsNullOrWhiteSpace(dateTimeFilterName))
{
var theDateTime = ODataWriter.GetTheDateTimeInZeroTimeZone(lastRunDateTime.Value.ToString(CultureInfo.InvariantCulture), isEdmDate);
if (isEdmDate)
{
dateTimeFilterName += " ge " + dateTimeInUtc.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture) + "z";
dateTimeFilterName += " ge " + theDateTime;
}
else
{
dateTimeFilterName += " gt " + dateTimeInUtc.ToString("yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture) + "z";
dateTimeFilterName += " gt " + theDateTime;
}
result = dateTimeFilterName;
}
Expand Down Expand Up @@ -553,11 +553,11 @@
{
throw exception;
}
task = RetryHelper.RetryOnExceptionAsync<Exception>(10, async () => { _httpRestClient.GetAsync(url, HandleStream, token, (Dictionary<string, string>)headers).Wait(new CancellationTokenSource(timeoutInMilliseconds).Token); }, _logger);

Check warning on line 556 in src/ODataSourceReader.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 556 in src/ODataSourceReader.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
}
else
{
task = RetryHelper.RetryOnExceptionAsync<Exception>(10, async () => { _httpRestClient.GetAsync(url, HandleStream, endpointAuthentication, (Dictionary<string, string>)headers).Wait(new CancellationTokenSource(timeoutInMilliseconds).Token); }, _logger);

Check warning on line 560 in src/ODataSourceReader.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 560 in src/ODataSourceReader.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
}
if (task.IsCanceled)
{
Expand Down
33 changes: 23 additions & 10 deletions src/ODataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public void Write(Dictionary<string, object> Row)
{
primaryKeyColumnValuesForPatch.Add($"{item.Key}='{item.Value}'");
}
else if (columnKeyType == typeof(DateTime))
{
primaryKeyColumnValuesForPatch.Add($"{item.Key}={GetTheDateTimeInZeroTimeZone(item.Value, false)}");
}
else
{
primaryKeyColumnValuesForPatch.Add($"{item.Key}={item.Value}");
Expand Down Expand Up @@ -232,6 +236,10 @@ internal List<string> GetKeyColumnValuesForFilter(Dictionary<string, object> row
{
keyColumnValues.Add($"{keyMapping.DestinationColumn.Name} eq '{keyMapping.ConvertInputValueToOutputValue(row[keyMapping.SourceColumn?.Name] ?? null)}'");
}
else if (keyMapping.DestinationColumn.Type == typeof(DateTime))
{
keyColumnValues.Add($"{keyMapping.DestinationColumn.Name} eq {keyMapping.ConvertInputValueToOutputValue(GetTheDateTimeInZeroTimeZone(row[keyMapping.SourceColumn?.Name], false))}");
}
else
{
keyColumnValues.Add($"{keyMapping.DestinationColumn.Name} eq {keyMapping.ConvertInputValueToOutputValue(row[keyMapping.SourceColumn?.Name] ?? null)}");
Expand Down Expand Up @@ -265,16 +273,7 @@ internal string MapValuesToJSon(ColumnMappingCollection columnMappings, Dictiona
jsonObject.Add(columnMapping.DestinationColumn.Name, Converter.ToDecimal(columnValue));
break;
case "datetime":
var dateTime = Converter.ToDateTime(columnValue);
DateTime dateTimeInUtc = TimeZoneInfo.ConvertTimeToUtc(dateTime);
if (dateTimeInUtc.TimeOfDay.TotalMilliseconds > 0)
{
jsonObject.Add(columnMapping.DestinationColumn.Name, dateTimeInUtc.ToString("yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture) + "z");
}
else
{
jsonObject.Add(columnMapping.DestinationColumn.Name, dateTimeInUtc.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture) + "z");
}
jsonObject.Add(columnMapping.DestinationColumn.Name, GetTheDateTimeInZeroTimeZone(columnValue, false));
break;
default:
jsonObject.Add(columnMapping.DestinationColumn.Name, Converter.ToString(columnValue));
Expand All @@ -285,6 +284,20 @@ internal string MapValuesToJSon(ColumnMappingCollection columnMappings, Dictiona
return jsonObject.ToString();
}

public static string GetTheDateTimeInZeroTimeZone(object dateTimeObject, bool isEdmDate)
{
var dateTime = Converter.ToDateTime(dateTimeObject);
DateTime dateTimeInUtc = TimeZoneInfo.ConvertTimeToUtc(dateTime);
if (dateTimeInUtc.TimeOfDay.TotalMilliseconds > 0 && !isEdmDate)
{
return dateTimeInUtc.ToString("yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture) + "z";
}
else
{
return dateTimeInUtc.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture) + "z";
}
}

public void Dispose() { }

public void Close() { }
Expand Down
Loading