Making Choices

Next -> Responding to Client-Side Data

Now it starts to get good... In this section, I'm going to show you how to return different values from your script in response to specific conditions. You can use this kind of script to do all sorts of nifty stuff. In this case, we're going to set up a script that returns a greeting based upon the time of day.

In order to tackle this, we're going to need more research than we did in the last chapter. To pull this off, we need to

  1. Figure out how to determine the time of day
  2. Divide the day up into parts.
  3. Return the correct response depending on the time

Time of Day

Because it is localized to so many languages, the NewtonOS has a really powerful system for dealing with dates and times. Newton keeps time in one of two ways:

For our purposes, we'll use Time In Minutes since it's the most general and we don't need seconds resolution.

Time in Minutes is pretty useless to humans: We're best at dealing with values like day, week, month, hours, and so on. In every language, the word for these values is different so the core of the Newton date system is the Date Frame3 which is a frame containing month, week, day, hour, minute, etc as integers.

You create a date frame with this kind of Newtonscript: Date(integer time in seconds)

To get today's date, we'd do Date(Time());

which returns on March 9, 1999 3:18 PM this date frame:

{
year: 1999,
month: 3,
Date: 9,
dayOfWeek: 2,
hour: 15,
minute: 18,
second: 0,
daysInMonth: 31
}

To access just the hour slot of the frame, you'd use Date(time()).hour and that's just what we'll do.

Dividing the Day Into Parts

Here are out rules for dividing the day up:

  1. If the hour slot is between 5 and 11 it's Morning
  2. If the hour slot is between 12 and 17 it's Afternoon
  3. If the hour slot is between 18 and 4 it's Evening

Greetings, Visitor!

Name: Greetings

SSI: TIMEGREET

func(nullvar)

begin

//Allocate the second part of the greeting
local theGreet;

//Store the current hour in a variable
local theHour:= Date(time()).hour;

//Simple set of if..then..else statements
//Refer to the Newtonscript Programming Language4
//for full syntax and usage information
if theHour > 4 and theHour < 12 then

theGreet:= "Morning";

else

if theHour > 11 and theHour < 18 then

theGreet:= "Afternoon";

else

if theHour > 17 or theHour < 5 then

theGreet:= "Evening";

//Return our greeting
return "Good" && theGreet;

end

1.NPR: 17-21
2.NPR: 17-22
3.NPR: 17-27
4.NPL: 3-1 to 3-3


Next -> Responding to Client-Side Data