19
19
20
20
import neo4j from '../../src' ;
21
21
import sharedNeo4j from '../internal/shared-neo4j' ;
22
- import { totalNanoseconds } from '../../src/v1/internal/temporal-util' ;
22
+ import { timeZoneOffsetInSeconds , totalNanoseconds } from '../../src/v1/internal/temporal-util' ;
23
23
import { ServerVersion , VERSION_3_4_0 } from '../../src/v1/internal/server-version' ;
24
24
import timesSeries from 'async/timesSeries' ;
25
25
import _ from 'lodash' ;
26
+ import testUtils from '../internal/test-utils' ;
26
27
27
28
const RANDOM_VALUES_TO_TEST = 2000 ;
28
29
const MIN_TEMPORAL_ARRAY_LENGTH = 20 ;
@@ -921,6 +922,50 @@ describe('temporal-types', () => {
921
922
expect ( ( ) => dateTimeWithZoneOffset ( 1 , 1 , 1 , 1 , 1 , 1 , 1000000000 , 0 ) ) . toThrow ( ) ;
922
923
} ) ;
923
924
925
+ it ( 'should convert standard Date with offset to neo4j Time' , ( ) => {
926
+ const standardDate1 = testUtils . fakeStandardDateWithOffset ( 0 ) ;
927
+ const neo4jTime1 = neo4j . types . Time . fromStandardDate ( standardDate1 ) ;
928
+ verifyTimeZoneOffset ( neo4jTime1 , 0 , 'Z' ) ;
929
+
930
+ const standardDate2 = testUtils . fakeStandardDateWithOffset ( - 600 ) ;
931
+ const neo4jTime2 = neo4j . types . Time . fromStandardDate ( standardDate2 ) ;
932
+ verifyTimeZoneOffset ( neo4jTime2 , 600 * 60 , '+10:00' ) ;
933
+
934
+ const standardDate3 = testUtils . fakeStandardDateWithOffset ( 480 ) ;
935
+ const neo4jTime3 = neo4j . types . Time . fromStandardDate ( standardDate3 ) ;
936
+ verifyTimeZoneOffset ( neo4jTime3 , - 1 * 480 * 60 , '-08:00' ) ;
937
+
938
+ const standardDate4 = testUtils . fakeStandardDateWithOffset ( - 180 ) ;
939
+ const neo4jTime4 = neo4j . types . Time . fromStandardDate ( standardDate4 ) ;
940
+ verifyTimeZoneOffset ( neo4jTime4 , 180 * 60 , '+03:00' ) ;
941
+
942
+ const standardDate5 = testUtils . fakeStandardDateWithOffset ( 150 ) ;
943
+ const neo4jTime5 = neo4j . types . Time . fromStandardDate ( standardDate5 ) ;
944
+ verifyTimeZoneOffset ( neo4jTime5 , - 1 * 150 * 60 , '-02:30' ) ;
945
+ } ) ;
946
+
947
+ it ( 'should convert standard Date with offset to neo4j DateTime' , ( ) => {
948
+ const standardDate1 = testUtils . fakeStandardDateWithOffset ( 0 ) ;
949
+ const neo4jDateTime1 = neo4j . types . DateTime . fromStandardDate ( standardDate1 ) ;
950
+ verifyTimeZoneOffset ( neo4jDateTime1 , 0 , 'Z' ) ;
951
+
952
+ const standardDate2 = testUtils . fakeStandardDateWithOffset ( - 600 ) ;
953
+ const neo4jDateTime2 = neo4j . types . DateTime . fromStandardDate ( standardDate2 ) ;
954
+ verifyTimeZoneOffset ( neo4jDateTime2 , 600 * 60 , '+10:00' ) ;
955
+
956
+ const standardDate3 = testUtils . fakeStandardDateWithOffset ( 480 ) ;
957
+ const neo4jDateTime3 = neo4j . types . DateTime . fromStandardDate ( standardDate3 ) ;
958
+ verifyTimeZoneOffset ( neo4jDateTime3 , - 1 * 480 * 60 , '-08:00' ) ;
959
+
960
+ const standardDate4 = testUtils . fakeStandardDateWithOffset ( - 180 ) ;
961
+ const neo4jDateTime4 = neo4j . types . DateTime . fromStandardDate ( standardDate4 ) ;
962
+ verifyTimeZoneOffset ( neo4jDateTime4 , 180 * 60 , '+03:00' ) ;
963
+
964
+ const standardDate5 = testUtils . fakeStandardDateWithOffset ( 150 ) ;
965
+ const neo4jDateTime5 = neo4j . types . DateTime . fromStandardDate ( standardDate5 ) ;
966
+ verifyTimeZoneOffset ( neo4jDateTime5 , - 1 * 150 * 60 , '-02:30' ) ;
967
+ } ) ;
968
+
924
969
function testSendAndReceiveRandomTemporalValues ( valueGenerator , done ) {
925
970
const asyncFunction = ( index , callback ) => {
926
971
const next = ( ) => callback ( ) ;
@@ -1117,7 +1162,7 @@ describe('temporal-types', () => {
1117
1162
function testStandardDateToTimeConversion ( date , nanosecond ) {
1118
1163
const converted = neo4j . types . Time . fromStandardDate ( date , nanosecond ) ;
1119
1164
const expected = new neo4j . types . Time ( date . getHours ( ) , date . getMinutes ( ) , date . getSeconds ( ) , totalNanoseconds ( date , nanosecond ) ,
1120
- date . getTimezoneOffset ( ) * 60 ) ;
1165
+ timeZoneOffsetInSeconds ( date ) ) ;
1121
1166
expect ( converted ) . toEqual ( expected ) ;
1122
1167
}
1123
1168
@@ -1137,7 +1182,14 @@ describe('temporal-types', () => {
1137
1182
function testStandardDateToDateTimeConversion ( date , nanosecond ) {
1138
1183
const converted = neo4j . types . DateTime . fromStandardDate ( date , nanosecond ) ;
1139
1184
const expected = new neo4j . types . DateTime ( date . getFullYear ( ) , date . getMonth ( ) + 1 , date . getDate ( ) , date . getHours ( ) , date . getMinutes ( ) , date . getSeconds ( ) ,
1140
- totalNanoseconds ( date , nanosecond ) , date . getTimezoneOffset ( ) * 60 ) ;
1185
+ totalNanoseconds ( date , nanosecond ) , timeZoneOffsetInSeconds ( date ) ) ;
1141
1186
expect ( converted ) . toEqual ( expected ) ;
1142
1187
}
1188
+
1189
+ function verifyTimeZoneOffset ( temporal , expectedValue , expectedStringValue ) {
1190
+ expect ( temporal . timeZoneOffsetSeconds ) . toEqual ( expectedValue ) ;
1191
+ const isoString = temporal . toString ( ) ;
1192
+ // assert ISO string ends with the expected suffix
1193
+ expect ( isoString . indexOf ( expectedStringValue , isoString . length - expectedStringValue . length ) ) . toBeGreaterThan ( 0 ) ;
1194
+ }
1143
1195
} ) ;
0 commit comments