.NET c# ConvertTimeToUtc fails sending to Firestore when using TimeZone UTC+0

Viewed 381

To store DateTime in Google Firestore, you must first convert to UTC. However this does not work with the TimeZone of UTC+0, but works for all other TimeZones.

Example 1: myTimeZone is (UTC+01:00) Casablanca

using Google.Cloud.Firestore;

DateTime localDate = new DateTime(2020, 12, 01);
DateTime utcDate = TimeZoneInfo.ConvertTimeToUtc(localDate, myTimeZone);

Debug.WriteLine("TimeZone: " + myTimeZone);
Debug.WriteLine("localDate : " + localDate.ToString() + " " + localDate.Kind);
Debug.WriteLine("utcDate: " + utcDate.ToString() + " " + utcDate.Kind);

WriteBatch batch = db.StartBatch();
dynamic dataObject = new ExpandoObject();
dataObject.startDate = utcDate;
batch.Set(dataReference, dataObject, SetOptions.MergeAll);

Output:

TimeZone: (UTC+01:00) Casablanca
localDate: 1/12/2020 12:00:00 AM Unspecified
utcDate: 30/11/2020 11:00:00 PM Utc

Data sends correctly.

Example 2: myTimeZone is (UTC+00:00) Dublin, Edinburgh, Lisbon, London

Same code as above.

Output:

TimeZone: (UTC+00:00) Dublin, Edinburgh, Lisbon, London
localDate: 1/12/2020 12:00:00 AM Unspecified
utcDate: 1/12/2020 12:00:00 AM Utc

Exception thrown: 'System.ArgumentException' in Google.Protobuf.dll
System.ArgumentException: Conversion from DateTime to Timestamp requires the DateTime kind to be Utc
Parameter name: dateTime
   at Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(DateTime dateTime) in T:\src\github\protobuf\csharp\src\Google.Protobuf\WellKnownTypes\TimestampPartial.cs:line 150
   at Google.Cloud.Firestore.Converters.DateTimeConverter.Serialize(SerializationContext context, Object value) in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/Converters/SimpleConverters.cs:line 173
   at Google.Cloud.Firestore.ValueSerializer.Serialize(SerializationContext context, Object value) in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/ValueSerializer.cs:line 49
   at Google.Cloud.Firestore.Converters.DictionaryConverter`1.SerializeMap(SerializationContext context, Object value, IDictionary`2 map) in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/Converters/DictionaryConverter.cs:line 65
   at Google.Cloud.Firestore.Converters.MapConverterBase.Serialize(SerializationContext context, Object value) in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/Converters/MapConverterBase.cs:line 35
   at Google.Cloud.Firestore.ValueSerializer.Serialize(SerializationContext context, Object value) in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/ValueSerializer.cs:line 49
   at Google.Cloud.Firestore.Converters.ListConverterBase.Serialize(SerializationContext context, Object value) in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/Converters/ListConverterBase.cs:line 36
   at Google.Cloud.Firestore.ValueSerializer.Serialize(SerializationContext context, Object value) in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/ValueSerializer.cs:line 49
   at Google.Cloud.Firestore.Converters.DictionaryConverter`1.SerializeMap(SerializationContext context, Object value, IDictionary`2 map) in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/Converters/DictionaryConverter.cs:line 65
   at Google.Cloud.Firestore.ValueSerializer.SerializeMap(SerializationContext context, Object value) in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/ValueSerializer.cs:line 61
   at Google.Cloud.Firestore.WriteBatch.Set(DocumentReference documentReference, Object documentData, SetOptions options)
   at CallSite.Target(Closure , CallSite , WriteBatch , DocumentReference , Object , SetOptions )
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid4[T0,T1,T2,T3](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)

I have confirmed that the code works correctly for ALL other TimeZones, except UTC+0.

I'm using .Net 4.7.2 and Google.Cloud.Firestore 2.3.0

Any ideas how to fix? Or a workaround?

Thanks!

1 Answers

The issue was already reported here. I created an internal case with the Firebase team. You can use this issuetracker to track fixing progress or for further communication.

Related