1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "ecmascript/platform/time.h" 17 18 #include <ctime> 19 20 namespace panda::ecmascript { 21 static constexpr uint16_t THOUSAND = 1000; 22 static constexpr int SEC_PER_MINUTE = 60; 23 GetLocalOffsetFromOS(int64_t timeMs, bool isLocal)24int64_t GetLocalOffsetFromOS(int64_t timeMs, bool isLocal) 25 { 26 if (!isLocal) { 27 return 0; 28 } 29 timeMs /= THOUSAND; 30 time_t tv = timeMs; 31 struct tm tm { 32 }; 33 // localtime_r is only suitable for linux. 34 struct tm *t = localtime_r(&tv, &tm); 35 // tm_gmtoff includes any daylight savings offset. 36 if (t == nullptr) { 37 return 0; 38 } 39 40 return t->tm_gmtoff / SEC_PER_MINUTE; 41 } 42 IsDst(int64_t timeMs)43bool IsDst(int64_t timeMs) 44 { 45 timeMs /= THOUSAND; 46 time_t tv = timeMs; 47 struct tm tm { 48 }; 49 struct tm *t = nullptr; 50 t = localtime_r(&tv, &tm); 51 return (t != nullptr) && t->tm_isdst; 52 } 53 } // namespace panda::ecmascript 54