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;








1















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):



enter image description here



enter image description here



How could I fix the gap?










share|improve this question


























  • 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

















1















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):



enter image description here



enter image description here



How could I fix the gap?










share|improve this question


























  • 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













1












1








1








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):



enter image description here



enter image description here



How could I fix the gap?










share|improve this question
















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):



enter image description here



enter image description here



How could I fix the gap?







swift macos timer clock






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












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
);



);













draft saved

draft discarded


















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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현