Skip to content

Commit f58fa79

Browse files
committed
Makes BigQueryRow.TimestampConverter accept pre 1970 dates.
1 parent 6e6392e commit f58fa79

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

apis/Google.Cloud.BigQuery.V2/Google.Cloud.BigQuery.V2.IntegrationTests/ExhaustiveTypesTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ public void QueryParameters()
151151
}
152152

153153
// Parameterized test to make it easy to add more test cases.
154-
// It looks like we didn't have a problem with pre-1970 dates anyway.
155-
// See https://github.com/googleapis/google-cloud-dotnet/issues/4031 for background.
154+
// See https://github.com/googleapis/google-cloud-dotnet/issues/4031 and
155+
// https://github.com/googleapis/google-cloud-dotnet/issues/4821 for background.
156156
[Theory]
157157
[InlineData("2004-07-26T15:25:28.173333Z")] // Value is "1.090855528173333E9"
158+
[InlineData("1899-12-31T23:00:00.000000Z")] // Vaue is "-2.2089924E9"
158159
public void TimestampRounding(string timestamp)
159160
{
160161
var client = BigQueryClient.Create(_fixture.ProjectId);

apis/Google.Cloud.BigQuery.V2/Google.Cloud.BigQuery.V2.Tests/BigQueryRowTest.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,18 @@ public void RecordWrongFieldCount()
173173
Assert.Throws<InvalidOperationException>(() => row["struct"]);
174174
}
175175

176-
[Fact]
177-
public void TimestampRounding()
176+
public static IEnumerable<object[]> TimestampRoundingData
177+
{
178+
get
179+
{
180+
yield return new object[] { "1.090855528173333E9", new DateTime(2004, 7, 26, 15, 25, 28, DateTimeKind.Utc).AddTicks(1733330) };
181+
yield return new object[] { "-2.2089924E9", new DateTime(1899, 12, 31, 23, 0, 0, 0, DateTimeKind.Utc) };
182+
}
183+
}
184+
185+
[Theory]
186+
[MemberData(nameof(TimestampRoundingData))]
187+
public void TimestampRounding(string rawValue, DateTime expected)
178188
{
179189
var schema = new TableSchemaBuilder
180190
{
@@ -184,11 +194,10 @@ public void TimestampRounding()
184194
{
185195
F = new[]
186196
{
187-
new TableCell { V = "1.090855528173333E9" },
197+
new TableCell { V = rawValue },
188198
}
189199
};
190200
var row = new BigQueryRow(rawRow, schema);
191-
var expected = new DateTime(2004, 7, 26, 15, 25, 28, DateTimeKind.Utc).AddTicks(1733330);
192201
Assert.Equal(expected, (DateTime) row["timestamp"]);
193202
}
194203

apis/Google.Cloud.BigQuery.V2/Google.Cloud.BigQuery.V2/BigQueryRow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ internal BigQueryRow(TableRow rawRow, TableSchema schema, IDictionary<string, in
6565
// Instead, we work out the number of ticks and add that.
6666
private static readonly Func<string, DateTime> TimestampConverter = v =>
6767
{
68-
decimal seconds = decimal.Parse(v, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
68+
decimal seconds = decimal.Parse(v, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);
6969
long microseconds = (long) (seconds * 1e6m);
7070
long ticks = microseconds * 10;
7171
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddTicks(ticks);

0 commit comments

Comments
 (0)