github.com/wanlay/gorm-dm8@v1.0.5/dmr/q.go (about)

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