Business hours verification function
The following sample function isBusinessHours verifies that a timestamp reflects a time that is in the business hours range of 9:00AM to 6:00PM, Monday through Friday. If the time is within the range, the function returns a value of true. Otherwise, it returns a value of false. This example uses the data type helpers API to verify values and decode the timestamps for verification against constants that use more common developer-style values.
/*
Copyright (c) 2007-2009 Netezza Corporation, an IBM Company
All rights reserved.
Function isBusinessHours takes a timestamp and returns true if the
timestamp is between 9 AM and 6 PM on a weekday. Returns false
otherwise. Returns NULL on NULL input.
REGISTRATION:
create or replace function
isBusinessHours(timestamp)
returns bool
language cpp
parameter style npsgeneric
returns null on null input
EXTERNAL CLASS NAME 'IsBusinessHours'
EXTERNAL HOST OBJECT '[host .o_x86 file]'
EXTERNAL SPU OBJECT '[SPU .o_spu10 file]'
USAGE:
create table times (c1 timestamp);
insert into times values ('12/15/2000, 1:50:00 PM');
insert into times values ('3/10/2008, 4:30:05.32 PM');
insert into times values ('3/10/2008, 6:00 PM');
select c1, isBusinessHours(c1) from times;
*/
#include "udxinc.h"
using namespace nz::udx;
using namespace nz::udx::dthelpers;
static const uint8 WORK_START_HOUR=9;
static const uint8 WORK_END_HOUR=18;
class IsBusinessHours : public Udf
{
public:
static Udf* instantiate();
virtual ReturnValue evaluate()
{
if(isArgNull(0))
NZ_UDX_RETURN_NULL();
int64 ts = timestampArg(0);
if(!isValidTimestamp(ts)) //if this test does not pass, we won't
// be able to decode ts
throwUdxException("invalid timestamp passed");
struct tm decomp;
bool err=false;
decodeTimestamp(ts, &decomp, &err);
if(err) //if isValidTimestamp(ts) is true, err should be false,
//but better safe than sorry
throwUdxException("error decoding timestamp");
if(decomp.tm_wday==0||decomp.tm_wday==6||decomp.tm_hour<=WORK_
START_HOUR||decomp.tm_hour>=WORK_END_HOUR)
NZ_UDX_RETURN_BOOL(false);
NZ_UDX_RETURN_BOOL(true);
}
private:
};
Udf* IsBusinessHours::instantiate()
{
return new IsBusinessHours;
}