Can i get a notification, when a new day begins? - cocoa

In Cocoa, is there a notification i can register for, that informs me, when a new day begins - at 00h:00min:01s in the morning?

If it is for iPhone development, you can also listen for a UIApplicationSignificantTimeChangeNotification. It gets posted on more occasions than the arrival of midnight, but when you receive one, you can simply check if you are on or near midnight.
For Mac OS X, you would have to do what Tom Dalling suggests but you should also keep track of changes to the system clock yourself (in order to update your timer) as well as changes to the current time zone.

There's no notification that I know of. You can get a timer to fire whenever a new day begins like so:
[[NSTimer alloc] initWithFireDate:midnight
interval:60 * 60 * 24 //one day, in seconds
target:someObj
selector:#selector(someSelector)
userInfo:nil
repeats:YES];
The trick is getting an NSDate set to midnight. Check the Date and Time Programming Guide for how to do that with date components and the like.
EDIT: see this question for how to get the midnight NSDate.

As of iOS8, you can also directly listen to NSCalendarDayChangedNotification.

As mentioned by others, in iOS 8 you can use NSCalendarDayChangedNotification. In terms of how to do that, this post gives you more info.
Essentially NSCalendarDayChangedNotification requires that you go to your appDelegate file and insert the below code in app (adapted from guide for Swift 3):
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.dayChangedOperations(notif:)), name:NSNotification.Name.NSCalendarDayChanged, object:nil)
Where "ViewController" is the classname of one of my classes and "dayChangedOperations" is the name of the function that I want to run whenever the day changes.
Count Days Since Last Change
Note that all you get from this is an alert that the day changed. You are not given the number of days since the app last ran. So remember to save the date to a userDefault each time this function runs, so you can use it for comparison later.

Related

How do I create a strategy trigger for a daily trade at a certain time?

I want to test a simple strategy based on time: every day at fixed time check some conditions and go long. every day at fixed time exit.
Whatever I try to get timeOpenCondition I get syntax error.
Tried things similar to:
EntryTime = if hour=0800 and minute=0
ExitTime = if hour=1400 and minute=0
It feels like I don't get the concept of how this works. Appreciate any help!

How to do syncronized playback using AVAudioPlayer API?

There is an example here on using AVAudioPlayer. In the description it says it's able to:
Play multiple sounds at the same time with optional synchronization.
I don't see how to do that in the example.
Apple API that says the same thing:
Play multiple sounds simultaneously by synchronizing the playback of multiple players
https://developer.apple.com/documentation/avfaudio/avaudioplayer?language=objc
Example:
https://github.com/xamarin/docs-archive/tree/master/Recipes/ios/media/sound/avaudioplayer
Note: The repository is archived and does not allow adding issues.
Use the playAtTime() method on all the sounds you want and pass in the same date to all the sounds to play at the same time.
I read about the playAtTime() method and thought it was "play at this position in time of the sound" BECAUSE IT SAYS IT SAYS THE PARAMETER IS NAMED TIME NOT DATE:
but it actually takes a Date and that means play at a future date and time.
So if you were only looking at the auto complete API and it says playAtTime(time) you don't get the details you do when looking at the documentation. Seeing that there is another property on sound player that is currentTime that is a number and not a date.
Documentation:
Plays audio asynchronously, starting at a specified point in the audio
output device’s timeline.
func startSynchronizedPlayback() {
// Create a time offset relative to the current device time.
let timeOffset = playerOne.deviceCurrentTime + 0.01
// Start playback of both players at the same time.
playerOne.play(atTime: timeOffset)
playerTwo.play(atTime: timeOffset)
}

Find the difference between 2 dates and check if smaller than a given value

