These are chat archives for MontCode/GeneralChat

15th
Aug 2016
Lily Romano
@Lilyheart
Aug 15 2016 13:24
Here is code I wrote at 2 a.m. I'm still to tired to tell if I did it some silly backwards way and there is a better way or not. Before I send this out to other people involved, input it welcome. Assume the travelTimeSeconds variable actually gets set by a json parse(); I just left it out for simplification of testing (and so don't need to do a JSON call every 5 seconds xD). End result is a return of an easily read string.
    // parse travel time from JSON
var travelTimeSeconds = 600;

var datetimeFormat = "";

//show hours only if exists
if(travelTimeSeconds >= 3600) {
datetimeFormat += Math.floor(travelTimeSeconds / 3600) + "H ";
travelTimeSeconds -= Math.floor(travelTimeSeconds / 3600) * 3600;
}
//append minutes
datetimeFormat += Math.floor(travelTimeSeconds / 60) + ":";
travelTimeSeconds -= Math.floor(travelTimeSeconds / 60) * 60;
//append two digit seconds
if (travelTimeSeconds >9) {
datetimeFormat += travelTimeSeconds;
} else {
datetimeFormat += "0" + travelTimeSeconds;
}
David Castner
@davidjcastner
Aug 15 2016 19:52
@Lilyheart what's the code for
Lily Romano
@Lilyheart
Aug 15 2016 19:53
Converts number of seconds into a more human readable string.
David Castner
@davidjcastner
Aug 15 2016 19:56
Workks well but I thought a standard format for time strings was hh:mm:ss rather than hhH mm:ss, could be wrong though
i think that's what i remember seeing on my stove and microwave
Lily Romano
@Lilyheart
Aug 15 2016 19:58
It's just for a short, easily and quickly read string. If there are hours it would show up 2H 10:12 but no hours would be 21:42. If I glanced at 2:10:02 I might not think 2 minutes vs 2 hours. Is it 2 hours or is it 2 minutes with 2 milliseconds?
David Castner
@davidjcastner
Aug 15 2016 19:58
oh good point
Lily Romano
@Lilyheart
Aug 15 2016 20:00
It's just the core of the string transform coding. I feel like I'm going at it from some weird angle with the if >= then the subtraction
It something for the home automation software I use. Now the dashboard will tell Cletus how long it will take for him to drive to the train station xD Could even do a dedicated LED lamp by the door that shows red if the time is to long (would need to keep the Num value somewhere tho as right now I'm only storing the String). If you wanted to parse out travel time, you would use this url and replace the TO, FROM, and APIKEY with your to and from address and Google API key (google directions). https://maps.googleapis.com/maps/api/distancematrix/json?origins=FROM&destinations=TO&key=APIKEY&language=en&departure_time=now
Then the code above would make a pretty string for you! You just gotta figure out your own json parse code for whatever system you are sticking it in.
David Castner
@davidjcastner
Aug 15 2016 20:04
if you are using es6 you can use string interpolation for string formats
Lily Romano
@Lilyheart
Aug 15 2016 20:05
I'm not. It's not actually a webpage. It's ... hard to describe. It's something in OpenHAB where it pulls an http request and then sends it through a transform file to spit out whatever value I want store. https://github.com/openhab/openhab/wiki/Transformations#java-script-transformation-service
David Castner
@davidjcastner
Aug 15 2016 20:06
const time = { hours: 2, minutes: 3, seconds: 4 };
console.log(${time.hours}H${time.minutes}:\${time.seconds});
Lily Romano
@Lilyheart
Aug 15 2016 20:06
yeah, this is as vanilla as it's gets.
I could convert it to a date object, but then I also have to figure out the days and add it to the hours as .getUTCHours() just give 0-23. I have some code that takes that travelTimeSeconds and adds it to now but since I only poll the travel time every 5 minutes... it gets more complicated xD
David Castner
@davidjcastner
Aug 15 2016 20:09
not compatible with everything but super useful if it was
Lily Romano
@Lilyheart
Aug 15 2016 20:11
You know joda time? That's what OpenHAB uses. xD
David Castner
@davidjcastner
Aug 15 2016 20:20
Never heard of it before
But it's a java library and I'm familiar with java so that's to be expected
Lily Romano
@Lilyheart
Aug 15 2016 20:27
as far as I know toLocaleTimeString just gives you clock time which the same thing I was talkin' about above. 25 hours wouldn't return 25.