I would like to be able to (on button click) start a countdown (in minute intervals, that also updates every minute) from the current time to a timespan that already exists. How can I do this?
I read somewhere there are a few different timers, I need a solution that I can use in windows phone 7 as well as in a windows forms application.
I'm aware there are a lot of existing questions, I just can't seem to find one that does this exact thing - if someone could point me in the right direction even?
Thanks
You can calculate the number of minutes and seconds between two timespans, then you can subtract a number of seconds or minutes from this new timespan each second to determine a new timespan, this new timespan will reduce each time the subtraction occurs. Once this new timespan reduces to 0, you know the time is up or the countdown has completed.
Related
I am applying the VRP example of optaplanner with time windows and I get feasible solutions whenever I define time windows in a range of 24 hours (00:00 to 23:59). But I am needing:
Manage long trips, where I know that the duration between leaving the depot to the first visit, or durations between visits, will be more than 24 hours. So currently it does not give me workable solutions, because the TW format is in 24 hour format. It happens that when applying the scoring rule "arrivalAfterDueTime", always the "arrivalTime" is higher than the "dueTime", because the "dueTime" is in a range of (00:00 to 23:59) and the "arrivalTime" is the next day.
I have thought that I should take each TW of each Customer and add more TW to it, one for each day that is planned.
Example, if I am planning a trip for 3 days, then I would have 3 time windows in each Customer. Something like this: if Customer 1 is available from [08:00-10:00], then say it will also be available from [32:00-34:00] and [56:00-58:00] which are the equivalent of the same TW for the following days.
Likewise I handle the times with long, converted to milliseconds.
I don't know if this is the right way, my consultation would be more about some ideas to approach this constraint, maybe you have a similar problematic and any idea for me would be very appreciated.
Sorry for the wording, I am a Spanish speaker. Thank you.
Without having checked the example, handing multiple days shouldn't be complicated. It all depends on how you model your time variable.
For example, you could:
model the time stamps as a long value denoted as seconds since epoch. This is how most of the examples are model if I remember correctly. Note that this is not very human-readable, but is the fastest to compute with
you could use a time data type, e.g. LocalTime, this is a human-readable time format but will work in the 24-hour range and will be slower than using a primitive data type
you could use a date time data tpe, e.g LocalDateTime, this is also human-readable and will work in any time range and will also be slower than using a primitive data type.
I would strongly encourage to not simply map the current day or current hour to a zero value and start counting from there. So, in your example you denote the times as [32:00-34:00]. This makes it appear as you are using the current day midnight as the 0th hour and start counting from there. While you can do this it will affect debugging and maintainability of your code. That is just my general advice, you don't have to follow it.
What I would advise is to have your own domain models and map them to Optaplanner models where you use a long value for any time stamp that is denoted as seconds since epoch.
Inside my scene, I have overridden the default -(void) update: (CFTimeInterval) currentTime function. It is worth noting that the currentTime variable is actually the elapsed time since the beginning of a presumably arbitrary system time, as opposed to the elapsed time since last frame. This update function runs through all of my characters and applies their movement based on velocity per second and elapsed time since last update in seconds. The usual way to get the elapsed time since last update in seconds is to have an NSTimeInterval in your class that stores the last update, and subtract the last update time from the current update time, getting elapsed seconds. However, the initial elapsed time is NOT zero. As such, subtracting zero from a very large number produces a perceived elapsed time of many tens of thousands of seconds. As such, any characters already in motion on the very first frame will have moved very far away.
The obvious solution to this would be to initially set the last update time to the initial time elapsed. However, I do not see any method to access this within SKScene, or more specifically within -(void) didMoveToView: (SKView *) view.
Another solution would be to set the initial CFTimeInterval to a negative number, such as -1. Then, one would check each update function whether the time is -1. If so, one would set the elapsed time since last update to 0, otherwise one would do the ordinary elapsedTime = currentTime - lastUpdate. However, doing this if statement every single update function seems messy and unneeded.
Are there any other ways of finding this elapsed time since last update accurately?
You're right that checking for a flag every iteration is wasteful, so it's a question of what's the most efficient solution.
I would think the best (read: most efficient) solution would be to not apply any changes in the first update besides simply saving the currentTime variable. This shouldn't affect your gameplay at all; nobody will notice the frame has been dropped. You could do it by saving a pointer to a function, calling that function in the first iteration of update which will also change the pointer to another function, and then update will call the second function from that point onward.
The method I think you're looking for is CFAbsoluteTimeGetCurrent, but SK's update method tends to hand back a different, earlier time, likely due to the amount of time taken to pass the time into update versus just using CFAbsoluteTimeGetCurrent. If you're OK with a little extra overhead without using an if, disregard currentTime and always use CFAbsoluteTimeGetCurrent in your update method.
Then again, if you're worried about the overhead of a single if statement in each frame, you wouldn't be using a framework. 60 ifs per second is trivial work for even the slowest mobile processor anyway.
I think I understand.. but I may not. So correct me if I'm wrong. But your problem is that your initial delta time is very large, and so using that value to move characters results in them jumping immediately on the first frame of your game. If thats the case, then youre missing a crucial piece of the code in update.
if self.last_update_time == 0.0 { // this is the important part
self.delta = 0
} else {
self.delta = currentTime - self.last_update_time
}
self.last_update_time = currentTime
last_update_time time will always be 0 when your game starts, and delta will always be 0 too. After that point, your delta value will be very small numbers which you can use to progressively move your sprites.
hopefully this was helpful
I have a certain amount of elements, and each of these elements represents one day. Each time midnight occurs (i.e. >>>user<<< time = 00:00), I want the "current" element in the list to expire (and the next one will take its place). Now this seems easy and all, but when you start scratching the surface it's a mess (at least according to me). The problems begin with time zones. If midnight occurs, and after this I change my time zone to one where this particular midnight has not occured yet, then when it does occur "again" in the new time zone, I do not want to count it again (the expired element should remain expired while the element that took its place should count as the current one). Also, when the app is suspended/shut down for a couple of days, I want it to update itself based on the number of valid midnights that occured since last use (as I see it, this makes using UIApplicationSignificantTimeChangeNotification pointless, as it is only sent for the most recent passed midnight).
Ideally, I would like these elements to be totally unaware of dates and time; they should simply be a list 0,1,2,3,... together with a "current element" pointer (i.e. a simple integer), which will be increased for each valid midnight occurence.
How would you suggest that I should implement this?
Base it on UTC midnight, so that no matter what time zone you're in, you're unaffected by the local time change. It eliminates the time zone issue altogether.
I'm relatively new to coding, and wondering if there's a conventional way to have some code execute on every nth iteration of a loop (in this case, an NSTimer ticking).
I'm using a CADisplayLink and it updates however many times per second, 40, 50, whatever. If I want to execute some code on every, say, 500 of those loops, is there a standard way to do so? I assume I could put something together with the modulo operator and an integer, but is there a better / more normalized way that a new coder should know?
Extra clarity (though I'm sure this is a fairly common thing to do..): I have a timer that ticks 60 times per second, but I only want to do something with every 10th iteration. I already know that I can use a modulo and an integer to do this, but I want to know if there's any other convention for handling a situation like this.
Thanks in advance!
Establish a dedicated timer for the right interval.
Regardless of whether you're using NSTimer or CADisplayLink, timer calls can be coalesced and offset if they take too long. If you have a display link that takes 20 frames to run, it only gets called thrice every second (given the refresh interval is 60 frames per second). And if you have a timer set to run every second that at one instance takes a bit more than two and a half seconds to run, it will have "ate" its next iteration and will run the next iteration half a second too late.
Because of this, your timer can get out of sync if you only count timer calls. To do something repeatedly on an interval, having a timer set to that interval is the absolute best approximation.
Having a second timer like this is not a performance problem unless you do very many timers, in which case you should standardize on one tick timer and have events scheduled for specific points in time (which still isn't the same as counting previous iterations).
My question is How can i calculate time duration between sunrise and sunset?
Then how can i divide this duration(sunrise to sunset) into 8 equal parts(Hr: Min)?
All i need is starting and ending time of every part. And i want to write this code in VB.NET
because in VB.NET i can easily design the GUI.
Thanks
A quick search for "sunrise VB.NET" came up with a few results, such as this one.
You can use TimeSpan. Add the sunrise and sunset to it, and then use timespan.Seconds, timespan.Hours etc
To devide it into 8 equal parts, just use timespan.Seconds / 8 and then calculate that into hours, minutes and seconds
If you don't already have the sunrise and sunset time, you can use this project to calculate it: http://www.codeproject.com/KB/cs/SunTime.aspx