gitee.com/curryzheng/dm@v0.0.1/r.go (about)

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