Closed destinyxxl closed 3 years ago
The time displayed in the sample app is UTC, time zone 0 (that's what the "+0000" signifies), displayed in an ISO format. It should be fairly simple to change that to displaying time in your locale, both your time zone (which changes in the summer for most countries) and any local formatting. Apple's developer documentation covers these, and many other aspects of time and date, quite well.
The time displayed in the sample app is UTC, time zone 0 (that's what the "+0000" signifies), displayed in an ISO format. It should be fairly simple to change that to displaying time in your locale, both your time zone (which changes in the summer for most countries) and any local formatting. Apple's developer documentation covers these, and many other aspects of time and date, quite well.
thanks for your reply! so i need to add my time zone manually after i call [NSDate date] (or [[NetworkClock sharedNetworkClock] networkTime]) if i want to get my locale time ?
If this is the case, other code call [NSDate date] will also be affected
You are confusing the time, which is the same everywhere, with how it is displayed, which has many variations. A very simple example: at exactly the same time, you could say it was day and I could say it was night, and we'd both be correct. You would benefit from reading Apple's documentation:
https://developer.apple.com/documentation/foundation/nsdate
"NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001)."
Just take the NSDate you get, don't change it, and use Apple's date formatting methods to display it in according to your local conventions (including the logograms I see at the top of your screenshot, if you want).
I've learned,thank you very much for your trouble.
This change to ntpViewController
will show you time in a localized form. It provides an NSDateFormatter
to be used instead of the default .. the default displays the date in the ISO format, the NSDateFormatter
created here displays a more local format. It should be noted that NSDateFormatter
is expensive .. all the complexity of the many different possible formats of an NSDate
makes it computationally intense so they shouldn't be created in a loop. Here, it is created in the (called once) viewDidLoad
and used repeatedly in the (called once a second) timerFireMethod
:
NSDateFormatter * dateFormatter;
- (void)viewDidLoad {
[super viewDidLoad];
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterMediumStyle;
dateFormatter.locale = [NSLocale currentLocale];
netClock = [NetworkClock sharedNetworkClock];
/*┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Create a timer that will fire every second to refresh the text labels in the UI. │
: : : : :
┃ The method executed by the timer -- gets the latest times and displays them. ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛*/
- (void) timerFireMethod:(NSTimer *) theTimer {
_sysClockLabel.text = [NSString stringWithFormat:@"System Clock: %@",
[dateFormatter stringFromDate:[NSDate date]]];
_netClockLabel.text = [NSString stringWithFormat:@"Network Clock: %@",
[dateFormatter stringFromDate:netClock.networkTime]];
_offsetLabel.text = [NSString stringWithFormat:@"Clock Offet: %5.3f mSec", netClock.networkOffset * 1000.0];
}
hi, when i run ios-ntp-app ,both system clock and network clock are eight hours late,what should i do to correct it?thanks! like this