Jump to content

how can i make my loop start at certain time?


ncc1701d
 Share

Recommended Posts

I have a nested "for loop below.
It nicely cycles through displaying like clock.
Every count of 60 seconds ups the minute and every 60 minutes ups the hour.
The question I have is.
How can make it so that
I can start the count out at say
hour = 5
minutes = 24
seconds = 30 ?
but not have the for loop affected.

Like for Example..I could just change say s = 0 to s = 5
but when it starts to loop again seconds doesnt start out at 0 but starts at 5 which is not what I want.
for example I only want to start out the program at 5 seconds or some moment in time not loop every 5 seconds.

cuurent code is:


for (var h = 0; h < 24; h++)
{

for (var m = 0; m < 60; m++)
{

for (var s = 0; s < 60; s++)
{

core.debug("hours = " + h + " minutes =" + m + " seconds =" + s);
core.output("hours = " + h + " minutes =" + m + " seconds =" + s);
}
}
}


sample of output looks like this and works but only if var s=0 and var m = 0 and var h = 0

hours = 0 minutes =0 seconds =0
hours = 0 minutes =0 seconds =1
hours = 0 minutes =0 seconds =2

etc

What I want to do is just start right in with counting that would output like this if I started with hours=1,minute=1 and seconds =57 but not screw up my counting clock for loops.
hours = 1 minutes =1 seconds =57
hours = 1 minutes =1 seconds =58
hours = 1 minutes =1 seconds =59
hours = 1 minutes =2 seconds =0
hours = 1 minutes =2 seconds =1
hours = 1 minutes =2 seconds =2

 

Iam hoping there is an if then statement to solve my problem

I dont have usage available to me using any javascript date functions that come with javascript like setMinutes() etc.

thanks

 

Link to comment
Share on other sites

Why are you using for loops? That's going to output everything immediately all at the same time.

 

I need to know more about your ultimate goal before I can provide a real solution.

Link to comment
Share on other sites

My goal is to output line by line in a text file over and over again with the time increasing by 1 second as i go, from a time of my choosing, looking like the example below. (Right now what i have posted only works if i start at hour=0 minute=0 and sec =0)

As it increases the seconds change and eventually the minutes and then hours

Example below just shows what the format would look like when its output.

If there is another way besides loops i am open to it but i cant use the time related functions that come wth javascript.

I canNOT use any functions from here though..http://www.w3schools.com/js/js_date_methods.asp

 

Example of what I would like it to look like is below but I want it to continue indefinatly. Iam not worried about it if it crashes from running indefinatly

hours = 1 minutes =1 seconds =57 //starting point of my choosing like this line i cant do. thats my problem.
hours = 1 minutes =1 seconds =58
hours = 1 minutes =1 seconds =59
hours = 1 minutes =2 seconds =0
hours = 1 minutes =2 seconds =1
hours = 1 minutes =2 seconds =2

Edited by ncc1701d
Link to comment
Share on other sites

Instead of 3 nested for loops, you probably just need 1 while loop. You would have your 3 time variables set to whatever you want, and the interior of the loop will print the time based on those variables and then increment the seconds. If the seconds hit 60 then it resets that to 0 and increments the minutes. If the minutes hit 60 then it resets that to 0 and increments the hours. It would be up to you to figure out the condition to stop that loop.

Link to comment
Share on other sites

The main question is when do you want the loop to stop? Does it stop at 23:59:59 or does it continue until 24 hours later?

Does this counter cycle hours or is 25 a valid value?

 

There must be some other goal for this other than to write known values into a text file, because there is no practical reason to do that.

Link to comment
Share on other sites

Its for a script in "Stellarium" Astronomy program. That program uses javascript language but doesnt recognise lots of time functions that javascript has.

When i run the program in stellariums really basic editor i can stop the script from running manually and i can add "waits" to slow down how fast each line is output to text file. Thats why Iam not too worried about crashes from while loops that go on forever.

As far as when the text file is done being generated i dont care much at this point since i can stop the program manually but

Ultimatly I need to generate a text file that is probably no more than like 36 hours but still greater that 24 hours but i would like to keep the ending "open ended" at this point. The counter per line would increment would work like any digital clock. Iam mainly only interested at this point of the basic counting and looping of numbers per text line using javascript.

Link to comment
Share on other sites

this is my first attempt at it using while but not working out so well. Iam a beginner programmer so i could be missing some very basic things thing.

 

var minutes = 0
var seconds = 50

