1919
2020import neo4j from '../../src' ;
2121import sharedNeo4j from '../internal/shared-neo4j' ;
22- import { totalNanoseconds } from '../../src/v1/internal/temporal-util' ;
22+ import { timeZoneOffsetInSeconds , totalNanoseconds } from '../../src/v1/internal/temporal-util' ;
2323import { ServerVersion , VERSION_3_4_0 } from '../../src/v1/internal/server-version' ;
2424import timesSeries from 'async/timesSeries' ;
2525import _ from 'lodash' ;
26+ import testUtils from '../internal/test-utils' ;
2627
2728const RANDOM_VALUES_TO_TEST = 2000 ;
2829const MIN_TEMPORAL_ARRAY_LENGTH = 20 ;
@@ -921,6 +922,50 @@ describe('temporal-types', () => {
921922 expect ( ( ) => dateTimeWithZoneOffset ( 1 , 1 , 1 , 1 , 1 , 1 , 1000000000 , 0 ) ) . toThrow ( ) ;
922923 } ) ;
923924
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+
924969 function testSendAndReceiveRandomTemporalValues ( valueGenerator , done ) {
925970 const asyncFunction = ( index , callback ) => {
926971 const next = ( ) => callback ( ) ;
@@ -1117,7 +1162,7 @@ describe('temporal-types', () => {
11171162 function testStandardDateToTimeConversion ( date , nanosecond ) {
11181163 const converted = neo4j . types . Time . fromStandardDate ( date , nanosecond ) ;
11191164 const expected = new neo4j . types . Time ( date . getHours ( ) , date . getMinutes ( ) , date . getSeconds ( ) , totalNanoseconds ( date , nanosecond ) ,
1120- date . getTimezoneOffset ( ) * 60 ) ;
1165+ timeZoneOffsetInSeconds ( date ) ) ;
11211166 expect ( converted ) . toEqual ( expected ) ;
11221167 }
11231168
@@ -1137,7 +1182,14 @@ describe('temporal-types', () => {
11371182 function testStandardDateToDateTimeConversion ( date , nanosecond ) {
11381183 const converted = neo4j . types . DateTime . fromStandardDate ( date , nanosecond ) ;
11391184 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 ) ) ;
11411186 expect ( converted ) . toEqual ( expected ) ;
11421187 }
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+ }
11431195} ) ;
0 commit comments