在PB中计算指定日期相对数量单位前(后)的日期
来源:程序员人生 发布时间:2014-02-14 20:10:06 阅读次数:3693次
通过灵活使用PB提供的RelativeDate函数还是比较容易实现的,RelativeDate函数要求提供的第一个参数必须为date类型,也即该函数只能处理日期 (不包含时间)运算,因此有关时间部分的运算只能分开执行。以下是f_dateadd函数完整代码,在PB8/9上测试通过。
/*******************************************************************
函数名称:f_dateadd()
参数说明:as_datepart string 日期单位,可取如下值:
year,yyyy,yy,y,年:年;
quarter,qq,季:季;
month,mm,m,月:月;
day,dd,天:天;
week,wk,ww,星期,周:星期;
hour,hh,h,小时:小时
minute,mi,分:分钟;
second,ss,秒:秒;
millisecond,ms,毫秒:毫秒
al_number long 相对数量,取后来日期,为正值, 取前置日期,为负值
adt_refdate datetime 参考日期
返回值:datetime 相对数量单位前(后)的日期
若三个参数中有一个为空,则返回空值,若输入的日期单位非要求的单位也返回空值
功能描述:计算指定日期相对数量单位前(后)的日期
创建日期:2006-07-27
版本号:V1.0
*******************************************************************/
datetime ldt_return
string ls_sql
any la_values[]
long ll_year,ll_month,ll_day,ll_hour,ll_minute,ll_second,ll_millisecond
long ll_day_tmp,ll_hour_tmp,ll_minute_tmp,ll_second_tmp,ll_millisecond_tmp
date ld_return
time lt_return
//检查参数是否有效
if isnull(as_datepart) or isnull(al_number) or isnull(adt_refdate) then
setnull(ldt_return)
return ldt_return
end if
if al_number = 0 then return adt_refdate
//取各部分时间
ll_year = year(date(adt_refdate))
ll_month = month(date(adt_refdate))
ll_day = day(date(adt_refdate))
ll_hour = hour(time(adt_refdate))
ll_minute = minute(time(adt_refdate))
ll_second = second(time(adt_refdate))
ll_millisecond = long(rightw(string(adt_refdate,'yyyy-mm-dd hh:mm:ss.fff'),3))
//根据不同的日期单位进行计算
choose case lower(as_datepart)
case "year","yyyy","yy","y","年"//年
ll_year = ll_year + al_number
if not isdate(string(ll_year) + "-" + string(ll_month) + "-" + string(ll_day)) then
do while isdate(string(ll_year) + "-" + string(ll_month) + "-" + string(ll_day))
ll_day --
loop
end if
ld_return = date(string(ll_year) + "-" + string(ll_month) + "-" + string(ll_day))
lt_return = time(string(ll_hour,'00')+":"+string(ll_minute,'00')+":"+string(ll_second,'00')+"."+string(ll_millisecond,'000'))
case "quarter","qq","q","季"//季
ll_year = ll_year + long((al_number * 3 + ll_month) / 12) - 1
if mod(al_number * 3 + ll_month,12) = 0 then
ll_month = 12
else
ll_month = mod(al_number * 3 + ll_month,12)
end if
if not isdate(string(ll_year) + "-" + string(ll_month) + "-" + string(ll_day)) then
do while isdate(string(ll_year) + "-" + string(ll_month) + "-" + string(ll_day))
ll_day --
loop
end if
ld_return = date(string(ll_year) + "-" + string(ll_month) + "-" + string(ll_day))
lt_return = time(string(ll_hour,'00')+":"+string(ll_minute,'00')+":"+string(ll_second,'00')+"."+string(ll_millisecond,'000'))
case "month","mm","m","月"//月
if al_number > 0 then
ll_year = ll_year + al_number / 12
else
if mod(abs(al_number),12) = 0 then
ll_year = ll_year + al_number / 12
else
ll_year = ll_year + al_number / 12 + 1
end if
end if
ll_month = ll_month + mod(al_number,12)
if ll_month > 12 then
ll_year = ll_year + 1
ll_month = ll_month - 12
elseif ll_month = 0 then
ll_year = ll_year - 1
ll_month = 12
end if
if not isdate(string(ll_year) + "-" + string(ll_month) + "-" + string(ll_day)) then
do while isdate(string(ll_year) + "-" + string(ll_month) + "-" + string(ll_day))
ll_day --
loop
end if
ld_return = date(string(ll_year) + "-" + string(ll_month) + "-" + string(ll_day))
lt_return = time(string(ll_hour,'00')+":"+string(ll_minute,'00')+":"+string(ll_second,'00')+"."+string(ll_millisecond,'000'))
case "day","dd","d","日"//日
ld_return = relativedate (date(adt_refdate),al_number)
lt_return = time(string(ll_hour,'00')+":"+string(ll_minute,'00')+":"+string(ll_second,'00')+"."+string(ll_millisecond,'000'))
case "week","wk","ww","星期","周"//周
return f_dateadd('day',al_number * 7,adt_refdate)
case "hour","hh","h","小时"//小时
ld_return = relativedate (date(adt_refdate),truncate((al_number + ll_hour) / 24,0))
if al_number > 0 then
ll_hour = mod(al_number + ll_hour,24)
else
ll_hour = 24 - mod(abs(al_number + ll_hour),24)
ld_return = relativedate (ld_return,-1)
end if
lt_return = time(string(ll_hour,'00')+":"+string(ll_minute,'00')+":"+string(ll_second,'00')+"."+string(ll_millisecond,'000'))
case "minute","mi","n","分"//分钟
if ll_hour * 60 + ll_minute + al_number < 0 then
ll_day_tmp = ceiling(abs(ll_hour * 60 + ll_minute + al_number) / 60 / 24) * (-1)
else
ll_day_tmp = (ll_hour * 60 + ll_minute + al_number) / 60 / 24
end if
ld_return = relativedate (date(adt_refdate),ll_day_tmp)
ll_hour_tmp = (ll_hour * 60 + ll_minute + al_number - ll_day_tmp * 24 * 60) / 60
ll_minute = ((ll_hour * 60 + ll_minute + al_number - ll_day_tmp * 24 * 60) / 60 - ll_hour_tmp) * 60
ll_hour = ll_hour_tmp
lt_return = time(string(ll_hour,'00')+":"+string(ll_minute,'00')+":"+string(ll_second,'00')+"."+string(ll_millisecond,'000'))
case "second","ss","s","秒"//秒
if ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number < 0 then
ll_day_tmp = ceiling(abs(ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number) / 60 / 60 / 24) * (-1)
else
ll_day_tmp = (ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number) / 60 / 60 / 24
end if
ld_return = relativedate (date(adt_refdate),ll_day_tmp)
ll_hour_tmp = (ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number - ll_day_tmp * 24 * 60 * 60) / 60 / 60
ll_minute_tmp = (ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number - ll_day_tmp * 24 * 60 * 60 - ll_hour_tmp * 60 * 60) / 60
ll_second = ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number - ll_day_tmp * 24 * 60 * 60 - ll_hour_tmp * 60 * 60 - ll_minute_tmp * 60
ll_hour = ll_hour_tmp
ll_minute = ll_minute_tmp
lt_return = time(string(ll_hour,'00')+":"+string(ll_minute,'00')+":"+string(ll_second,'00')+"."+string(ll_millisecond,'000'))
case "millisecond","ms","毫秒"//毫秒
//先换算成秒计算
if ll_millisecond + al_number < 0 then
ll_second_tmp = ceiling(abs(ll_millisecond + al_number) / 1000) * (-1)
else
ll_second_tmp = (ll_millisecond + al_number) / 1000
end if
ll_millisecond = ll_millisecond + al_number - ll_second_tmp * 1000
if ll_millisecond >= 1000 then
ll_second_tmp = ll_second_tmp + 1
ldt_return = f_dateadd('second',ll_second_tmp,adt_refdate)
ll_millisecond = 1000 - ll_millisecond
else
ldt_return = f_dateadd('second',ll_second_tmp,adt_refdate)
end if
ld_return = date(ldt_return)
ll_hour = hour(time(ldt_return))
ll_minute = minute(time(ldt_return))
ll_second = second(time(ldt_return))
lt_return = time(string(ll_hour,'00')+":"+string(ll_minute,'00')+":"+string(ll_second,'00')+"."+string(ll_millisecond,'000'))
case else//无效参数
setnull(ldt_return)
return ldt_return
end choose
ldt_return = datetime(ld_return,lt_return)
return ldt_return
摘自 忘记阁
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