while (seconds < 100000)
{

if (seconds > 59)
{
var minutes = 0 //resets seconds counter to 0
var seconds = 0 //resets seconds counter to 0
minutes++;
}
seconds++;


core.output("minutes " + minutes +" seconds " + seconds );
core.wait(1);

}
output looks like:
minutes 0 seconds 51
minutes 0 seconds 52
minutes 0 seconds 53
minutes 0 seconds 54
minutes 0 seconds 55
minutes 0 seconds 56
minutes 0 seconds 57
minutes 0 seconds 58
minutes 0 seconds 59
minutes 0 seconds 60 //error should say minutes 1 seconds 0
minutes 1 seconds 1
minutes 1 seconds 2
minutes 1 seconds 3
minutes 1 seconds 4
minutes 1 seconds 5
minutes 1 seconds 6
minutes 1 seconds 7
minutes 1 seconds 8
minutes 1 seconds 9
minutes 1 seconds 10
minutes 1 seconds 11
etc etc moving downward
minutes 1 seconds 55
minutes 1 seconds 56
minutes 1 seconds 57
minutes 1 seconds 58
minutes 1 seconds 59
minutes 1 seconds 60 //problem again
minutes 1 seconds 1 //other problem is the minute counter wont advance anymore Dont know why.
minutes 1 seconds 2
minutes 1 seconds 3
minutes 1 seconds 4

Link to comment
Share on other sites

If you put seconds++ before the if() statement it won't get to 60.

 

The reason minutes never gets above 1 is that every time 'seconds' reaches 60 it resets the minutes to zero, then adds one.

Link to comment
Share on other sites

ok this worked.thanks everyone for your input.

var minutes = 0
var seconds = 50
var hours = 7

while (seconds < 100000)
{
seconds++;

if (seconds > 59)
{

seconds = 0 //resets seconds counter to 0
minutes++;
}

if (minutes > 59)
{

minutes = 0 //resets seconds counter to 0
hours++;
}


//core.output("minutes " + minutes +" seconds " + seconds );
core.output("hours " + hours + "minutes " + minutes +" seconds " + seconds );

core.wait(.4);

}

Link to comment
Share on other sites

This is my approach to the problem:

// Parameters
var hours = 0
var minutes = 50
var seconds = 7
var duration = 24 * 60 * 60;

// Algorithm
var start = hours * 3600 + minutes * 60 + seconds;
var end = start + duration;
var h, m, s;
for(var time = start; time < end; time++) {
  h = Math.floor(time / 3600);
  m = Math.floor(time / 60) % 60;
  s = time % 60;
  core.output("hours " + h + " minutes " + m +" seconds " + s );
  core.wait(.4);
}
Link to comment
Share on other sites

 

This is my approach to the problem:

// Parameters
var hours = 0
var minutes = 50
var seconds = 7
var duration = 24 * 60 * 60;

// Algorithm
var start = hours * 3600 + minutes * 60 + seconds;
var end = start + duration;
var h, m, s;
for(var time = start; time < end; time++) {
  h = Math.floor(time / 3600);
  m = Math.floor(time / 60) % 60;
  s = time % 60;
  core.output("hours " + h + " minutes " + m +" seconds " + s );
  core.wait(.4);
}
Wow. That's very elegant! I like your approach.
Link to comment
Share on other sites

Thanks that is good and tested well. I dont understand math.floor and % yet. I may post a follow up question here soon dealing with expanding my program to include days, months and years. I have added that to my method and am stuck on integrating concepts of leap year and dealing with the fact that some months have 30 days and some having 31 days and feb having 29 or 28 days. Because i added days and months and years as the time increments increase I have to make sure it skips over generating days that dont exist for certain months.

Edited by ncc1701d
Link to comment
Share on other sites

You can learn about the % operator here: http://www.w3schools.com/jsref/jsref_operators.asp

Math.floor() rounds decimal numbers down. http://www.w3schools.com/jsref/jsref_floor.asp

 

Dealing with dates is complicated, which is why most programming languages provide date functions or objects. Javascript has the Date() object. Perhaps you should find the Stellarium documentation to see if it has an alternative. If it doesn't then you will have to build your own date object.

Link to comment
Share on other sites

Foxy Mod...Ya .. without going into to much detail... stellarium has its own time function.....core.setDate("+1 hours");... but it has a bug or something going on where when you use it in a loop it looses acuracy over time. I tryed to get them to look at but they dont seem interested in doing much about.

 

if your interested

I brought up subject originally here

 

https://sourceforge.net/p/stellarium/discussion/278769/thread/6de51bae/

 

"malformatted string" was brought up as the problem in their function and direction to javascripts tutorials to help with alternatives etc lead me here.

 

dsonesuk..I have not tryed your idea yet. Thats still above my capabiliites but food for thought.

Edited by ncc1701d
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...