github.com/xiyichan/dm8@v0.0.0-20211213021639-be727be3e136/s.go (about)

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