Skip to content

Commit

Permalink
chore: Convert DateTimeOffset to string with the invariant culture
Browse files Browse the repository at this point in the history
Fixes #911

We should release this as a new patch of the support library, and
release all libraries to depend on it - but only after September 1st.
  • Loading branch information
jskeet committed Aug 21, 2023
1 parent a327f59 commit c33576f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Src/Support/Google.Apis.Core/Util/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ date is null
? null
// While FFF sounds like it should work, we really want to produce no subsecond parts or 3 digits.
: date.Value.Millisecond == 0 ? date.Value.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss'Z'")
: date.Value.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
: date.Value.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'", CultureInfo.InvariantCulture);

/// <summary>
/// Deserializes the given raw value to an object using <see cref="NewtonsoftJsonSerializer.Instance"/>,
Expand Down
19 changes: 19 additions & 0 deletions Src/Support/Google.Apis.Tests/Apis/Utils/UtilitiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

using Google.Apis.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Globalization;
using System.Threading;
Expand Down Expand Up @@ -125,6 +126,24 @@ public void GetStringFromDateTimeOffset_MillisecondHandling(int tickOfSecond, st
Assert.Equal(expectedResult, Utilities.GetStringFromDateTimeOffset(value));
}

[Fact]
public void DateTimeOffsetConversionsAreInvariant()
{
var originalCulture = Thread.CurrentThread.CurrentCulture;
try
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
var dto = new DateTimeOffset(2023, 6, 13, 15, 54, 13, 500, TimeSpan.Zero);
string text = "2023-06-13T15:54:13.500Z";
Assert.Equal(text, Utilities.GetStringFromDateTimeOffset(dto));
Assert.Equal(dto, Utilities.GetDateTimeOffsetFromString(text));
}
finally
{
Thread.CurrentThread.CurrentCulture = originalCulture;
}
}

// Local time of 2023-06-13T15:54:13, with variable UTC offset.
[Theory]
[InlineData(60, "2023-06-13T14:54:13Z")]
Expand Down

0 comments on commit c33576f

Please sign in to comment.