# How to use the Time.format() function to give a integer result?

Hey Particle Community,

Hope you are having a s’well time this holiday time,

I’m trying to write some conditional statements that compare time parameters, to do so, I need to have the time variable in the form of an integer.

Essentially, I have a 2D Array which I am checking to find out which period we are currently situated in, noted as ‘i’ using the query:

``````  for(int i = 0; i < 7; i++) {
if (periodStart[day][i] < rawTime < periodEnd[day][i]) {
return period = i;
}
}
``````

where rawTime is

``````rawTime = Time.format(Time.now(), "%H%M");
``````

But I’m not sure how to set it as an integer?

Any ideas?

Thank you,

You can’t, but `Time` has lots of other methods for that

I’d go with `Time.local()` which provides time zone aware epoch time.

And some basic math helps extracting the time

``````int numOfSecond = Time.local() % 86400;
``````

Hi @ScruffR,

Apologies for my delayed response, I was trying to research how to take the epoch value to date/time on the Particle Photon, but I have come without any success.

Would there be a chance you could push me in any direction to get a date/time output that could be plugged into my formula in the original question?

Thank you,

Sam

You want something more like this (untested):

``````int tnow = Time.local();
int thour = Time.hour(tnow);
int tminute = Time.minute(tnow);
int tminaftermidnight = thour * 60 + tminute;  // convert to minutes since midnight
for (int i=0;i<7;i++) {
if ( tminaftermidnight >= periodStart[day][i] && tminuteaftermidnight <= periodEnd[day][i] ) {
return i;
}
}
``````

I’m assuming you can convert the start and end points to minutes after midnight in the same way as the code above. If you need seconds too, then you can add that and count in seconds (thour * 60 * 60 + tminute * 60 + tsecond).

1 Like

@bko, if you want to use the `hour()` method you have to use `Time.now()` instead of `Time.local()` otherwise you’ll have the selected `zone()` offset in there twice (once from `local()`and then again via `hour()`).

But for my original proposal, it’s just the same with less code and a constant factor of 60.

``````  int minSinceMidnight = (Time.local() % 86400) / 60;  // only take the time and drop the date and the drop seconds too
for (int i=0;i<7;i++) {
if ( periodStart[day][i] <= minSinceMidnight && minSinceMidnight < periodEnd[day][i] ) {
return i;
}
}
``````

But if you had your `periodXXX` values calculated as seconds (e.g. `periodStart[0][0] = 8 * 3600 + 30 * 60;` for 8:30), you can even drop the extra `/ 60` and go with seconds since midnight.

2 Likes

Hi @ScruffR and @bko,

Great to hear from you both and thank you for your help,

I have implemented the script:

``````  int minSinceMidnight = (Time.local() % 86400) / 60;  // only take the time and drop the date and the drop seconds too
for (int i=1;i<7;i++) {
if ( periodStart[day][i] <= minSinceMidnight && minSinceMidnight < periodEnd[day][i] ) {
return i;
}
}
``````

But upon testing it is returning a result of 55, I’m not certain why? Any ideas? I thought the maximum it would give would be 6, given the for loop - but I may be wrong.

The two arrays I am checking for the validation are:

``````int periodStart[5][6] = {
{'0905', '1000', '1110', '1205', '1340', '1435'} ,   /*  initializers for row indexed by 0 | MONDAY */
{'0905', '1000', '1110', '1205', '1340', '1435'} ,   /*  initializers for row indexed by 1 | TUESDAY */
{'0850', '0945', '1055', '1150', '1320', '0'} ,   /*  initializers for row indexed by 3 | WEDNESDAY */
{'0905', '1000', '1110', '1205', '1340', '1435'} ,   /*  initializers for row indexed by 4 | THURSDAY*/
{'0905', '1000', '1110', '1205', '1340', '1435'}     /*  initializers for row indexed by 5 | FRIDAY */
};

int periodEnd[5][6] = {
{'1000', '1055', '1205', '1300', '1435', '1530'} ,   /*  initializers for row indexed by 0 | MONDAY */
{'1000', '1055', '1205', '1300', '1435', '1530'} ,   /*  initializers for row indexed by 1 | TUESDAY */
{'0945', '1040', '1150', '1245', '1415', '0'} ,   /*  initializers for row indexed by 3 | WEDNESDAY */
{'1000', '1055', '1205', '1300', '1435', '1530'} ,   /*  initializers for row indexed by 4 | THURSDAY*/
{'1000', '1055', '1205', '1300', '1435', '1530'}     /*  initializers for row indexed by 5 | FRIDAY */
};
``````

As a part of the debugging I added an else return 0 after the if statement to see what might happen and the program only gave 0 as a result.

Sam

You initialization is wrong for several reasons.

1. If you actually were to initialize the arrays with strings, you should have used double quotes (`"`) since single quotes (`'`) are only used for single byte characters.
2. You actually have two `int`arrays which want to be initialized with numbers and not with strings.
3. Since the hour isn’t 100 minutes your times would need to be calculated as `9*60 + 05` to get `9:05` - not `100*9 + 5 = 905`
2 Likes

Dear @ScruffR,

Thank you for those tips, I’m not an expert by any means but I hope learning bit by bit.

After a little tweaking here and there, I have managed to get it working following your guide!

Thank you (and thank you @bko) ,

Sam

3 Likes