-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new architecture support and region quering
- Loading branch information
Samitha
authored and
Samitha
committed
Dec 10, 2023
1 parent
8b6c2b6
commit 9506b1f
Showing
25 changed files
with
15,108 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,59 @@ | ||
# react-native-timezone | ||
# React Native Timezone and Region | ||
|
||
A Simple react native module to get Timezone and Region of the Android/iOS device. | ||
[![Maintainability](https://api.codeclimate.com/v1/badges/3713253a365fe6a55615/maintainability)](https://codeclimate.com/github/samitha9125/react-native-timezone/maintainability) | ||
|
||
## Installation | ||
A Simple react native module to get Timezone and the Region of the Android/iOS devices. | ||
|
||
```sh | ||
npm install react-native-timezone | ||
``` | ||
# Motivation | ||
|
||
## Usage | ||
For a project of mine, I had to acquire the current selected timezone of the user. But unfortunately I could not find any react native package or react native in-build function which facilitates this. Thus I created a small library. | ||
Also, in v3.0.0 and above, you can access the Region details. More details can be found below. | ||
|
||
```js | ||
import { multiply } from 'react-native-timezone'; | ||
# Compatibility | ||
|
||
// ... | ||
Timezone version 3.0.0 only supports React Native version 0.62.3 and above due to React Native Regular Expression Denial of Service (ReDoS) vulnerability. | ||
|
||
const result = await multiply(3, 7); | ||
``` | ||
| React native version | Tested | Result | | ||
| -------------------- | ------ | ------ | | ||
| 0.62.3 + | ✅ | ✅ | | ||
| 0.70.0 + | ✅ | ✅ | | ||
| 0.73.0 + | ✅ | ✅ | | ||
|
||
# Installation | ||
|
||
`npm i react-native-timezone` | ||
|
||
## iOS | ||
|
||
## Contributing | ||
Do `cd ios/ && pod install`. | ||
|
||
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow. | ||
# Usage | ||
|
||
```javascript | ||
import TimeZone from 'react-native-timezone'; | ||
|
||
export default function App() { | ||
React.useEffect(() => { | ||
const timezone = Timezone.getTimeZone(); | ||
const isAutoTimeZoneEnabled = Timezone.isAutoTimeZoneEnabled(); | ||
const telephonyRegion = Timezone.getRegionByTelephony(); | ||
const localeRegion = Timezone.getRegionByLocale(); | ||
// Update state or use data as needed | ||
}, []); | ||
|
||
// Render your component | ||
} | ||
``` | ||
|
||
## License | ||
Check out the [example]("https://github.com/samitha9125/react-native-timezone/tree/master/example") folder. | ||
|
||
MIT | ||
# APIs | ||
|
||
--- | ||
# API | ||
|
||
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob) | ||
| API | Description | | ||
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| getTimeZone | Android: Returns timezone ID using `java.util.TimeZone.getID()`<br>iOS: Reflects the current system time zone using `localTimeZone` of `NSTimeZone` | | ||
| isAutoTimeZoneEnabled | Returns a boolean indicating if auto timezone is enabled on the device **(Android Only)** | | ||
| getRegionByTelephony | Retrieves the region information based on the telephony (SIM card) of the device. Returns `undefined` if the device has no SIM card. | | ||
| getRegionByLocale | Retrieves the region information based on the device's locale settings | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.timezone"> | ||
package="com.samitha.rn.timezone"> | ||
</manifest> |
60 changes: 60 additions & 0 deletions
60
android/src/main/java/com/samitha/rn/timezone/TimezoneModuleImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.samitha.rn.timezone; | ||
|
||
import androidx.annotation.NonNull; | ||
import android.os.Build; | ||
|
||
import com.facebook.react.bridge.Promise; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactMethod; | ||
|
||
import java.util.TimeZone; | ||
import java.util.Calendar; | ||
import android.telephony.TelephonyManager; | ||
|
||
import android.provider.Settings; | ||
import android.content.ContentResolver; | ||
import android.util.Log; | ||
|
||
public class TimezoneModuleImpl { | ||
public static final String NAME = "Timezone"; | ||
|
||
// Whether the device is set to automatically detect the time zone. | ||
@ReactMethod | ||
public static boolean isAutoTimeZoneEnabled(ReactApplicationContext reactContext) { | ||
ContentResolver resolver = reactContext.getContentResolver(); | ||
return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 | ||
? Settings.Global.getInt(resolver, Settings.Global.AUTO_TIME_ZONE, 0) | ||
: Settings.System.getInt(resolver, Settings.System.AUTO_TIME_ZONE, 0)) != 0; | ||
} | ||
|
||
@ReactMethod | ||
public static String getTimeZone() { | ||
try { | ||
Calendar calendar = Calendar.getInstance(TimeZone.getDefault()); | ||
TimeZone zone = calendar.getTimeZone(); | ||
return zone.getID(); | ||
}catch (Exception e){ | ||
return null; | ||
} | ||
} | ||
|
||
@ReactMethod | ||
public static String getRegionByTelephony(ReactApplicationContext reactContext) { | ||
try { | ||
TelephonyManager tm = (TelephonyManager)reactContext.getSystemService(reactContext.TELEPHONY_SERVICE); | ||
return tm.getNetworkCountryIso(); | ||
}catch (Exception e){ | ||
return null; | ||
} | ||
|
||
} | ||
|
||
@ReactMethod | ||
public static String getRegionByLocale(ReactApplicationContext reactContext) { | ||
try { | ||
return reactContext.getResources().getConfiguration().locale.getCountry(); | ||
}catch (Exception e){ | ||
return null; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...in/java/com/timezone/TimezonePackage.java → .../samitha/rn/timezone/TimezonePackage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.timezone; | ||
package com.samitha.rn.timezone; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
|
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
android/src/newarch/TimezoneSpec.java → android/src/newarch/TimezoneModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.samitha.rn.timezone; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.module.annotations.ReactModule; | ||
|
||
public class TimezoneModule extends ReactContextBaseJavaModule { | ||
public static final String NAME = TimezoneModuleImpl.NAME; | ||
|
||
TimezoneModule(ReactApplicationContext context) { | ||
super(context); | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public String getName() { | ||
return TimezoneModuleImpl.NAME; | ||
} | ||
|
||
@ReactMethod(isBlockingSynchronousMethod = true) | ||
public boolean isAutoTimeZoneEnabled() { | ||
return TimezoneModuleImpl.isAutoTimeZoneEnabled(getReactApplicationContext()); | ||
} | ||
@ReactMethod(isBlockingSynchronousMethod = true) | ||
public String getTimeZone() { | ||
return TimezoneModuleImpl.getTimeZone(); | ||
} | ||
@ReactMethod(isBlockingSynchronousMethod = true) | ||
public String getRegionByTelephony() { | ||
return TimezoneModuleImpl.getRegionByTelephony(getReactApplicationContext()); | ||
} | ||
@ReactMethod(isBlockingSynchronousMethod = true) | ||
public String getRegionByLocale() { | ||
return TimezoneModuleImpl.getRegionByLocale(getReactApplicationContext()); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export NODE_BINARY=/opt/homebrew/opt/node@18/bin/node | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.