<?php
$rainstring = "STAT$RAIN:TOTAL:TODAY$ RAINUNITS$";
$rain = chop($rainstring,"in");
$amount = 0.01;
if ($rain < $amount) {
echo '<td class="right">No rain during this period.</td>';
} else {
echo '<tr>
<td class="right">Total Rain:</td>
<td class="left">' . $rainstring . '</td>
</tr>
<tr>
<td class="right">Rain Rate:</td>
<td class="left">CR$</td>
</tr>';
}
?>
Nothing earth-shaking here! [blush] I might find more use by making a "0" wind indicate "Calm" (with or without a direction [lol] ).
<?php
$light_air = "1";
$light_breeze = "4";
$gentle_breeze = "8";
$moderate_breeze = "14";
$fresh_breeze = "19";
$strong_breeze = "25";
$near_gale = "32";
$severe_gale = "47";
$storm = "55";
$violent_storm = "64";
$hurricane = "74";
$windstring = "CW$";
$pos = strpos($windstring,'MPH');
$wind = substr($windstring, 0, $pos);
if ($wind < $light_air)
{
echo "Calm";
}
else if ($wind < $light_breeze)
{
echo "Light air";
}
else if ($wind < $gentle_breeze)
{
echo "Light breeze";
}
else if ($wind < $moderate_breeze)
{
echo "Gentle breeze ";
}
else if ($wind < $fresh_breeze)
{
echo "Moderate breeze";
}
else if ($wind < $strong_breeze)
{
echo "Fresh breeze";
}
else if ($wind < $near_gale)
{
echo "Strong breeze";
}
else if ($wind < $severe_gale)
{
echo "Near gale";
}
else if ($wind < $storm)
{
echo "Severe gale";
}
else if ($wind < $violent_storm)
{
echo "Storm";
}
else if ($wind < $hurricane)
{
echo "Violent storm";
}
else
{
echo "Hurricane";
}
?>
After being shamed by an AppleScript guru (who shall remain unnamed) living on the Left Coast, I decided to tackle the task of including the run time of the WC computer.
on getMacUpTime()
-- - - - - -
-- Function to get time Macintosh has been running.
-- This information can be displayed on a website
-- for computers left running 24/7
-- - - - - -
local periodString, resultString, resultContent, bootTimeSeconds, UTCtimeSeconds, timeSeconds, uptimeSeconds
-- Set a default value in case there is a problem getting UNIX data
set periodString to "N/A"
try
-- Use system call to get boot time in UNIX seconds.
set resultString to do shell script "sysctl kern.boottime"
on error
-- If can't make UNIX call - return "N/A"
return (periodString)
end try
-- Breakup sysctl command to extract time. Time is 4th item.
set resultContent to words of resultString
set bootTimeSeconds to item 4 of resultContent as integer
-- Get current time as universal time and adjust it to UNIX epoch
set UTCtimeSeconds to (current date) - (time to GMT)
set timeSeconds to UTCtimeSeconds - (date ("1/1/1970"))
-- Compute difference to get uptime in seconds, then covert to elapsed time.
set uptimeSeconds to timeSeconds - bootTimeSeconds
tell WChelperTools to set periodString to seconds2Elapse(uptimeSeconds, false)
return (periodString)
end getMacUpTime
If it were simply the number of seconds since 1900 (or even 2000, when all computers were supposed to crash!), it would be a lot simpler! [tup]
I could use your AS to create my own version of what the OS is already doing. I'm sure there is a PHP script already available to do the same thing. But why buy a wheel when it's so much fun to re-invent one?! [banghead] [biggrin]
Thanks Edouard, that "sysctl kern.boottime" was what I needed. I had found the current time in epoc seconds with "date +%s". But I needed the start up seconds to do the simple math!
I was a bit confused, at first, about your "4th item" in post #7 above. Now I get it! duh! [goofy]
-- Easier to work with epoc seconds (seconds since 1/1/1970) for all timesAs you'll see, the output is:
--
set now_time to do shell script "date +%s"
-- Output-> "1452896555"
set boot_time to do shell script "sysctl kern.boottime"
-- Output->"kern.boottime: { sec = 1452638263, usec = 0 } Tu..."
set run_time to (characters 24 thru 33 of boot_time) as text
-- or string (OK), can't use integer
set running_string to (now_time - run_time) as string
set running_text to running_string as text
set running_integer to running_string as integer
set result to "STRING -->" & running_string & "
TEXT -->" & running_text & "
INTEGER --> " & running_integer
I've already shut down the access to my apple scripting mini, so I'll have to check to morrow. My question is if AS is still 32 bits? I had trouble getting it to write a "small" value to the text file without converting it to scientific notation. Setting a variable to the value 'as integer' or 'as text' still converted it. Even had trouble with 'as string', but by adding a tiny piece of text to it, it was forced to stay as i wanted it. I'll check the numbers tomorrow...
-- *** Applescript code snippet to compute system uptime ***
-- Use system call to get boot time in UNIX seconds.
set resultString to do shell script "sysctl kern.boottime"
-- Breakup sysctl command to extract time. Time is 4th item.
set resultContent to words of resultString
set bootTimeSeconds to item 4 of resultContent as integer
-- Get current time as universal time and adjust it to UNIX epoch
set UTCtimeSeconds to (current date) - (time to GMT)
set timeSeconds to UTCtimeSeconds - (date ("1/1/1970"))
-- Compute difference to get uptime in seconds, then covert to elapsed time.
set uptimeSeconds to timeSeconds - bootTimeSeconds
seconds2Elapse(uptimeSeconds, false)
on seconds2Elapse(totalSeconds, countSeconds)
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-- Function to convert elapsed seconds into a string
-- describing the amount of time that has passed in days,
-- hours, minutes, and optionally seconds
--
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-- 3600 seconds in an hour
-- 86400 seconds in a day
set elapsedTime to " "
set elapsedDays to 0
set elapsedhours to 0
set elapsedMinutes to 0
set elapsedSeconds to 0
set needComma to false
-- Catch limiting case of no elapsed time and return a "reasonable answer"
if (totalSeconds = 0) then
return ("0 seconds")
end if
-- Compute the number of days since event started.
if (totalSeconds is greater than 86400) then
set elapsedDays to totalSeconds div 86400
set totalSeconds to totalSeconds mod 86400
end if
-- Compute the number of hours rain has fallen (after removing days.)
if (totalSeconds is greater than 3600) then
set elapsedhours to totalSeconds div 3600
set totalSeconds to totalSeconds mod 3600
end if
-- Compute number of minutes rain has fallen (after removing hours)
if (totalSeconds is greater than 60) then
set elapsedMinutes to totalSeconds div 60
set elapsedSeconds to totalSeconds mod 60
else
-- if nothing else, their must be seconds
set elapsedSeconds to totalSeconds as integer
end if
-- If countSeconds flag is false ignore leftover seconds.
if (not countSeconds) then
set elapsedSeconds to 0
end if
-- Now create a human readable string by assembling the "pieces."
if (elapsedDays is greater than 0) then
set elapsedTime to elapsedTime & elapsedDays & " " & makePlural(elapsedDays, "day", "days")
set needComma to true
end if
if (elapsedhours is greater than 0) then
if (needComma) then
set elapsedTime to elapsedTime & ", "
end if
set elapsedTime to elapsedTime & elapsedhours & " " & makePlural(elapsedhours, "hour", "hours")
set needComma to true
end if
if (elapsedMinutes is greater than 0) then
if (needComma) then
set elapsedTime to elapsedTime & ", "
end if
set elapsedTime to elapsedTime & elapsedMinutes & " " & makePlural(elapsedMinutes, "minute", "minutes")
set needComma to true
end if
if (elapsedSeconds is greater than 0) then
if (needComma) then
set elapsedTime to elapsedTime & ", "
end if
set elapsedTime to elapsedTime & elapsedSeconds & " " & makePlural(elapsedSeconds, "second", "seconds")
end if
return (elapsedTime)
end seconds2Elapse
on makePlural(value, singularUnit, pluralUnit)
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-- Function to add an 's' to the end of text if the value is greater
-- than one. It is a simple-minded way to get the tense of the unit
-- correct
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ((value is greater than 1) or (value = 0)) then
return (localized string pluralUnit)
else
return (localized string singularUnit)
end if
end makePlural
Only thing worse would be caving to Edouard's AS (assuming it does what I want: Up time in Days, Hours, Minutes). [blush] [lol]
Thanks for all the work put into these, Edouard!
OK. I give up. How do I get that script to run on the webpage? I can run it from the script editor and from the terminal and as an app, but I have noy been able to get it to run in the webpage. I am googled out.
I'm sure Stu has heard this before, but I got a laugh when I read it today:Ninety-nine little bugs in the code,
Take one down, patch it around,
117 little bugs in the code.