1/* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import {TIME_UNIT_TO_NANO} from './time_units'; 18import {UTCOffset} from './utc_offset'; 19 20describe('UTCOffset', () => { 21 const utcOffset = new UTCOffset(); 22 23 it('sets positive offset for whole single-digit number hours', () => { 24 utcOffset.initialize(BigInt(TIME_UNIT_TO_NANO.h * 2)); 25 expect(utcOffset.format()).toEqual('UTC+02:00'); 26 }); 27 28 it('sets positive offset for whole double-digit number hours', () => { 29 utcOffset.initialize(BigInt(TIME_UNIT_TO_NANO.h * 11)); 30 expect(utcOffset.format()).toEqual('UTC+11:00'); 31 }); 32 33 it('sets positive offset for fractional hours', () => { 34 utcOffset.initialize(BigInt(TIME_UNIT_TO_NANO.h * 5.5)); 35 expect(utcOffset.format()).toEqual('UTC+05:30'); 36 }); 37 38 it('sets negative offset for whole single-digit number hours', () => { 39 utcOffset.initialize(BigInt(TIME_UNIT_TO_NANO.h * -8)); 40 expect(utcOffset.format()).toEqual('UTC-08:00'); 41 }); 42 43 it('sets negative offset for whole double-digit number hours', () => { 44 utcOffset.initialize(BigInt(TIME_UNIT_TO_NANO.h * -10)); 45 expect(utcOffset.format()).toEqual('UTC-10:00'); 46 }); 47 48 it('sets negative offset for fractional hours', () => { 49 utcOffset.initialize(BigInt(TIME_UNIT_TO_NANO.h * -4.5)); 50 expect(utcOffset.format()).toEqual('UTC-04:30'); 51 }); 52 53 it('does not set offset for invalid value', () => { 54 const utcOffset = new UTCOffset(); 55 utcOffset.initialize(BigInt(TIME_UNIT_TO_NANO.h * 15)); // later than UTC+14:00 56 expect(utcOffset.getValueNs()).toBeUndefined(); 57 utcOffset.initialize(BigInt(TIME_UNIT_TO_NANO.h * -13)); // earlier than UTC-12:00 58 expect(utcOffset.getValueNs()).toBeUndefined(); 59 }); 60}); 61