SNIPPET Code Examples

Dates and Times

The following examples demonstrate how to handle dates and times.

Date/Time Literals

ASSIGN dy=#"11/5/08 6:00PM"

Output:

DY=11/5/2008 6:00:00 PM

The leading pound sign (#) in front of a string interprets the string as a date, time, or a combined date and time. If only a time is specified, the current date is automatically implied. Notice that the Resulting value of DY is a fully formatted date and time string even though the original assignment abbreviated the year and time.

Date/Time Functions

ASSIGN dx=datepart("year", dy) // DX=2008

ASSIGN dx=datepart("month", dy) // DX=11

ASSIGN dx=datepart("hour", dy) // DX=18

ASSIGN dx=datediff("day", #"11/3/2008", dy) // DX=2.75

ASSIGN dx=datediff("minute", #"11/5/2008 4PM", dy) // DX=120

ASSIGN dy=dateadd("hour", dy, 1.5) // DY=39757.8125

ASSIGN dy=#"{dateadd('hour', dy, 1.5).asdatetime}" // DY=11/5/2008 7:30:00 PM

Date/Time Comparisons

IF now >= #"8:00AM" & now < #"5:00PM"

{

ASSIGN IsOpen=1

}

SWITCH date

{

CASE #"1/1/2008" { ASSIGN IsHoliday=1 }

CASE #"12/25/2008" { ASSIGN IsHoliday=1 }

CASE #"12/31/2008" { ASSIGN IsHoliday=1 }

DEFAULT { ASSIGN IsHoliday=0 }

}

 

Time Zone Information

ASSIGN tz="{timezoneid}" // TZ=Mountain Standard Time

Miscellaneous

ASSIGN d="{weekday}" // D=Wednesday

ASSIGN d=dow // D=3

ASSIGN ts=timestamp // TS=6.3361522003499E+17

ASSIGN ts="{timestamp}" // TS=633615220809385200

ASSIGN t2="{utc.asgmt}" // T2=Wed, 05 Nov 2008 22:50:12 GMT

Advanced Examples

Simple Obfuscation

FUNCTION encrypt(ssn)

{

ASSIGN rtn=""

FOR i = 1 to ssn.length

{

SWITCH ssn.mid(i,1)

{

CASE "0" { ASSIGN rtn="{rtn}X" }

CASE "1" { ASSIGN rtn="{rtn}T" }

CASE "2" { ASSIGN rtn="{rtn}U" }

CASE "3" { ASSIGN rtn="{rtn}W" }

CASE "4" { ASSIGN rtn="{rtn}V" }

CASE "5" { ASSIGN rtn="{rtn}A" }

CASE "6" { ASSIGN rtn="{rtn}C" }

CASE "7" { ASSIGN rtn="{rtn}1" }

CASE "8" { ASSIGN rtn="{rtn}R" }

CASE "9" { ASSIGN rtn="{rtn}P" }

}

}

RETURN "{rtn}"

}

FUNCTION decrypt(ssn)

{

ASSIGN rtn=""

FOR i = 1 to ssn.length

{

SWITCH ssn.mid(i,1)

{

CASE "X" { ASSIGN rtn="{rtn}0" }

CASE "T" { ASSIGN rtn="{rtn}1" }

CASE "U" { ASSIGN rtn="{rtn}2" }

CASE "W" { ASSIGN rtn="{rtn}3" }

CASE "V" { ASSIGN rtn="{rtn}4" }

CASE "A" { ASSIGN rtn="{rtn}5" }

CASE "C" { ASSIGN rtn="{rtn}6" }

CASE "1" { ASSIGN rtn="{rtn}7" }

CASE "R" { ASSIGN rtn="{rtn}8" }

CASE "P" { ASSIGN rtn="{rtn}9" }

}

}

RETURN "{rtn}"

}

ASSIGN p1="541195428" // Simulated input Social Security #

ASSIGN essn=encrypt(p1)

ASSIGN myssn=decrypt(essn)

Output:

P1=541195428

ESSN=AVTTPAVUR

MYSSN=541195428

Demo PetShop.dll

The PetShop library is a demonstration DLL which contains most of the features that might be found in a real Web Service. The examples below are shown only for their syntactical merit.

Reading an array of objects

USES "petshop.dll"

ASSIGN shop=new PetShopStore()

ASSIGN cats=shop.Cats

SHOP=PetShopStore

CATS=Cat|Cat|CatReading a single element from an array

ASSIGN mycat=cats[1]

Exploring an object

TRACE "{mycat.name} {mycat.gender} {mycat.breed}"

Invoking a method with no return value

Shop.dogs[1].Bark()

Invoking a method with a return value

ASSIGN bone=shop.dogs[1].FetchBone()

Invoking a method with a return value and outputting one of its properties within a text block

TRACE "{shop.dogs[1].fetchbone().chewed}"

Output: False

Creating a new object and passing it as a parameter to another method

ASSIGN bird=new Bird()

ASSIGN bird.Name="Tweety"

shop.SellPet(bird)

Iterating over an array that is a member of an object

FOREACH dog in shop.dogs

{

TRACE "{dog.name}"

}

Nested expressions – An object from a sub-element passed as an argument

ASSIGN cost=shop.buypet(shop.dogs[2])

COST=10.677

The same expression used in a text block

TRACE "{shop.buypet(shop.dogs[2])}"

Output: 10.677

Using built-in functions to manipulate the output of a method

TRACE "{round(shop.buypet(shop.dogs[2]) * 100) / 100}"

Output: 10.68

Using a custom function

FUNCTION roundmoney(amt)

{

RETURN round(amt*100)/100

}

ASSIGN cost=roundmoney(shop.buypet(shop.dogs[2]))

COST=10.68