4

I know this it the repeated question. I have reviewed many answers but still didn’t get any solution.

My question is, How can I get the Time of Device if user have set it manually..?

I have implemented one chatting app. But in this app, I have issues of timings. If user have set manually time then how can we identify that.?

I want to get correct current UTC time. So using this, I can identify the difference between device time and UTC time.

If we are using, NSDate *currentDateAndTime = [NSDate date];, then it returns only UTC time according to device, not the current UTC time.

Is there any easy way to find out this solution?

Any help would be appreciated.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
  • You need a web service if you don't trust the device's time. – Desdenova May 30 '16 at 12:55
  • 1
    ... or a NTP server, see e.g. this answer: http://stackoverflow.com/questions/14964318/how-can-i-get-the-actual-date-and-time-if-the-device-date-and-time-are-inaccurat. – Martin R May 30 '16 at 12:56

6 Answers6

15

There is no trusted time source in iOS. You just operate with monotonic and non-monotonic time.

Monotonic time (or absolute time) represents the absolute elapsed wall-clock time since some arbitrary, fixed point in the past. The CACurrentMediaTime() or mach_absolute_time return monotonic time.

Non-monotonic time represents the machine's best-guess as to the current wall-clock, time-of-day time. It can jump forwards and backwards as the system time-of-day clock is changed. The [NSDate date] call returns it.

As an option, you can take the trusted date/time from the http Date response header. Then save it and current monotonic time(CACurrentMediaTime()) in the keychain. Here is the way to get the current trusted time:

last known online time + (CACurrentMediaTime() - saved monotonic time)

Another solution would be to use NTP server.

Luiz Dias
  • 1,947
  • 17
  • 22
sgl0v
  • 1,357
  • 11
  • 13
2

This might solve your problem

NSDate * date = [NSDate date];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSString *currentTime = [dateFormatter stringFromDate:date];
        NSLog(@"current time is:%@",currentTime);
icodebuster
  • 8,890
  • 7
  • 62
  • 65
Sucharu Hasija
  • 1,096
  • 11
  • 23
1

use this code it helps you

NSDate * datee = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *localDateString = [dateFormatter stringFromDate:currentDate];

NSLog(@"%@",localDateString);
0

Use : NSDate *currentDateAndTime = [NSDate date];

This will get you the absolute date and time in GMT reference zone.

Nicolas Buquet
  • 3,880
  • 28
  • 28
  • 2
    It is not working, if you are setting manually time without using any time zone. It will returns on device time with UTC time. – Meet Doshi May 30 '16 at 12:52
  • Device always works using UTC time. Only the display of time use timeZone precision, but for display only. – Nicolas Buquet May 30 '16 at 12:54
0

Try this:

swift:

let date = NSDate()
let dateFormatter = NSDateFormatter()
let timeZone = NSTimeZone(name: "UTC")

dateFormatter.timeZone = timeZone

println(dateFormatter.stringFromDate(date))

Obj-C:

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];

[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:timeZone];       
NSLog(@"%@", [dateFormatter stringFromDate:date]);
AkBombe
  • 638
  • 4
  • 10
0

You can use this code for getting current time:-

Swift 3

let date = Date()
let formatter = DateFormatter()
formatter.timeZone = TimeZone(abbreviation: "GMT+0:00") //Current time zone
formatter.dateFormat = "HH:MM a"
let currentTime: String = formatter.stringFromDate(date)
print("My Current Time is \(currentTime)")

Result **
Output : - "My Current Time is 11:05 AM\n"
Community
  • 1
  • 1
Anand Nimje
  • 6,163
  • 4
  • 24
  • 43