How to get the time in sync with macOS system clock when using a timer to update the time?How to create a JQuery Clock / TimerWhy are .NET timers limited to 15 ms resolution?Clock in Discrete Event SimulationSync Swing Timer to system clockWhy are two NSTimers conflicting?Math error during creation of countdown timerNSTimer ConfusionAVaudioplayer audio player progess as Uislider crashesHow to periodically synchronize NTP time with local timer-based timeStart a new timer with remaining time interval
How can I unambiguously ask for a new user's "Display Name"?
How to find out the average duration of the peer-review process for a given journal?
How to gently end involvement with an online community?
Would it be possible to have a GMO that produces chocolate?
Can a Rogue PC teach an NPC to perform Sneak Attack?
Is a player able to change alignment midway through an adventure?
Why do banks “park” their money at the European Central Bank?
Architectural feasibility of a tiered circular stone keep
What is the difference between "Grippe" and "Männergrippe"?
Antonym of "billable"
If all stars rotate, why was there a theory developed that requires non-rotating stars?
Who was president of the USA?
How do we calculate energy of food?
Is using a hyperlink to close a modal a poor design decision?
A discontinuity in an integral
Strange-looking FM transmitter circuit
What is this symbol: semicircles facing each other?
Dealing with an extrovert co-worker
State-of-the-art algorithms for solving linear programs
LeetCode: Group Anagrams C#
Remarkable applications of Dickson's lemma
How to make Ubuntu support single display 5120x1440 resolution?
Compelling story with the world as a villain
Is for(( ... )) ... ; a valid shell syntax? In which shells?
How to get the time in sync with macOS system clock when using a timer to update the time?
How to create a JQuery Clock / TimerWhy are .NET timers limited to 15 ms resolution?Clock in Discrete Event SimulationSync Swing Timer to system clockWhy are two NSTimers conflicting?Math error during creation of countdown timerNSTimer ConfusionAVaudioplayer audio player progess as Uislider crashesHow to periodically synchronize NTP time with local timer-based timeStart a new timer with remaining time interval
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm using this code to show the time in a NSLabel:
Model
func startClock()
timer = Timer(timeInterval: 1.0, target: self, selector: #selector(fireAction), userInfo: nil, repeats: true)
RunLoop.current.add(timer!, forMode: RunLoop.Mode.common)
timer?.tolerance = 0.05
fireAction()
@objc dynamic func fireAction()
delegate?.timeToUpdateClock(self)
ViewController
extension ViewController: ClockWorksProtocol
func timeToUpdateClock(_ timer: ClockWorks)
tick()
func tick()
clockDateLabel.stringValue = DateFormatter.localizedString(from: Date(), dateStyle: .full, timeStyle: .none)
clockTimeLabel.stringValue = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .medium)
Everything seems to work pretty well but unfortunately the seconds shown are not synchronized with the seconds you see in the clock on the menu bar. This happens I think, because I update the clock every second but user don't click when the second is right to start but in a casual interval from a second and the next second (I'm using a timeInterval for the timer of 1.0).
I could change the timeInterval of the timer from 1.0 to 0.1 seconds. This seems to work but I'm afraid this could be too much resource consuming.
I post two screenshots made at the same time. Them show two different time (the seconds):
How could I fix the gap?
swift macos timer clock
add a comment |
I'm using this code to show the time in a NSLabel:
Model
func startClock()
timer = Timer(timeInterval: 1.0, target: self, selector: #selector(fireAction), userInfo: nil, repeats: true)
RunLoop.current.add(timer!, forMode: RunLoop.Mode.common)
timer?.tolerance = 0.05
fireAction()
@objc dynamic func fireAction()
delegate?.timeToUpdateClock(self)
ViewController
extension ViewController: ClockWorksProtocol
func timeToUpdateClock(_ timer: ClockWorks)
tick()
func tick()
clockDateLabel.stringValue = DateFormatter.localizedString(from: Date(), dateStyle: .full, timeStyle: .none)
clockTimeLabel.stringValue = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .medium)
Everything seems to work pretty well but unfortunately the seconds shown are not synchronized with the seconds you see in the clock on the menu bar. This happens I think, because I update the clock every second but user don't click when the second is right to start but in a casual interval from a second and the next second (I'm using a timeInterval for the timer of 1.0).
I could change the timeInterval of the timer from 1.0 to 0.1 seconds. This seems to work but I'm afraid this could be too much resource consuming.
I post two screenshots made at the same time. Them show two different time (the seconds):
How could I fix the gap?
swift macos timer clock
Not synchronized in what sense?Date()
is the time on the clock. It is now, plain and simple.
– matt
Mar 27 at 17:24
I matt, I edited the question to make it more clear. Thank you.
– Cue
Mar 27 at 17:37
2
Instead of a blindly repeating timer, look to see what time it is now and set an interval to fire once at the start of the next second.
– matt
Mar 27 at 20:19
Hi matt, thank you for the useful comment, are you so kind to create an answer so than I can accept it? If you can also add an example to clarify the technique you suggest.
– Cue
Apr 26 at 10:04
add a comment |
I'm using this code to show the time in a NSLabel:
Model
func startClock()
timer = Timer(timeInterval: 1.0, target: self, selector: #selector(fireAction), userInfo: nil, repeats: true)
RunLoop.current.add(timer!, forMode: RunLoop.Mode.common)
timer?.tolerance = 0.05
fireAction()
@objc dynamic func fireAction()
delegate?.timeToUpdateClock(self)
ViewController
extension ViewController: ClockWorksProtocol
func timeToUpdateClock(_ timer: ClockWorks)
tick()
func tick()
clockDateLabel.stringValue = DateFormatter.localizedString(from: Date(), dateStyle: .full, timeStyle: .none)
clockTimeLabel.stringValue = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .medium)
Everything seems to work pretty well but unfortunately the seconds shown are not synchronized with the seconds you see in the clock on the menu bar. This happens I think, because I update the clock every second but user don't click when the second is right to start but in a casual interval from a second and the next second (I'm using a timeInterval for the timer of 1.0).
I could change the timeInterval of the timer from 1.0 to 0.1 seconds. This seems to work but I'm afraid this could be too much resource consuming.
I post two screenshots made at the same time. Them show two different time (the seconds):
How could I fix the gap?
swift macos timer clock
I'm using this code to show the time in a NSLabel:
Model
func startClock()
timer = Timer(timeInterval: 1.0, target: self, selector: #selector(fireAction), userInfo: nil, repeats: true)
RunLoop.current.add(timer!, forMode: RunLoop.Mode.common)
timer?.tolerance = 0.05
fireAction()
@objc dynamic func fireAction()
delegate?.timeToUpdateClock(self)
ViewController
extension ViewController: ClockWorksProtocol
func timeToUpdateClock(_ timer: ClockWorks)
tick()
func tick()
clockDateLabel.stringValue = DateFormatter.localizedString(from: Date(), dateStyle: .full, timeStyle: .none)
clockTimeLabel.stringValue = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .medium)
Everything seems to work pretty well but unfortunately the seconds shown are not synchronized with the seconds you see in the clock on the menu bar. This happens I think, because I update the clock every second but user don't click when the second is right to start but in a casual interval from a second and the next second (I'm using a timeInterval for the timer of 1.0).
I could change the timeInterval of the timer from 1.0 to 0.1 seconds. This seems to work but I'm afraid this could be too much resource consuming.
I post two screenshots made at the same time. Them show two different time (the seconds):
How could I fix the gap?
swift macos timer clock
swift macos timer clock
edited Mar 27 at 18:36
Cue
asked Mar 27 at 17:20
CueCue
1,28020 silver badges33 bronze badges
1,28020 silver badges33 bronze badges
Not synchronized in what sense?Date()
is the time on the clock. It is now, plain and simple.
– matt
Mar 27 at 17:24
I matt, I edited the question to make it more clear. Thank you.
– Cue
Mar 27 at 17:37
2
Instead of a blindly repeating timer, look to see what time it is now and set an interval to fire once at the start of the next second.
– matt
Mar 27 at 20:19
Hi matt, thank you for the useful comment, are you so kind to create an answer so than I can accept it? If you can also add an example to clarify the technique you suggest.
– Cue
Apr 26 at 10:04
add a comment |
Not synchronized in what sense?Date()
is the time on the clock. It is now, plain and simple.
– matt
Mar 27 at 17:24
I matt, I edited the question to make it more clear. Thank you.
– Cue
Mar 27 at 17:37
2
Instead of a blindly repeating timer, look to see what time it is now and set an interval to fire once at the start of the next second.
– matt
Mar 27 at 20:19
Hi matt, thank you for the useful comment, are you so kind to create an answer so than I can accept it? If you can also add an example to clarify the technique you suggest.
– Cue
Apr 26 at 10:04
Not synchronized in what sense?
Date()
is the time on the clock. It is now, plain and simple.– matt
Mar 27 at 17:24
Not synchronized in what sense?
Date()
is the time on the clock. It is now, plain and simple.– matt
Mar 27 at 17:24
I matt, I edited the question to make it more clear. Thank you.
– Cue
Mar 27 at 17:37
I matt, I edited the question to make it more clear. Thank you.
– Cue
Mar 27 at 17:37
2
2
Instead of a blindly repeating timer, look to see what time it is now and set an interval to fire once at the start of the next second.
– matt
Mar 27 at 20:19
Instead of a blindly repeating timer, look to see what time it is now and set an interval to fire once at the start of the next second.
– matt
Mar 27 at 20:19
Hi matt, thank you for the useful comment, are you so kind to create an answer so than I can accept it? If you can also add an example to clarify the technique you suggest.
– Cue
Apr 26 at 10:04
Hi matt, thank you for the useful comment, are you so kind to create an answer so than I can accept it? If you can also add an example to clarify the technique you suggest.
– Cue
Apr 26 at 10:04
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55383123%2fhow-to-get-the-time-in-sync-with-macos-system-clock-when-using-a-timer-to-update%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55383123%2fhow-to-get-the-time-in-sync-with-macos-system-clock-when-using-a-timer-to-update%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not synchronized in what sense?
Date()
is the time on the clock. It is now, plain and simple.– matt
Mar 27 at 17:24
I matt, I edited the question to make it more clear. Thank you.
– Cue
Mar 27 at 17:37
2
Instead of a blindly repeating timer, look to see what time it is now and set an interval to fire once at the start of the next second.
– matt
Mar 27 at 20:19
Hi matt, thank you for the useful comment, are you so kind to create an answer so than I can accept it? If you can also add an example to clarify the technique you suggest.
– Cue
Apr 26 at 10:04