q.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. /*
  2. * Copyright (c) 2000-2018, 达梦数据库有限公司.
  3. * All rights reserved.
  4. */
  5. package dm
  6. import (
  7. "database/sql/driver"
  8. "math"
  9. "strconv"
  10. "strings"
  11. "gitee.com/chunanyong/dm/util"
  12. )
  13. const (
  14. LOADPREC_DEFAULT = 2
  15. LOADPREC_MAX = 9
  16. SECDPREC_DEFAULT = 6
  17. SECDPREC_MAX = 6
  18. QUA_D byte = 3
  19. QUA_DH byte = 4
  20. QUA_DHM byte = 5
  21. QUA_DHMS byte = 6
  22. QUA_H byte = 7
  23. QUA_HM byte = 8
  24. QUA_HMS byte = 9
  25. QUA_M byte = 10
  26. QUA_MS byte = 11
  27. QUA_S byte = 12
  28. )
  29. type DmIntervalDT struct {
  30. _type byte
  31. leadScale int
  32. secScale int
  33. negative bool
  34. days int
  35. hours int
  36. minutes int
  37. seconds int
  38. fraction int
  39. scaleForSvr int
  40. Valid bool
  41. }
  42. func (dt *DmIntervalDT) init() {
  43. dt._type = QUA_D
  44. dt.leadScale = 2
  45. dt.secScale = 6
  46. dt.negative = false
  47. dt.days = 0
  48. dt.hours = 0
  49. dt.minutes = 0
  50. dt.seconds = 0
  51. dt.fraction = 0
  52. dt.scaleForSvr = 0
  53. dt.Valid = true
  54. }
  55. func newDmIntervalDTByBytes(bytes []byte) *DmIntervalDT {
  56. dt := new(DmIntervalDT)
  57. dt.init()
  58. dt._type = bytes[21]
  59. dt.scaleForSvr = int(Dm_build_1.Dm_build_103(bytes, 20))
  60. dt.leadScale = (dt.scaleForSvr >> 4) & 0x0000000F
  61. dt.secScale = dt.scaleForSvr & 0x0000000F
  62. switch dt._type {
  63. case QUA_D:
  64. dt.days = int(Dm_build_1.Dm_build_103(bytes, 0))
  65. case QUA_DH:
  66. dt.days = int(Dm_build_1.Dm_build_103(bytes, 0))
  67. dt.hours = int(Dm_build_1.Dm_build_103(bytes, 4))
  68. case QUA_DHM:
  69. dt.days = int(Dm_build_1.Dm_build_103(bytes, 0))
  70. dt.hours = int(Dm_build_1.Dm_build_103(bytes, 4))
  71. dt.minutes = int(Dm_build_1.Dm_build_103(bytes, 8))
  72. case QUA_DHMS:
  73. dt.days = int(Dm_build_1.Dm_build_103(bytes, 0))
  74. dt.hours = int(Dm_build_1.Dm_build_103(bytes, 4))
  75. dt.minutes = int(Dm_build_1.Dm_build_103(bytes, 8))
  76. dt.seconds = int(Dm_build_1.Dm_build_103(bytes, 12))
  77. dt.fraction = int(Dm_build_1.Dm_build_103(bytes, 16))
  78. case QUA_H:
  79. dt.hours = int(Dm_build_1.Dm_build_103(bytes, 4))
  80. case QUA_HM:
  81. dt.hours = int(Dm_build_1.Dm_build_103(bytes, 4))
  82. dt.minutes = int(Dm_build_1.Dm_build_103(bytes, 8))
  83. case QUA_HMS:
  84. dt.hours = int(Dm_build_1.Dm_build_103(bytes, 4))
  85. dt.minutes = int(Dm_build_1.Dm_build_103(bytes, 8))
  86. dt.seconds = int(Dm_build_1.Dm_build_103(bytes, 12))
  87. dt.fraction = int(Dm_build_1.Dm_build_103(bytes, 16))
  88. case QUA_M:
  89. dt.minutes = int(Dm_build_1.Dm_build_103(bytes, 8))
  90. case QUA_MS:
  91. dt.minutes = int(Dm_build_1.Dm_build_103(bytes, 8))
  92. dt.seconds = int(Dm_build_1.Dm_build_103(bytes, 12))
  93. dt.fraction = int(Dm_build_1.Dm_build_103(bytes, 16))
  94. case QUA_S:
  95. dt.seconds = int(Dm_build_1.Dm_build_103(bytes, 12))
  96. dt.fraction = int(Dm_build_1.Dm_build_103(bytes, 16))
  97. }
  98. if dt.days < 0 {
  99. dt.days = -dt.days
  100. dt.negative = true
  101. }
  102. if dt.hours < 0 {
  103. dt.hours = -dt.hours
  104. dt.negative = true
  105. }
  106. if dt.minutes < 0 {
  107. dt.minutes = -dt.minutes
  108. dt.negative = true
  109. }
  110. if dt.seconds < 0 {
  111. dt.seconds = -dt.seconds
  112. dt.negative = true
  113. }
  114. if dt.fraction < 0 {
  115. dt.fraction = -dt.fraction
  116. dt.negative = true
  117. }
  118. return dt
  119. }
  120. func NewDmIntervalDTByString(str string) (dt *DmIntervalDT, err error) {
  121. defer func() {
  122. if p := recover(); p != nil {
  123. err = ECGO_INVALID_TIME_INTERVAL.throw()
  124. }
  125. }()
  126. dt = new(DmIntervalDT)
  127. dt.init()
  128. if str == "" {
  129. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  130. }
  131. leadStr := strings.TrimSpace(strings.ToUpper(str))
  132. if !(strings.Index(leadStr, "INTERVAL ") == 0) {
  133. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  134. }
  135. leadStr = strings.TrimSpace(leadStr[strings.Index(leadStr, " "):])
  136. endIndex := 0
  137. var valueStr string
  138. if endIndex = strings.Index(leadStr[1:], "'"); leadStr[0] == '\'' && endIndex != -1 {
  139. endIndex += 1
  140. valueStr = strings.TrimSpace(leadStr[1:endIndex])
  141. valueStr = dt.checkSign(valueStr)
  142. leadStr = strings.TrimSpace(leadStr[endIndex+1:])
  143. }
  144. if valueStr == "" {
  145. leadStr = dt.checkSign(leadStr)
  146. if endIndex = strings.Index(leadStr[1:], "'"); leadStr[0] != '\'' || endIndex == -1 {
  147. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  148. }
  149. endIndex += 1
  150. valueStr = strings.TrimSpace(leadStr[1:endIndex])
  151. leadStr = strings.TrimSpace(leadStr[endIndex+1:])
  152. }
  153. strLeadPrec := ""
  154. strSecPrec := ""
  155. leadPrecIndex := 0
  156. secPrecIndex := 0
  157. toIndex := 0
  158. if leadPrecIndex = strings.Index(leadStr, "DAY"); leadPrecIndex != -1 {
  159. toIndex = strings.Index(leadStr[leadPrecIndex:], "TO")
  160. if toIndex == -1 {
  161. strLeadPrec = strings.TrimSpace(leadStr[leadPrecIndex:])
  162. if err := dt.setDay(valueStr); err != nil {
  163. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  164. }
  165. } else {
  166. toIndex += leadPrecIndex
  167. strLeadPrec = strings.TrimSpace(leadStr[leadPrecIndex:toIndex])
  168. if strings.Index(leadStr[toIndex:], "HOUR") != -1 {
  169. if err := dt.setDayToHour(valueStr); err != nil {
  170. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  171. }
  172. } else if strings.Index(leadStr[toIndex:], "MINUTE") != -1 {
  173. if err := dt.setDayToMinute(valueStr); err != nil {
  174. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  175. }
  176. } else if secPrecIndex = strings.Index(leadStr[toIndex:], "SECOND"); secPrecIndex != -1 {
  177. secPrecIndex += toIndex
  178. strSecPrec = leadStr[secPrecIndex:]
  179. if err := dt.setDayToSecond(valueStr); err != nil {
  180. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  181. }
  182. } else {
  183. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  184. }
  185. }
  186. if err := dt.setPrecForSvr(leadStr, strLeadPrec, strSecPrec); err != nil {
  187. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  188. }
  189. return dt, nil
  190. }
  191. if leadPrecIndex = strings.Index(leadStr, "HOUR"); leadPrecIndex != -1 {
  192. toIndex = strings.Index(leadStr[leadPrecIndex:], "TO")
  193. if toIndex == -1 {
  194. toIndex += leadPrecIndex
  195. strLeadPrec = leadStr[leadPrecIndex:]
  196. if err := dt.setHour(valueStr); err != nil {
  197. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  198. }
  199. } else {
  200. strLeadPrec = leadStr[leadPrecIndex:toIndex]
  201. if strings.Index(leadStr[toIndex:], "MINUTE") != -1 {
  202. if err := dt.setHourToMinute(valueStr); err != nil {
  203. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  204. }
  205. } else if secPrecIndex = strings.Index(leadStr[toIndex:], "SECOND"); secPrecIndex != -1 {
  206. secPrecIndex += toIndex
  207. strSecPrec = leadStr[secPrecIndex:]
  208. if err := dt.setHourToSecond(valueStr); err != nil {
  209. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  210. }
  211. } else {
  212. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  213. }
  214. }
  215. if err := dt.setPrecForSvr(leadStr, strLeadPrec, strSecPrec); err != nil {
  216. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  217. }
  218. return dt, nil
  219. }
  220. if leadPrecIndex = strings.Index(leadStr, "MINUTE"); leadPrecIndex != -1 {
  221. toIndex = strings.Index(leadStr, "TO")
  222. if toIndex == -1 {
  223. toIndex += leadPrecIndex
  224. strLeadPrec = leadStr[leadPrecIndex:]
  225. if err := dt.setMinute(valueStr); err != nil {
  226. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  227. }
  228. } else {
  229. strLeadPrec = leadStr[leadPrecIndex:toIndex]
  230. if secPrecIndex = strings.Index(leadStr[toIndex:], "SECOND"); secPrecIndex != -1 {
  231. strSecPrec = leadStr[secPrecIndex:]
  232. if err := dt.setMinuteToSecond(valueStr); err != nil {
  233. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  234. }
  235. } else {
  236. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  237. }
  238. }
  239. if err := dt.setPrecForSvr(leadStr, strLeadPrec, strSecPrec); err != nil {
  240. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  241. }
  242. return dt, nil
  243. }
  244. if leadPrecIndex = strings.Index(leadStr, "SECOND"); leadPrecIndex != -1 {
  245. if err := dt.setSecond(valueStr); err != nil {
  246. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  247. }
  248. leadStr = strings.TrimSpace(leadStr[leadPrecIndex:])
  249. colonIndex := strings.Index(leadStr, ",")
  250. if colonIndex != -1 {
  251. strLeadPrec = strings.TrimSpace(leadStr[:colonIndex]) + ")"
  252. strSecPrec = "(" + strings.TrimSpace(leadStr[:colonIndex+1])
  253. }
  254. if err := dt.setPrecForSvr(leadStr, strLeadPrec, strSecPrec); err != nil {
  255. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  256. }
  257. return dt, nil
  258. }
  259. return nil, ECGO_INVALID_TIME_INTERVAL.throw()
  260. }
  261. func (dt *DmIntervalDT) GetDay() int {
  262. return dt.days
  263. }
  264. func (dt *DmIntervalDT) GetHour() int {
  265. return dt.hours
  266. }
  267. func (dt *DmIntervalDT) GetMinute() int {
  268. return dt.minutes
  269. }
  270. func (dt *DmIntervalDT) GetSecond() int {
  271. return dt.seconds
  272. }
  273. func (dt *DmIntervalDT) GetMsec() int {
  274. return dt.fraction
  275. }
  276. func (dt *DmIntervalDT) GetDTType() byte {
  277. return dt._type
  278. }
  279. func (dt *DmIntervalDT) String() string {
  280. if !dt.Valid {
  281. return ""
  282. }
  283. var l, destLen int
  284. var dStr, hStr, mStr, sStr, nStr string
  285. interval := "INTERVAL "
  286. switch dt._type {
  287. case QUA_D:
  288. dStr := strconv.FormatInt(int64(float64(dt.days)), 10)
  289. if dt.negative {
  290. interval += "-"
  291. }
  292. if len(dStr) < dt.leadScale {
  293. l = len(dStr)
  294. destLen = dt.leadScale
  295. for destLen > l {
  296. dStr = "0" + dStr
  297. destLen--
  298. }
  299. }
  300. interval += "'" + dStr + "' DAY(" + strconv.FormatInt(int64(dt.leadScale), 10) + ")"
  301. case QUA_DH:
  302. dStr = strconv.FormatInt(int64(float64(dt.days)), 10)
  303. hStr = strconv.FormatInt(int64(float64(dt.hours)), 10)
  304. if dt.negative {
  305. interval += "-"
  306. }
  307. if len(dStr) < dt.leadScale {
  308. l = len(dStr)
  309. destLen = dt.leadScale
  310. for destLen > l {
  311. dStr = "0" + dStr
  312. destLen--
  313. }
  314. }
  315. if len(hStr) < 2 {
  316. hStr = "0" + hStr
  317. }
  318. interval += "'" + dStr + " " + hStr + "' DAY(" + strconv.FormatInt(int64(dt.leadScale), 10) + ") TO HOUR"
  319. case QUA_DHM:
  320. dStr = strconv.FormatInt(int64(float64(dt.days)), 10)
  321. hStr = strconv.FormatInt(int64(float64(dt.hours)), 10)
  322. mStr = strconv.FormatInt(int64(float64(dt.minutes)), 10)
  323. if dt.negative {
  324. interval += "-"
  325. }
  326. if len(dStr) < dt.leadScale {
  327. l = len(dStr)
  328. destLen = dt.leadScale
  329. for destLen > l {
  330. dStr = "0" + dStr
  331. destLen--
  332. }
  333. }
  334. if len(hStr) < 2 {
  335. hStr = "0" + hStr
  336. }
  337. if len(mStr) < 2 {
  338. mStr = "0" + mStr
  339. }
  340. interval += "'" + dStr + " " + hStr + ":" + mStr + "' DAY(" + strconv.FormatInt(int64(dt.leadScale), 10) + ") TO MINUTE"
  341. case QUA_DHMS:
  342. dStr = strconv.FormatInt(int64(float64(dt.days)), 10)
  343. hStr = strconv.FormatInt(int64(float64(dt.hours)), 10)
  344. mStr = strconv.FormatInt(int64(float64(dt.minutes)), 10)
  345. sStr = strconv.FormatInt(int64(float64(dt.seconds)), 10)
  346. nStr = dt.getMsecString()
  347. if dt.negative {
  348. interval += "-"
  349. }
  350. if len(dStr) < dt.leadScale {
  351. l = len(dStr)
  352. destLen = dt.leadScale
  353. for destLen > l {
  354. dStr = "0" + dStr
  355. destLen--
  356. }
  357. }
  358. if len(hStr) < 2 {
  359. hStr = "0" + hStr
  360. }
  361. if len(mStr) < 2 {
  362. mStr = "0" + mStr
  363. }
  364. if len(sStr) < 2 {
  365. sStr = "0" + sStr
  366. }
  367. interval += "'" + dStr + " " + hStr + ":" + mStr + ":" + sStr
  368. if nStr != "" {
  369. interval += "." + nStr
  370. }
  371. interval += "' DAY(" + strconv.FormatInt(int64(dt.leadScale), 10) + ") TO SECOND(" + strconv.FormatInt(int64(dt.secScale), 10) + ")"
  372. case QUA_H:
  373. hStr = strconv.FormatInt(int64(float64(dt.hours)), 10)
  374. if dt.negative {
  375. interval += "-"
  376. }
  377. if len(hStr) < dt.leadScale {
  378. l = len(hStr)
  379. destLen = dt.leadScale
  380. for destLen > l {
  381. hStr = "0" + hStr
  382. destLen--
  383. }
  384. }
  385. interval += "'" + hStr + "' HOUR(" + strconv.FormatInt(int64(dt.leadScale), 10) + ")"
  386. case QUA_HM:
  387. hStr = strconv.FormatInt(int64(float64(dt.hours)), 10)
  388. mStr = strconv.FormatInt(int64(float64(dt.minutes)), 10)
  389. if dt.negative {
  390. interval += "-"
  391. }
  392. if len(hStr) < dt.leadScale {
  393. l = len(hStr)
  394. destLen = dt.leadScale
  395. for destLen > l {
  396. hStr = "0" + hStr
  397. destLen--
  398. }
  399. }
  400. if len(mStr) < 2 {
  401. mStr = "0" + mStr
  402. }
  403. interval += "'" + hStr + ":" + mStr + "' HOUR(" + strconv.FormatInt(int64(dt.leadScale), 10) + ") TO MINUTE"
  404. case QUA_HMS:
  405. nStr = dt.getMsecString()
  406. hStr = strconv.FormatInt(int64(float64(dt.hours)), 10)
  407. mStr = strconv.FormatInt(int64(float64(dt.minutes)), 10)
  408. sStr = strconv.FormatInt(int64(float64(dt.seconds)), 10)
  409. if dt.negative {
  410. interval += "-"
  411. }
  412. if len(hStr) < dt.leadScale {
  413. l = len(hStr)
  414. destLen = dt.leadScale
  415. for destLen > l {
  416. hStr = "0" + hStr
  417. destLen--
  418. }
  419. }
  420. if len(mStr) < 2 {
  421. mStr = "0" + mStr
  422. }
  423. if len(sStr) < 2 {
  424. sStr = "0" + sStr
  425. }
  426. interval += "'" + hStr + ":" + mStr + ":" + sStr
  427. if nStr != "" {
  428. interval += "." + nStr
  429. }
  430. interval += "' HOUR(" + strconv.FormatInt(int64(dt.leadScale), 10) + ") TO SECOND(" + strconv.FormatInt(int64(dt.secScale), 10) + ")"
  431. case QUA_M:
  432. mStr = strconv.FormatInt(int64(float64(dt.minutes)), 10)
  433. if dt.negative {
  434. interval += "-"
  435. }
  436. if len(mStr) < dt.leadScale {
  437. l = len(mStr)
  438. destLen = dt.leadScale
  439. for destLen > l {
  440. mStr = "0" + mStr
  441. destLen--
  442. }
  443. }
  444. interval += "'" + mStr + "' MINUTE(" + strconv.FormatInt(int64(dt.leadScale), 10) + ")"
  445. case QUA_MS:
  446. nStr = dt.getMsecString()
  447. mStr = strconv.FormatInt(int64(float64(dt.minutes)), 10)
  448. sStr = strconv.FormatInt(int64(float64(dt.seconds)), 10)
  449. if dt.negative {
  450. interval += "-"
  451. }
  452. if len(mStr) < dt.leadScale {
  453. l = len(mStr)
  454. destLen = dt.leadScale
  455. for destLen > l {
  456. mStr = "0" + mStr
  457. destLen--
  458. }
  459. }
  460. if len(sStr) < 2 {
  461. sStr = "0" + sStr
  462. }
  463. interval += "'" + mStr + ":" + sStr
  464. if nStr != "" {
  465. interval += "." + nStr
  466. }
  467. interval += "' MINUTE(" + strconv.FormatInt(int64(dt.leadScale), 10) + ") TO SECOND(" + strconv.FormatInt(int64(dt.secScale), 10) + ")"
  468. case QUA_S:
  469. nStr = dt.getMsecString()
  470. sStr = strconv.FormatInt(int64(float64(dt.seconds)), 10)
  471. if dt.negative {
  472. interval += "-"
  473. }
  474. if len(sStr) < dt.leadScale {
  475. l = len(sStr)
  476. destLen = dt.leadScale
  477. for destLen > l {
  478. sStr = "0" + sStr
  479. destLen--
  480. }
  481. }
  482. interval += "'" + sStr
  483. if nStr != "" {
  484. interval += "." + nStr
  485. }
  486. interval += "' SECOND(" + strconv.FormatInt(int64(dt.leadScale), 10) + ", " + strconv.FormatInt(int64(dt.secScale), 10) + ")"
  487. }
  488. return interval
  489. }
  490. func (dest *DmIntervalDT) Scan(src interface{}) error {
  491. if dest == nil {
  492. return ECGO_STORE_IN_NIL_POINTER.throw()
  493. }
  494. switch src := src.(type) {
  495. case nil:
  496. *dest = *new(DmIntervalDT)
  497. (*dest).Valid = false
  498. return nil
  499. case *DmIntervalDT:
  500. *dest = *src
  501. return nil
  502. case string:
  503. ret, err := NewDmIntervalDTByString(src)
  504. if err != nil {
  505. return err
  506. }
  507. *dest = *ret
  508. return nil
  509. default:
  510. return UNSUPPORTED_SCAN
  511. }
  512. }
  513. func (dt DmIntervalDT) Value() (driver.Value, error) {
  514. if !dt.Valid {
  515. return nil, nil
  516. }
  517. return dt, nil
  518. }
  519. func (dt *DmIntervalDT) checkScale(leadScale int) (int, error) {
  520. switch dt._type {
  521. case QUA_D:
  522. if leadScale == -1 {
  523. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.days))), 10))
  524. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.days))), 10)) {
  525. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  526. }
  527. case QUA_DH:
  528. if leadScale == -1 {
  529. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.days))), 10))
  530. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.days))), 10)) {
  531. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  532. }
  533. if int64(math.Abs(float64((dt.hours)))) > 23 {
  534. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  535. }
  536. case QUA_DHM:
  537. if leadScale == -1 {
  538. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.days))), 10))
  539. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.days))), 10)) {
  540. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  541. }
  542. if int64(math.Abs(float64(dt.hours))) > 23 || int64(math.Abs(float64(dt.minutes))) > 59 {
  543. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  544. }
  545. case QUA_DHMS:
  546. if leadScale == -1 {
  547. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.days))), 10))
  548. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.days))), 10)) {
  549. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  550. }
  551. if int64(math.Abs(float64(dt.hours))) > 23 || int64(math.Abs(float64(dt.minutes))) > 59 ||
  552. int64(math.Abs(float64(dt.seconds))) > 59 {
  553. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  554. }
  555. case QUA_H:
  556. if leadScale == -1 {
  557. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.hours))), 10))
  558. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.hours))), 10)) {
  559. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  560. }
  561. case QUA_HM:
  562. if leadScale == -1 {
  563. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.hours))), 10))
  564. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.hours))), 10)) {
  565. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  566. }
  567. if int64(math.Abs(float64(dt.minutes))) > 59 {
  568. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  569. }
  570. case QUA_HMS:
  571. if leadScale == -1 {
  572. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.hours))), 10))
  573. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.hours))), 10)) {
  574. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  575. }
  576. if int64(math.Abs(float64(dt.minutes))) > 59 || int64(math.Abs(float64(dt.seconds))) > 59 {
  577. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  578. }
  579. case QUA_M:
  580. if leadScale == -1 {
  581. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.minutes))), 10))
  582. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.minutes))), 10)) {
  583. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  584. }
  585. case QUA_MS:
  586. if leadScale == -1 {
  587. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.minutes))), 10))
  588. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.minutes))), 10)) {
  589. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  590. }
  591. if int64(math.Abs(float64(dt.seconds))) > 59 {
  592. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  593. }
  594. case QUA_S:
  595. if leadScale == -1 {
  596. leadScale = len(strconv.FormatInt(int64(math.Abs(float64(dt.minutes))), 10))
  597. } else if leadScale < len(strconv.FormatInt(int64(math.Abs(float64(dt.minutes))), 10)) {
  598. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  599. }
  600. }
  601. if leadScale > LOADPREC_MAX {
  602. return 0, ECGO_INVALID_TIME_INTERVAL.throw()
  603. }
  604. return leadScale, nil
  605. }
  606. func (dt *DmIntervalDT) parsePrec(leadStr string) (int, error) {
  607. leftBtId := strings.Index(leadStr, "(")
  608. rightBtId := strings.Index(leadStr, ")")
  609. var prec int64 = -1
  610. if rightBtId != -1 && leftBtId != -1 && rightBtId > leftBtId+1 {
  611. strPrec := strings.TrimSpace(leadStr[leftBtId+1 : rightBtId])
  612. var err error
  613. prec, err = strconv.ParseInt(strPrec, 10, 32)
  614. if err != nil {
  615. return -1, err
  616. }
  617. }
  618. return int(prec), nil
  619. }
  620. func (dt *DmIntervalDT) setPrecForSvr(fullStr string, leadScale string, secScale string) error {
  621. prec, err := dt.parsePrec(leadScale)
  622. if err != nil {
  623. return err
  624. }
  625. prec, err = dt.checkScale(prec)
  626. if err != nil {
  627. return err
  628. }
  629. if prec < LOADPREC_DEFAULT {
  630. dt.leadScale = LOADPREC_DEFAULT
  631. } else {
  632. dt.leadScale = prec
  633. }
  634. prec, err = dt.parsePrec(secScale)
  635. if err != nil {
  636. return err
  637. }
  638. if prec >= 0 && prec < SECDPREC_MAX {
  639. dt.secScale = prec
  640. } else {
  641. dt.secScale = SECDPREC_DEFAULT
  642. }
  643. dt.scaleForSvr = (int(dt._type) << 8) + (dt.leadScale << 4) + dt.secScale
  644. return nil
  645. }
  646. func (dt *DmIntervalDT) checkSign(str string) string {
  647. if str[0] == '-' {
  648. str = strings.TrimSpace(str[1:])
  649. dt.negative = true
  650. } else if str[0] == '+' {
  651. str = strings.TrimSpace(str[1:])
  652. dt.negative = false
  653. }
  654. return str
  655. }
  656. func (dt *DmIntervalDT) setDay(value string) error {
  657. list := util.Split(value, " :.")
  658. if len(list) > 1 {
  659. return ECGO_INVALID_TIME_INTERVAL.throw()
  660. }
  661. dt._type = QUA_D
  662. i, err := strconv.ParseInt(value, 10, 32)
  663. if err != nil {
  664. return err
  665. }
  666. if i < 0 {
  667. dt.days = int(-i)
  668. dt.negative = true
  669. } else {
  670. dt.days = int(i)
  671. }
  672. return nil
  673. }
  674. func (dt *DmIntervalDT) setHour(value string) error {
  675. list := util.Split(value, " :.")
  676. if len(list) > 1 {
  677. return ECGO_INVALID_TIME_INTERVAL.throw()
  678. }
  679. dt._type = QUA_H
  680. i, err := strconv.ParseInt(value, 10, 32)
  681. if err != nil {
  682. return err
  683. }
  684. if i < 0 {
  685. dt.hours = int(-i)
  686. dt.negative = true
  687. } else {
  688. dt.hours = int(i)
  689. }
  690. return nil
  691. }
  692. func (dt *DmIntervalDT) setMinute(value string) error {
  693. list := util.Split(value, " :.")
  694. if len(list) > 1 {
  695. return ECGO_INVALID_TIME_INTERVAL.throw()
  696. }
  697. dt._type = QUA_M
  698. i, err := strconv.ParseInt(value, 10, 32)
  699. if err != nil {
  700. return err
  701. }
  702. if i < 0 {
  703. dt.minutes = int(-i)
  704. dt.negative = true
  705. } else {
  706. dt.minutes = int(i)
  707. }
  708. return nil
  709. }
  710. func (dt *DmIntervalDT) setSecond(value string) error {
  711. list := util.Split(value, " :.")
  712. if len(list) > 2 {
  713. return ECGO_INVALID_TIME_INTERVAL.throw()
  714. }
  715. dt._type = QUA_S
  716. i, err := strconv.ParseInt(list[0], 10, 32)
  717. if err != nil {
  718. return err
  719. }
  720. nano := 0
  721. if len(list) > 1 {
  722. strNano := "0" + "." + list[1]
  723. d_v, err := strconv.ParseFloat(strNano, 64)
  724. if err != nil {
  725. return err
  726. }
  727. nx := math.Pow10(dt.secScale)
  728. nano = (int)(d_v * nx)
  729. }
  730. if i < 0 {
  731. dt.seconds = int(-i)
  732. } else {
  733. dt.seconds = int(i)
  734. }
  735. if nano < 0 {
  736. dt.fraction = -nano
  737. } else {
  738. dt.fraction = nano
  739. }
  740. if i < 0 || nano < 0 {
  741. dt.negative = true
  742. }
  743. return nil
  744. }
  745. func (dt *DmIntervalDT) setHourToSecond(value string) error {
  746. list := util.Split(value, " :.")
  747. if len(list) > 4 {
  748. return ECGO_INVALID_TIME_INTERVAL.throw()
  749. }
  750. dt._type = QUA_HMS
  751. h, err := strconv.ParseInt(list[0], 10, 32)
  752. if err != nil {
  753. return err
  754. }
  755. m, err := strconv.ParseInt(list[1], 10, 32)
  756. if err != nil {
  757. return err
  758. }
  759. s, err := strconv.ParseInt(list[2], 10, 32)
  760. if err != nil {
  761. return err
  762. }
  763. nano := 0
  764. if len(list) > 3 {
  765. strNano := "0" + "." + list[3]
  766. d_v, err := strconv.ParseFloat(strNano, 64)
  767. if err != nil {
  768. return err
  769. }
  770. nx := math.Pow10(dt.secScale)
  771. nano = (int)(d_v * nx)
  772. }
  773. if h < 0 {
  774. dt.hours = int(-h)
  775. } else {
  776. dt.hours = int(h)
  777. }
  778. if m < 0 {
  779. dt.minutes = int(-m)
  780. } else {
  781. dt.minutes = int(m)
  782. }
  783. if s < 0 {
  784. dt.seconds = int(-s)
  785. } else {
  786. dt.seconds = int(s)
  787. }
  788. if nano < 0 {
  789. dt.fraction = -nano
  790. } else {
  791. dt.fraction = nano
  792. }
  793. if h < 0 || m < 0 || s < 0 || nano < 0 {
  794. dt.negative = true
  795. }
  796. return nil
  797. }
  798. func (dt *DmIntervalDT) setHourToMinute(value string) error {
  799. value = strings.TrimSpace(value)
  800. list := util.Split(value, " :.")
  801. if len(list) > 2 {
  802. return ECGO_INVALID_TIME_INTERVAL.throw()
  803. }
  804. dt._type = QUA_HM
  805. h, err := strconv.ParseInt(list[0], 10, 32)
  806. if err != nil {
  807. return err
  808. }
  809. m, err := strconv.ParseInt(list[1], 10, 32)
  810. if err != nil {
  811. return err
  812. }
  813. if h < 0 {
  814. dt.hours = int(-h)
  815. } else {
  816. dt.hours = int(h)
  817. }
  818. if m < 0 {
  819. dt.minutes = int(-m)
  820. } else {
  821. dt.minutes = int(m)
  822. }
  823. if h < 0 || m < 0 {
  824. dt.negative = true
  825. }
  826. return nil
  827. }
  828. func (dt *DmIntervalDT) setMinuteToSecond(value string) error {
  829. list := util.Split(value, " :.")
  830. if len(list) > 3 {
  831. return ECGO_INVALID_TIME_INTERVAL.throw()
  832. }
  833. dt._type = QUA_MS
  834. m, err := strconv.ParseInt(list[0], 10, 32)
  835. if err != nil {
  836. return err
  837. }
  838. s, err := strconv.ParseInt(list[1], 10, 32)
  839. if err != nil {
  840. return err
  841. }
  842. nano := 0
  843. if len(list) > 2 {
  844. strNano := "0" + "." + list[2]
  845. d_v, err := strconv.ParseFloat(strNano, 64)
  846. if err != nil {
  847. return err
  848. }
  849. nx := math.Pow10(dt.secScale)
  850. nano = (int)(d_v * nx)
  851. }
  852. if m < 0 {
  853. dt.minutes = int(-m)
  854. } else {
  855. dt.minutes = int(m)
  856. }
  857. if s < 0 {
  858. dt.seconds = int(-s)
  859. } else {
  860. dt.seconds = int(s)
  861. }
  862. if nano < 0 {
  863. dt.fraction = -nano
  864. } else {
  865. dt.fraction = nano
  866. }
  867. if m < 0 || s < 0 || nano < 0 {
  868. dt.negative = true
  869. }
  870. return nil
  871. }
  872. func (dt *DmIntervalDT) setDayToHour(value string) error {
  873. list := util.Split(value, " :.")
  874. if len(list) > 2 {
  875. return ECGO_INVALID_TIME_INTERVAL.throw()
  876. }
  877. dt._type = QUA_DH
  878. d, err := strconv.ParseInt(list[0], 10, 32)
  879. if err != nil {
  880. return err
  881. }
  882. h, err := strconv.ParseInt(list[1], 10, 32)
  883. if err != nil {
  884. return err
  885. }
  886. if d < 0 {
  887. dt.days = int(-d)
  888. } else {
  889. dt.days = int(d)
  890. }
  891. if h < 0 {
  892. dt.hours = int(-h)
  893. } else {
  894. dt.hours = int(h)
  895. }
  896. if d < 0 || h < 0 {
  897. dt.negative = true
  898. }
  899. return nil
  900. }
  901. func (dt *DmIntervalDT) setDayToMinute(value string) error {
  902. list := util.Split(value, " :.")
  903. if len(list) > 3 {
  904. return ECGO_INVALID_TIME_INTERVAL.throw()
  905. }
  906. dt._type = QUA_DHM
  907. d, err := strconv.ParseInt(list[0], 10, 32)
  908. if err != nil {
  909. return err
  910. }
  911. h, err := strconv.ParseInt(list[1], 10, 32)
  912. if err != nil {
  913. return err
  914. }
  915. m, err := strconv.ParseInt(list[2], 10, 32)
  916. if err != nil {
  917. return err
  918. }
  919. if d < 0 {
  920. dt.days = int(-d)
  921. } else {
  922. dt.days = int(d)
  923. }
  924. if h < 0 {
  925. dt.hours = int(-h)
  926. } else {
  927. dt.hours = int(h)
  928. }
  929. if m < 0 {
  930. dt.minutes = int(-m)
  931. } else {
  932. dt.minutes = int(m)
  933. }
  934. if d < 0 || h < 0 || m < 0 {
  935. dt.negative = true
  936. }
  937. return nil
  938. }
  939. func (dt *DmIntervalDT) setDayToSecond(value string) error {
  940. list := util.Split(value, " :.")
  941. if len(list) > 5 {
  942. return ECGO_INVALID_TIME_INTERVAL.throw()
  943. }
  944. dt._type = QUA_DHMS
  945. d, err := strconv.ParseInt(list[0], 10, 32)
  946. if err != nil {
  947. return err
  948. }
  949. h, err := strconv.ParseInt(list[1], 10, 32)
  950. if err != nil {
  951. return err
  952. }
  953. m, err := strconv.ParseInt(list[2], 10, 32)
  954. if err != nil {
  955. return err
  956. }
  957. s, err := strconv.ParseInt(list[3], 10, 32)
  958. if err != nil {
  959. return err
  960. }
  961. nano := 0
  962. if len(list) > 4 {
  963. strNano := "0" + "." + list[4]
  964. d_v, err := strconv.ParseFloat(strNano, 64)
  965. if err != nil {
  966. return err
  967. }
  968. nx := math.Pow10(dt.secScale)
  969. nano = (int)(d_v * nx)
  970. }
  971. if d < 0 {
  972. dt.days = int(-d)
  973. } else {
  974. dt.days = int(d)
  975. }
  976. if h < 0 {
  977. dt.hours = int(-h)
  978. } else {
  979. dt.hours = int(h)
  980. }
  981. if m < 0 {
  982. dt.minutes = int(-m)
  983. } else {
  984. dt.minutes = int(m)
  985. }
  986. if s < 0 {
  987. dt.seconds = int(-s)
  988. } else {
  989. dt.seconds = int(s)
  990. }
  991. if nano < 0 {
  992. dt.fraction = -nano
  993. } else {
  994. dt.fraction = nano
  995. }
  996. if d < 0 || h < 0 || m < 0 || s < 0 || nano < 0 {
  997. dt.negative = true
  998. }
  999. return nil
  1000. }
  1001. func (dt *DmIntervalDT) getMsecString() string {
  1002. nano := strconv.Itoa(dt.fraction)
  1003. for i := 6 - len(nano); i > 0; i-- {
  1004. nano = "0" + nano
  1005. }
  1006. if len(nano) > dt.secScale {
  1007. nano = nano[:dt.secScale]
  1008. }
  1009. return nano
  1010. }
  1011. func (dt *DmIntervalDT) encode(scale int) ([]byte, error) {
  1012. if scale == 0 {
  1013. scale = dt.scaleForSvr
  1014. }
  1015. day, hour, minute, second, f := dt.days, dt.hours, dt.minutes, dt.seconds, dt.fraction
  1016. if scale != dt.scaleForSvr {
  1017. convertDT, err := dt.convertTo(scale)
  1018. if err != nil {
  1019. return nil, err
  1020. }
  1021. day, hour, minute, second, f = convertDT.days, convertDT.hours, convertDT.minutes, convertDT.seconds, convertDT.fraction
  1022. } else {
  1023. loadPrec := (scale >> 4) & 0x0000000F
  1024. if _, err := dt.checkScale(loadPrec); err != nil {
  1025. return nil, err
  1026. }
  1027. }
  1028. bytes := make([]byte, 24)
  1029. if dt.negative {
  1030. Dm_build_1.Dm_build_17(bytes, 0, int32(-day))
  1031. Dm_build_1.Dm_build_17(bytes, 4, int32(-hour))
  1032. Dm_build_1.Dm_build_17(bytes, 8, int32(-minute))
  1033. Dm_build_1.Dm_build_17(bytes, 12, int32(-second))
  1034. Dm_build_1.Dm_build_17(bytes, 16, int32(-f))
  1035. Dm_build_1.Dm_build_17(bytes, 20, int32(scale))
  1036. } else {
  1037. Dm_build_1.Dm_build_17(bytes, 0, int32(day))
  1038. Dm_build_1.Dm_build_17(bytes, 4, int32(hour))
  1039. Dm_build_1.Dm_build_17(bytes, 8, int32(minute))
  1040. Dm_build_1.Dm_build_17(bytes, 12, int32(second))
  1041. Dm_build_1.Dm_build_17(bytes, 16, int32(f))
  1042. Dm_build_1.Dm_build_17(bytes, 20, int32(scale))
  1043. }
  1044. return bytes, nil
  1045. }
  1046. func (dt *DmIntervalDT) convertTo(scale int) (*DmIntervalDT, error) {
  1047. destType := (scale & 0x0000FF00) >> 8
  1048. leadPrec := (scale >> 4) & 0x0000000F
  1049. secScale := scale & 0x0000000F
  1050. dayIndex := 0
  1051. hourIndex := 1
  1052. minuteIndex := 2
  1053. secondIndex := 3
  1054. fractionIndex := 4
  1055. orgDT := make([]int, 5)
  1056. destDT := make([]int, 5)
  1057. switch dt._type {
  1058. case QUA_D:
  1059. orgDT[dayIndex] = dt.days
  1060. case QUA_DH:
  1061. orgDT[dayIndex] = dt.days
  1062. orgDT[hourIndex] = dt.hours
  1063. case QUA_DHM:
  1064. orgDT[dayIndex] = dt.days
  1065. orgDT[hourIndex] = dt.hours
  1066. orgDT[minuteIndex] = dt.minutes
  1067. case QUA_DHMS:
  1068. orgDT[dayIndex] = dt.days
  1069. orgDT[hourIndex] = dt.hours
  1070. orgDT[minuteIndex] = dt.minutes
  1071. orgDT[secondIndex] = dt.seconds
  1072. orgDT[fractionIndex] = dt.fraction
  1073. case QUA_H:
  1074. orgDT[dayIndex] = dt.hours / 24
  1075. orgDT[hourIndex] = dt.hours % 24
  1076. case QUA_HM:
  1077. orgDT[dayIndex] = dt.hours / 24
  1078. orgDT[hourIndex] = dt.hours % 24
  1079. orgDT[minuteIndex] = dt.minutes
  1080. case QUA_HMS:
  1081. orgDT[dayIndex] = dt.hours / 24
  1082. orgDT[hourIndex] = dt.hours % 24
  1083. orgDT[minuteIndex] = dt.minutes
  1084. orgDT[secondIndex] = dt.seconds
  1085. orgDT[fractionIndex] = dt.fraction
  1086. case QUA_M:
  1087. orgDT[dayIndex] = dt.minutes / (24 * 60)
  1088. orgDT[hourIndex] = (dt.minutes % (24 * 60)) / 60
  1089. orgDT[minuteIndex] = (dt.minutes % (24 * 60)) % 60
  1090. case QUA_MS:
  1091. orgDT[dayIndex] = dt.minutes / (24 * 60)
  1092. orgDT[hourIndex] = (dt.minutes % (24 * 60)) / 60
  1093. orgDT[minuteIndex] = (dt.minutes % (24 * 60)) % 60
  1094. orgDT[secondIndex] = dt.seconds
  1095. orgDT[fractionIndex] = dt.fraction
  1096. case QUA_S:
  1097. orgDT[dayIndex] = dt.seconds / (24 * 60 * 60)
  1098. orgDT[hourIndex] = (dt.seconds % (24 * 60 * 60)) / (60 * 60)
  1099. orgDT[minuteIndex] = ((dt.seconds % (24 * 60 * 60)) % (60 * 60)) / 60
  1100. orgDT[secondIndex] = ((dt.seconds % (24 * 60 * 60)) % (60 * 60)) % 60
  1101. orgDT[fractionIndex] = dt.fraction
  1102. }
  1103. switch byte(destType) {
  1104. case QUA_D:
  1105. destDT[dayIndex] = orgDT[dayIndex]
  1106. if orgDT[hourIndex] >= 12 {
  1107. incrementDay(QUA_D, destDT)
  1108. }
  1109. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[dayIndex]))))) {
  1110. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1111. }
  1112. case QUA_DH:
  1113. destDT[dayIndex] = orgDT[dayIndex]
  1114. destDT[hourIndex] = orgDT[hourIndex]
  1115. if orgDT[minuteIndex] >= 30 {
  1116. incrementHour(QUA_DH, destDT)
  1117. }
  1118. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[dayIndex]))))) {
  1119. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1120. }
  1121. case QUA_DHM:
  1122. destDT[dayIndex] = orgDT[dayIndex]
  1123. destDT[hourIndex] = orgDT[hourIndex]
  1124. destDT[minuteIndex] = orgDT[minuteIndex]
  1125. if orgDT[secondIndex] >= 30 {
  1126. incrementMinute(QUA_DHM, destDT)
  1127. }
  1128. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[dayIndex]))))) {
  1129. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1130. }
  1131. case QUA_DHMS:
  1132. destDT[dayIndex] = orgDT[dayIndex]
  1133. destDT[hourIndex] = orgDT[hourIndex]
  1134. destDT[minuteIndex] = orgDT[minuteIndex]
  1135. destDT[secondIndex] = orgDT[secondIndex]
  1136. destDT[fractionIndex] = orgDT[fractionIndex]
  1137. dt.convertMSecond(QUA_DHMS, destDT, secScale)
  1138. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[dayIndex]))))) {
  1139. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1140. }
  1141. case QUA_H:
  1142. destDT[hourIndex] = orgDT[dayIndex]*24 + orgDT[hourIndex]
  1143. if orgDT[minuteIndex] >= 30 {
  1144. incrementHour(QUA_H, destDT)
  1145. }
  1146. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[hourIndex]))))) {
  1147. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1148. }
  1149. case QUA_HM:
  1150. destDT[hourIndex] = orgDT[dayIndex]*24 + orgDT[hourIndex]
  1151. destDT[minuteIndex] = orgDT[minuteIndex]
  1152. if orgDT[secondIndex] >= 30 {
  1153. incrementMinute(QUA_HM, destDT)
  1154. }
  1155. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[hourIndex]))))) {
  1156. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1157. }
  1158. case QUA_HMS:
  1159. destDT[hourIndex] = orgDT[dayIndex]*24 + orgDT[hourIndex]
  1160. destDT[minuteIndex] = orgDT[minuteIndex]
  1161. destDT[secondIndex] = orgDT[secondIndex]
  1162. destDT[fractionIndex] = orgDT[fractionIndex]
  1163. dt.convertMSecond(QUA_HMS, destDT, secScale)
  1164. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[hourIndex]))))) {
  1165. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1166. }
  1167. case QUA_M:
  1168. destDT[minuteIndex] = orgDT[dayIndex]*24*60 + orgDT[hourIndex]*60 + orgDT[minuteIndex]
  1169. if orgDT[secondIndex] >= 30 {
  1170. incrementMinute(QUA_M, destDT)
  1171. }
  1172. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[minuteIndex]))))) {
  1173. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1174. }
  1175. case QUA_MS:
  1176. destDT[minuteIndex] = orgDT[dayIndex]*24*60 + orgDT[hourIndex]*60 + orgDT[minuteIndex]
  1177. destDT[secondIndex] = orgDT[secondIndex]
  1178. destDT[fractionIndex] = orgDT[fractionIndex]
  1179. dt.convertMSecond(QUA_MS, destDT, secScale)
  1180. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[minuteIndex]))))) {
  1181. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1182. }
  1183. case QUA_S:
  1184. destDT[secondIndex] = orgDT[dayIndex]*24*60*60 + orgDT[hourIndex]*60*60 + orgDT[minuteIndex]*60 + orgDT[secondIndex]
  1185. destDT[fractionIndex] = orgDT[fractionIndex]
  1186. dt.convertMSecond(QUA_S, destDT, secScale)
  1187. if leadPrec < len(strconv.Itoa(int(math.Abs(float64(destDT[secondIndex]))))) {
  1188. return nil, ECGO_INTERVAL_OVERFLOW.throw()
  1189. }
  1190. }
  1191. return &DmIntervalDT{
  1192. _type: byte(destType),
  1193. negative: dt.negative,
  1194. leadScale: (scale >> 4) & 0x0000000F,
  1195. secScale: scale & 0x0000000F,
  1196. scaleForSvr: scale,
  1197. days: destDT[dayIndex],
  1198. hours: destDT[hourIndex],
  1199. minutes: destDT[minuteIndex],
  1200. seconds: destDT[secondIndex],
  1201. fraction: destDT[fractionIndex],
  1202. Valid: true,
  1203. }, nil
  1204. }
  1205. func (dt DmIntervalDT) convertMSecond(destType byte, destDT []int, destSecScale int) {
  1206. fractionIndex := 4
  1207. orgFraction := destDT[fractionIndex]
  1208. if destSecScale == 0 || destSecScale < dt.secScale {
  1209. n := int(math.Pow(10, 6-float64(destSecScale)-1))
  1210. f := orgFraction / n / 10
  1211. if (orgFraction/n)%10 >= 5 {
  1212. f++
  1213. f = f * n * 10
  1214. if f == 1000000 {
  1215. destDT[fractionIndex] = 0
  1216. incrementSecond(destType, destDT)
  1217. return
  1218. }
  1219. }
  1220. destDT[fractionIndex] = f
  1221. }
  1222. }
  1223. func incrementDay(destType byte, dt []int) {
  1224. dayIndex := 0
  1225. dt[dayIndex]++
  1226. }
  1227. func incrementHour(destType byte, dt []int) {
  1228. hourIndex := 1
  1229. dt[hourIndex]++
  1230. if dt[hourIndex] == 24 && destType < QUA_H {
  1231. incrementDay(destType, dt)
  1232. dt[hourIndex] = 0
  1233. }
  1234. }
  1235. func incrementMinute(destType byte, dt []int) {
  1236. minuteIndex := 2
  1237. dt[minuteIndex]++
  1238. if dt[minuteIndex] == 60 && destType < QUA_M {
  1239. incrementHour(destType, dt)
  1240. dt[minuteIndex] = 0
  1241. }
  1242. }
  1243. func incrementSecond(destType byte, dt []int) {
  1244. secondIndex := 3
  1245. dt[secondIndex]++
  1246. if dt[secondIndex] == 60 && destType < QUA_S {
  1247. incrementMinute(destType, dt)
  1248. dt[secondIndex] = 0
  1249. }
  1250. }
  1251. func (dt *DmIntervalDT) checkValid() error {
  1252. if !dt.Valid {
  1253. return ECGO_IS_NULL.throw()
  1254. }
  1255. return nil
  1256. }