交功课,主要借鉴csyx的代码为主,加上些实际应用,没用类的方式
程序代码:
Clear
? x_秒表加减('0:10:10.123', '1.123')
? x_秒表加减('0:10:10.123', '+1.123')
? x_秒表加减('10:10.123', 1.123)
? x_秒表加减('1:10:10.123', -1.124)
? x_秒表加减('1:10:10.123', '-11:1.124')
? x_秒表加减('2022-07-09T23:59:08.999', '+11:1.124')
? x_秒表加减('2000-01-01T00:10:11.123','1.123')
? x_秒表加减(x_秒表加减(x_秒表加减('0', 1.123), '+11:1.124'), '-1.124')
Function x_秒表加减
Lparameters lcValue1,lcValue2
lcValue2 = Iif(Vartype(lcValue2) == 'N', Transform(lcValue2), Alltrim(lcValue2))
Local ArTime1,ArTime2,LnSs, LnTt, LnMs, LnY, LnM, LnD
Dimension ArTime1[4],ArTime2[4]
ArTime1=x_formatTime(lcValue1)&&格式化输入数据,并装入数组
ArTime2=x_formatTime(lcValue2)
If Vartype(ArTime1)="L" Or Vartype(ArTime2)="L"
Return .F.
Endif
LnY = Year(Date())
LnM = Month(Date())
LnD = Day(Date())
LnSs = ArTime2[1] * 3600 + ArTime2[2] * 60 + ArTime2[3]
If Left(lcValue2, 1) == '-'
LnTt = Datetime(LnY,LnM,LnD, ArTime1[1], ArTime1[2], ArTime1[3]) - LnSs
LnMs = ArTime1[4] - ArTime2[4]
If LnMs < 0
LnTt = LnTt - 1
LnMs = LnMs + 1000
Endif
Else
LnTt = Datetime(LnY,LnM,LnD, ArTime1[1], ArTime1[2], ArTime1[3]) + LnSs
LnMs = ArTime1[4] + ArTime2[4]
If LnMs > 999
LnTt = LnTt + 1
LnMs = LnMs - 1000
Endif
Endif
Return Ttoc(LnTt, 3) + '.' + Padl(LnMs, 3, '0')
Endfunc
Function x_formatTime
Lparameters tcTime
If Occurs("T",tcTime)>0
If Ctod(Getwordnum(tcTime, 1, 'T'))<Date()
Messagebox("日期数据不能小于当时日期",16+0,"停止")
Return .F.
Else
tcTime = Getwordnum(tcTime, 2, 'T')
Endif
Endif
If Occurs(".",tcTime)>1 Or Occurs(":",tcTime)>2 Or Occurs("-",tcTime)>1 Or Occurs("+",tcTime)>1 Or Occurs("+-",tcTime)>0 Or Occurs("-+",tcTime)>0
Messagebox("输入数据格式错误",16+0,"停止")
Return .F.
Endif
lcTm = Ltrim(Alltrim(Getwordnum(tcTime, 1, '.')), '-')
lcTm = Ltrim(lcTm, '+')
lcTm = Replicate ("0:",2-Occurs(":",lcTm))+lcTm
lcMs = Getwordnum(tcTime, 2, '.')
Publ ArTime
Dimension ArTime[4]
ArTime[1] = Cast(Getwordnum(LcTm, 1, ':') As I)
ArTime[2] = Cast(Getwordnum(LcTm, 2, ':') As I)
ArTime[3] = Cast(Getwordnum(LcTm, 3, ':') As I)
ArTime[4] = Cast(LcMs As I)
Return @ArTime
Endfunc
[此贴子已经被作者于2022-7-9 15:06编辑过]