时区 Hermes 修复
时区 Hermes 修正 是一个跨平台 React Industrial 库,在使用 Hermes JavaScript 引擎时为与时区相關的問題提供强大的工作回放. 它包括Android和iOS的本土模块,被设计为React Industrial项目需要准确的时区处理的滴入解决方案.
QQ 反应原生支持
| 库版本 | 反应原生版本 |
|---|---|
| 0.x - 0.3.x (中文(简体) ) | 0.80.2 - 0.81x (中文(简体) ) |
| 0.4.x (中文(简体) ) | 0.82.x - 0.86.x (英语) |
| 0.5.x (中文(简体) ) | 0.87.x及以上(含) |
问题
使用Hermes JavaScript引擎的回放原生应用程序可能会遇到时区计算问题,特别是在:
- 设备时区在应用程序运行时更改
- 日期/时间计算不反映当前时区
- 不同设备或区域的行为不一致
此库修复这些 已知问题 通过提供本地时区缓存重置.
* 安装
使用 Yarn( 建议)
yarn add @callstack/timezone-hermes-fix
使用 npm
npm install @callstack/timezone-hermes-fix
使用量
快速从虎克开始
使用库最简单的方法是使用提供的回放钩:
import React from 'react';
import { View, Text } from 'react-native';
import { useTimezoneHermesFix } from '@callstack/timezone-hermes-fix';
export default function App() {
const { currentTimezone } = useTimezoneHermesFix();
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Current Timezone: {currentTimezone}</Text>
<Text>Current Time: {new Date().toLocaleString()}</Text>
</View>
);
}
直接本地模块使用
为了进行更多的控制,您可以直接使用本地模块:
import { NativeTimezoneHermesFix } from '@callstack/timezone-hermes-fix';
// Get current timezone information
const timezone = NativeTimezoneHermesFix.getCurrentTimeZone();
console.log('Timezone:', timezone.name); // "America/New_York"
console.log('GMT Offset:', timezone.secondsFromGMT); // -18000 (for EST)
console.log('DST Active:', timezone.isDaylightSavingTime); // true/false
// Get all supported timezones
const supportedTimezones = NativeTimezoneHermesFix.getSupportedTimeZones();
console.log('Available timezones:', supportedTimezones.length);
// Listen for timezone changes
const subscription = NativeTimezoneHermesFix.onTimezoneChange.addListener(
(newTimezone) => {
console.log('Timezone changed to:', newTimezone.name);
console.log('New GMT offset:', newTimezone.secondsFromGMT);
console.log('DST status:', newTimezone.isDaylightSavingTime);
}
);
// Clean up listener when component unmounts
// subscription.remove();
API 参考
类型
type CurrentTimezone = {
name: string; // e.g., "America/New_York"
secondsFromGMT: number; // Offset in seconds from GMT
isDaylightSavingTime: boolean; // Whether DST is currently active
};
方法
getCurrentTimeZone(): CurrentTimezone
返回当前时区信息 .
getSupportedTimeZones(): string[]
返回所有支持的时区标识符的数组.
onTimezoneChange: EventEmitter<CurrentTimezone>
当设备时段变化时,事件会发射起火.
钩子
useTimezoneHermesFix(): { currentTimezone: string }
反应钩子,当时区变化时自动更新.
示例应用程序
一个完整的示例应用程序包含在寄存器中. 运行它:
# Clone the repository
git clone https://github.com/callstack/timezone-hermes-fix.git
cd timezone-hermes-fix
# Install dependencies
yarn
# Run on iOS
yarn example ios
# Run on Android
yarn example android
捐款
我们欢迎捐款! 请看我们的 贡献指南 详细情况.
许可证
这个项目根据麻省理工学院的许可证获得许可。 见 自由主义 详细情况.
由 Calltack 制作
@callstack/timezone-hermes-fix 是一个开源项目,将永远自由使用。 如果你觉得这很酷,请 显示它。 Calltack是一群反动和反动的本地怪人 联系我们 你好@ callstack.com 如果你需要帮忙的话 或者只是想打个招呼!