my issue is that I want to be able to get two time stamps and compare if the second (later taken) one is less than 59 minutes away from the first one.
Following this thread Compare two dates with JavaScript
the date object may do the job.
but first thing i am not happy with is that it takes the time from my system.
is it possible to get the time from some public server or something?
cause there always is a chance that the system clock gets manipulated within the time stamps, so that would be too unreliable.
some outside source would be great.
then i am not too sure how to get the difference between 2 times (using 2 date objects).
many issue that may pop up:
time being something like 3:59 and 6:12
so just comparing minutes would give the wrong idea.
so we consider hours too.
biut there the issue with the modulo 24.
day 3 23:59 and day 4 0:33 wouldnt be viewed proper either.
so including days too.
then the modulo 30 thing, even though that on top changes month for month.
so month and year to be included as well.
so we would need the whole date, everything from current year to second (because second would be nice too, for precision)
and comparing them would require tons of if clauses for year, month, etc.
do the date objects have some predfeined date comparision function that actually keeps all these things in mind (havent even mentioned leap years yet, have I)?
time would be very important cause exactly at the 59 minutes mark (+-max 5 seconds wouldnt matter but getting rmeitely close to 60 is forbidden)
a certain function would have to be used that without fail closes a website.
script opens website at mark 0 min, does some stuff rinse and repeat style and closes page at 59 min mark.
checking the time like every few seconds would be smart.
Any good ideas how to implement such a time comparision that doesnt take too more computer power yet is efficient as in new month starting and stuff doesnt mess it up?
You can compare the two Date times, but when creating a date time there is a parameter of DateTime(value) which you can use.
You can use this API to get the current UTC time which returns a example JSON array like this:
{
"$id":"1",
"currentDateTime":"2019-11-09T21:12Z",
"utcOffset":"00:00:00",
"isDayLightSavingsTime":false,
"dayOfTheWeek":"Saturday",
"timeZoneName":"UTC",
"currentFileTime":132178075626292927,
"ordinalDate":"2019-313",
"serviceResponse":null
}
So you can use either the currentFileTime or the currentDateTime return from that API to construct your date object.
Example:
const date1 = new Date('2019-11-09T21:12Z') // time when I started writing this answer
const date2 = new Date('2019-11-09T21:16Z') // time when I finished writing this answer
const diff = new Date(date2-date1)
console.log(diff.toTimeString()) // time it took me to write this
Please keep in mind that due to network speeds, the time API will be a little bit off (by a few milliseconds)

Pebble.js Clock.Weekday function

So, I don't know if I didn't notice before or what but if I use Clock.weekday and assign a day and hour and minute that are very close to the current date (today or tomorrow) it works. If I assign to a day two days later than the current date it says wakeup failed invalid argument when I try to set up a wakeup with that time (time being var time = Clock.weekday(blah blah)...
Am I doing something wrong? or is there a limit with the clock.weekday function?
There was an issue with how moment was being used. It is has now been fixed on master. Thanks for bringing this up!
Here is the commit with the fix.

Any body have any luck with ShellTileSchedule?

Any body have any luck with ShellTileSchedule? I have followed the Microsoft example and still have gotten no where.
"How to: Update Your Tile Without Push Notifications for Windows Phone"
Has any one seen a complete example that works on a device or emulator?
Yes...I started with the sample at http://channel9.msdn.com/learn/courses/WP7TrainingKit/WP7Silverlight/UsingPushNotificationsLab/Exercise-2-Introduction-to-the-Toast-and-Tile-Notifications-for-Alerts/
and skipped immediately down to "Task 3 – Processing Scheduled Tile Notifications on the Phone." After that I had to wait about 1 hour, leaving the emulator running on my desktop (1 hour is the minimum update interval, indicated as such for "performance considerations."
_shellTileSchedule = new ShellTileSchedule
{
Recurrence = UpdateRecurrence.Interval,
Interval = UpdateInterval.EveryHour,
StartTime = DateTime.Now - TimeSpan.FromMinutes(59),
RemoteImageUri = new Uri(#"http://cdn3.afterdawn.fi/news/small/windows-phone-7-series.png")
};
Note that setting the StartTime to DateTime.Now - 59 minutes did nothing. It still waited a full hour for its first update. I could not find any mechanism to perform "go to this URI and Update yourself NOW!", other than calling out to a web service that tickles a Tile Notification.
as #avidgator said, you'll have to wait an hour.
i have written a tutorial on how to update the tile instantly here:
http://www.diaryofaninja.com/blog/2011/04/03/windows-phone-7-live-tile-schedules-ndash-executing-instant-live-tile-updates
basically it involves opening a push/toast update channel and then getting the phone to send "itself" a live tile update request. this will trigger the phone to go and get the tile "right now"
hope this helps
Are the channels necessary for this kind of update?
Is there a full code example of what has to be done to create an app that just updates its tile?
BTW: How about setting the Recurrence to UpdateRecurrence.Onetime and the StartTime to Now + 20 seconds for testing purposes?
I just got an tile update after an hour without channels and so on. So that answered my first question. But having to wait an hour while trying to develop an app is... unsatisfying.
It is easy. Just use the following code when you setup ShellTileSchedule.
ShellTile applicationTile = ShellTile.ActiveTiles.First();
applicationTile.Update(
new StandardTileData {
BackgroundImage = new Uri("www.ash.com/logo.jpg"),
Title = ""
});

Resources