Git Product home page Git Product logo

mhsntp's Introduction

MHSNTP – SNTP pod

CocoaPods version CocoaPods license

This is a pod for easy SNTP client support in macOS, iOS and tvOS. It's main goals are:

  • RFC 4330 compliance.
  • As much covered by unit tests as possible.
  • Modern Objective-C syntax for good support of both Objective-C and Swift.

This pod does not alter the system's clock. It merely provides easy access to networked time for your apps.

Current limitations

  • Only unicast operation is supported at the moment.

Dependencies

This pod requires CocoaAsyncSocket to handle the UDP communication.

Integration in your project

This project is available as a CocoaPod, so you simply need to add the following line to your Podfile:

pod 'MHSNTP'

There are two ways to integrate this pod: as module or not. To integrate using modules, add use_frameworks! to your Podfile. After this, you reference the pod like this:

// Objective-C
@import MHSNTP;

// Swift
import MHSNTP

If you are not using modules, you need to import the umbrella header in Objective-C:

#import <MHSNTP/MHSNTP.h>

When using the pod in Swift without modules, you need add #import <MHSNTP/MHSNTP.h> to your bridging header. No import MHSNTP is needed in this case.

Usage

The most simple way to use this pod is to instantiate a MHSNTPManager instance and add Apple's NTP servers (there's a convenience method for that).

Objective-C example:

// Store the instance in a property or global variable to keep it alive.
MHSNTPManager * sntp = [[MHSNTPManager alloc] init];
[sntp addAppleSNTPServers];

// After adding the servers, the manager automatically starts querying them.
// You can then fetch the corrected current time via:
NSDate * now = [sntp now];

// Or you can query the offset of the local clock for other time calculations:
NSTimeInterval offset = [sntp systemClockOffset];

Swift example:

let sntp = MHSNTPManager()
sntp.addAppleSNTPServers()

let now = sntp.now()
let offset = sntp.systemClockOffset()

Please note that querying the servers for the first time is not instantaneous. So you may want to instantiate the manager as early as possible/necessary in your app's lifecycle so you hopefully have received responses by the time you actually need to use the network time.

mhsntp's People

Contributors

darkdust avatar

Stargazers

 avatar  avatar

Watchers

 avatar

mhsntp's Issues

Improve unit test helper methods

Right now in ClientTests.swift, when an assertion is triggered in runLocalServerTest or runLocalServerTestKiss, it's not obvious which call to these methods failed. This should be improved.

Users Messing With the System Clock

Hey, I found by way here (and by the way, thanks for MHSNTP!) researching / planning a solution to user interference with the system clock.

In that stead, I think instead of using [NSDate date] to keep track of relative time....

(for example...
- (void)locked_query:(MHSNTPManagerEntry *)entry
entry.lastRequestDate = [NSDate date];
)

that it would be better to use this method of calculating system uptime

Manager should give feedback about whether querying failed.

Right now, there is no way to tell whether querying the time offsets has failed for all servers (for example, because the network is down). This might be useful information.

Maybe a state property might be useful for this. It would also help solve a use-case of #1: waiting for the first responses to arrive.

Post notification on time offset change

The MHSNTPManager should probably post a notification once the calculated time offset has changed. This comes at a little cost and I'm not sure whether it's needed at all. Need to come up with a use-case first.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.