How can I retrieve all the timezones supported by a device? until now i was using:
TimeZone[] zone = TimeZoneUtilities.getAvailableTimeZones();
This is returning the timezones even if not shown in the device timezone list. Please help me to get all the timezones supported by a device. Each device has its own list.
Use
TimeZone[] zone = TimeZoneUtilities.getAvailableTimeZones();
String zones=new String[zone.length];
for(int i=0;i<zone.length; i++)
{
TimeZone tz=zone[i];
zones[i]=TimeZoneUtilities.getDisplayName(tz, TimeZoneUtilities.SHORT) ;
}
Related
Do we have any flutter/native iOS plugins to get the network time during offline mode? Or Is there any option to track the date time automatically settings in iOS device?
If you want to get datetime from device , U can use :
import 'package:intl/intl.dart';
main() {
static final DateTime now = DateTime.now();
static final DateFormat formatter = DateFormat('yyyy-MM-dd');
final String formatted = formatter.format(now);
print(formatted); // something like 2013-04-20
}
If you want to fetch date time from internet you must enable internet in your device and Use this package to get date time from internet.
https://pub.dev/packages/ntp
Converting string to Date fails in jailbroken iPhone devices, but works fine in normal iPhone devices.
Note: I get the date string from a server in UTC format.
My code snippet is as below.
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone
if let date = dateFormatter.date(from: result["timestamp"].stringValue) {
//convert the UTC time to Mexico's local time if string to date conversion is successful
} else {
//failed to get Date from string (happens in Jailbroken devices only)
}
This code enters the else condition when it is run on jailbroken iPhone devices. My client reports that it happens only on jailbroken devices.
When I run the code on non-jailbroken iPhone devices, the string to date conversion works perfectly.
So, I am not getting this: how do I get a Date from a string on "jailbroken" iPhone devices?
Please do help me out.
Thanks in advance.
I am working on an app which asks users for phone number and has a country picker, where the user should input country code.
But while testing null country code values, I found out that somehow my iPhone with iOS 10.2.1 has a region format set to Europe.
The Europe region does not exist in the regions list when I search for it.
When I try to get the current locale country code
NSLocale *currentLocale = [NSLocale currentLocale];
return [currentLocale objectForKey:NSLocaleCountryCode]
I get en_150 which is not useful to determine user's country, at least on my device. Returned country code is 150, which does not exist.
I found a workaround to find to country code by using CTTelephonyNetworkInfo
if(![currentLocale objectForKey:NSLocaleCountryCode]) {
CTCarrier *carrier = [[CTTelephonyNetworkInfo new] subscriberCellularProvider];
NSString *countryCode = carrier.isoCountryCode;
return [countryCode capitalizedString];
}
But how does it work with this Europe locale and why do I have it set like this?
First of all, Europe is a continent not a country. It doesn’t have a country code. It does, however, have a region code. That region code is indeed 150.
Continents have numeric area codes instead including 001 for “World” and 150 for “Europe”.
Be sure to read my article “Are there standard language codes for ‘World English’ and ‘European English’” for a much more detailed explanation. These codes are based in UN M.49, which is the basis for ISO 3166, which is the standard you’re probably thinking of when you say “country codes”.
The problem you’re facing is that you can’t use someone’s language or formatting preferences to determine their physical location. My current device’s locale is en_DK but my physical location is NO. Many devices default to en_US and users all over the world never change the default settings.
Please use CoreLocation to automatically determine the user’s location, or just ask the user to disclose their location.
I'm trying to determine whether to use imperial or metric units automatically before asking the user to enable location services. I know that you can't get precise data but all I really need is United States or not. Can you determine what store it was downloaded from or use IP address or anything like that?
You could use the device's locale to achieve this...
Obj-C
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
Swift
let locale = NSLocale.currentLocale()
let countryCode = locale.objectForKey(NSLocaleCountryCode)
Country codes are in the format US (United States), FR (France), etc...
Note that the locale is based on the user's device settings and not necessarily the current physical location of the device.
You're probably looking for NSLocale:
let theLocale = NSLocale.autoupdatingCurrentLocale()
print(theLocale.objectForKey(NSLocaleMeasurementSystem))
print(theLocale.objectForKey(NSLocaleUsesMetricSystem))
Look at the NSLocale class reference for more options.
Swift 3
NSLocale.current
NSLocale.Key.countryCode
Yes, annoyingly renamed again, don't you have more important stuff to attend Swift team???
It looks like the timestamp property of CLLocation takes the device's current date and time... I have this code snippet:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
NSDate *eventDate = newLocation.timestamp;
long long locTimestamp = [date timeIntervalSince1970] * 1000;
NSLog(#"Loc timestamp: %lld:", locTimestamp);
}
At the moment I'm writing this, if I print the description of eventDate in the Xcode's debug area console, I get:
Printing description of eventDate:
2015-08-26 15:14:09 +0000
And if I change the date in the device's settings, I get that wrong date in evenDate:
Printing description of eventDate:
2015-07-25 15:16:33 +0000
Maybe I'm misunderstanding the NSDate that is returned in the timestamp property... it really takes the date and time you have set in the device, or it is just given the format of those device's settings?
The point is, I need to get UTC times of locations to report them to a server and make some comparisons between different devices, and if user changes the date and time settings of his device and my app sends wrong or "fake" timestamps, my app couldn't perform well...
It seems that it is possible in Android apps to get the NMEA data from GPS and then get the universal time of positions, but I can´t find that in iOS. Is it possible to get it in iOS?
I need help with this issue, thanks in advance
You can convert the timestamps into any calendar or timezone you want via the NSCalendar and NSDateFormatter and NSLocaleclasses. Docs here