@@ -640,7 +640,8 @@ export class BigQuery extends Service {
640
640
break ;
641
641
}
642
642
case 'TIMESTAMP' : {
643
- value = BigQuery . timestamp ( value ) ;
643
+ const pd = new PreciseDate ( BigInt ( value ) * BigInt ( 1000 ) ) ;
644
+ value = BigQuery . timestamp ( pd ) ;
644
645
break ;
645
646
}
646
647
case 'GEOGRAPHY' : {
@@ -881,6 +882,10 @@ export class BigQuery extends Service {
881
882
* A timestamp represents an absolute point in time, independent of any time
882
883
* zone or convention such as Daylight Savings Time.
883
884
*
885
+ * The recommended input here is a `Date` or `PreciseDate` class.
886
+ * If passing as a `string`, it should be Timestamp literals: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#timestamp_literals.
887
+ * When passing a `number` input, it should be epoch seconds in float representation.
888
+ *
884
889
* @method BigQuery.timestamp
885
890
* @param {Date|string } value The time.
886
891
*
@@ -890,12 +895,19 @@ export class BigQuery extends Service {
890
895
* const timestamp = BigQuery.timestamp(new Date());
891
896
* ```
892
897
*/
898
+ static timestamp ( value : Date | PreciseDate | string | number ) {
899
+ return new BigQueryTimestamp ( value ) ;
900
+ }
893
901
894
902
/**
895
903
* A timestamp represents an absolute point in time, independent of any time
896
904
* zone or convention such as Daylight Savings Time.
897
905
*
898
- * @param {Date|string } value The time.
906
+ * The recommended input here is a `Date` or `PreciseDate` class.
907
+ * If passing as a `string`, it should be Timestamp literals: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#timestamp_literals.
908
+ * When passing a `number` input, it should be epoch seconds in float representation.
909
+ *
910
+ * @param {Date|string|string|number } value The time.
899
911
*
900
912
* @example
901
913
* ```
@@ -904,10 +916,6 @@ export class BigQuery extends Service {
904
916
* const timestamp = bigquery.timestamp(new Date());
905
917
* ```
906
918
*/
907
- static timestamp ( value : Date | PreciseDate | string | number ) {
908
- return new BigQueryTimestamp ( value ) ;
909
- }
910
-
911
919
timestamp ( value : Date | PreciseDate | string | number ) {
912
920
return BigQuery . timestamp ( value ) ;
913
921
}
@@ -2204,6 +2212,11 @@ export class Geography {
2204
2212
2205
2213
/**
2206
2214
* Timestamp class for BigQuery.
2215
+ *
2216
+ * The recommended input here is a `Date` or `PreciseDate` class.
2217
+ * If passing as a `string`, it should be Timestamp literals: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#timestamp_literals.
2218
+ * When passing a `number` input, it should be epoch seconds in float representation.
2219
+ *
2207
2220
*/
2208
2221
export class BigQueryTimestamp {
2209
2222
value : string ;
@@ -2217,13 +2230,15 @@ export class BigQueryTimestamp {
2217
2230
if ( / ^ \d { 4 } - \d { 1 , 2 } - \d { 1 , 2 } / . test ( value ) ) {
2218
2231
pd = new PreciseDate ( value ) ;
2219
2232
} else {
2220
- pd = new PreciseDate ( BigInt ( value ) * BigInt ( 1000 ) ) ;
2233
+ const floatValue = Number . parseFloat ( value ) ;
2234
+ if ( ! Number . isNaN ( floatValue ) ) {
2235
+ pd = this . fromFloatValue_ ( floatValue ) ;
2236
+ } else {
2237
+ pd = new PreciseDate ( value ) ;
2238
+ }
2221
2239
}
2222
- } else if ( value ) {
2223
- pd = new PreciseDate ( BigInt ( value ) * BigInt ( 1000 ) ) ;
2224
2240
} else {
2225
- // Nan or 0 - invalid dates
2226
- pd = new PreciseDate ( value ) ;
2241
+ pd = this . fromFloatValue_ ( value ) ;
2227
2242
}
2228
2243
// to keep backward compatibility, only converts with microsecond
2229
2244
// precision if needed.
@@ -2233,6 +2248,15 @@ export class BigQueryTimestamp {
2233
2248
this . value = new Date ( pd . getTime ( ) ) . toJSON ( ) ;
2234
2249
}
2235
2250
}
2251
+
2252
+ fromFloatValue_ ( value : number ) : PreciseDate {
2253
+ const secs = Math . trunc ( value ) ;
2254
+ // Timestamps in BigQuery have microsecond precision, so we must
2255
+ // return a round number of microseconds.
2256
+ const micros = Math . trunc ( ( value - secs ) * 1e6 + 0.5 ) ;
2257
+ const pd = new PreciseDate ( [ secs , micros * 1000 ] ) ;
2258
+ return pd ;
2259
+ }
2236
2260
}
2237
2261
2238
2262
/**
0 commit comments