Skip to content

Commit

Permalink
Fix: Added polyfill for functions that don't exist on certain verisons
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHougaard committed Aug 31, 2024
1 parent 57a84dd commit fd0eb24
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
70 changes: 70 additions & 0 deletions languages/csharp/Infisical.Sdk/DateTimeTypeForwarding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#if NETSTANDARD2_0 || NETSTANDARD2_1
using System;
using System.Globalization;

namespace System
{
public struct DateOnly
{
private readonly DateTime _date;

public DateOnly(int year, int month, int day)
{
_date = new DateTime(year, month, day);
}

public static DateOnly Parse(string s)
{
return new DateOnly(DateTime.Parse(s).Ticks);
}

public string ToString(string format)
{
return _date.ToString(format);
}

public override string ToString()
{
return _date.ToString("yyyy-MM-dd");
}

public static implicit operator DateTime(DateOnly dateOnly) => dateOnly._date;
public static explicit operator DateOnly(DateTime dateTime) => new DateOnly(dateTime.Year, dateTime.Month, dateTime.Day);

// Additional constructor for ticks
public DateOnly(long ticks)
{
_date = new DateTime(ticks);
}
}

public struct TimeOnly
{
private readonly TimeSpan _timeSpan;

public TimeOnly(int hour, int minute, int second = 0)
{
_timeSpan = new TimeSpan(hours: hour, minutes: minute, seconds: second);
}

public static TimeOnly Parse(string s)
{
var time = DateTime.Parse(s).TimeOfDay;
return new TimeOnly(time.Hours, time.Minutes, time.Seconds);
}

public string ToString(string format)
{
return DateTime.Today.Add(_timeSpan).ToString(format);
}

public override string ToString()
{
return _timeSpan.ToString(@"hh\:mm\:ss");
}

public static implicit operator TimeSpan(TimeOnly timeOnly) => timeOnly._timeSpan;
public static explicit operator TimeOnly(TimeSpan timeSpan) => new TimeOnly(timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
}
}
#endif
19 changes: 18 additions & 1 deletion languages/csharp/Infisical.Sdk/Infisical.Sdk.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Infisical.Sdk</RootNamespace>
<LangVersion>10.0</LangVersion>


<Title>Infisical SDK</Title>
<Authors>Infisical</Authors>
Expand All @@ -30,6 +32,21 @@
<None Include="../LICENSE.txt" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
</ItemGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'netstandard2.1'">
<DefineConstants>$(DefineConstants);NETSTANDARD2_0_OR_GREATER</DefineConstants>
</PropertyGroup>

<!-- Remove files from compilation for non-netstandard targets -->
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0' And '$(TargetFramework)' != 'netstandard2.1'">
<Compile Remove="DateTimeCompatibility.cs" />
</ItemGroup>



<PropertyGroup>
<!--Debugonly-->
</PropertyGroup>
Expand Down

0 comments on commit fd0eb24

Please sign in to comment.