github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/tests/dailytest/rand.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package dailytest
    15  
    16  import (
    17  	"fmt"
    18  	"math/rand"
    19  	"time"
    20  )
    21  
    22  const (
    23  	alphabet       = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    24  	yearFormat     = "2006"
    25  	dateFormat     = "2006-01-02"
    26  	timeFormat     = "15:04:05"
    27  	dateTimeFormat = "2006-01-02 15:04:05"
    28  )
    29  
    30  func init() {
    31  	rand.Seed(time.Now().UnixNano())
    32  }
    33  
    34  func randInt(min int, max int) int {
    35  	return min + rand.Intn(max-min+1)
    36  }
    37  
    38  func randInt64(min int64, max int64) int64 {
    39  	return min + rand.Int63n(max-min+1)
    40  }
    41  
    42  func randString(n int) string {
    43  	bytes := make([]byte, n)
    44  	for i := range bytes {
    45  		bytes[i] = alphabet[randInt(0, len(alphabet)-1)]
    46  	}
    47  	return string(bytes)
    48  }
    49  
    50  func randDate(min string, max string) string {
    51  	if len(min) == 0 {
    52  		year := time.Now().Year()
    53  		month := randInt(1, 12)
    54  		day := randInt(1, 28)
    55  		return fmt.Sprintf("%04d-%02d-%02d", year, month, day)
    56  	}
    57  
    58  	minTime, _ := time.Parse(dateFormat, min)
    59  	if len(max) == 0 {
    60  		t := minTime.Add(time.Duration(randInt(0, 365)) * 24 * time.Hour)
    61  		return fmt.Sprintf("%04d-%02d-%02d", t.Year(), t.Month(), t.Day())
    62  	}
    63  
    64  	maxTime, _ := time.Parse(dateFormat, max)
    65  	days := int(maxTime.Sub(minTime).Hours() / 24)
    66  	t := minTime.Add(time.Duration(randInt(0, days)) * 24 * time.Hour)
    67  	return fmt.Sprintf("%04d-%02d-%02d", t.Year(), t.Month(), t.Day())
    68  }
    69  
    70  func randTime(min string, max string) string {
    71  	if len(min) == 0 || len(max) == 0 {
    72  		hour := randInt(0, 23)
    73  		min := randInt(0, 59)
    74  		sec := randInt(0, 59)
    75  		return fmt.Sprintf("%02d:%02d:%02d", hour, min, sec)
    76  	}
    77  
    78  	minTime, _ := time.Parse(timeFormat, min)
    79  	maxTime, _ := time.Parse(timeFormat, max)
    80  	seconds := int(maxTime.Sub(minTime).Seconds())
    81  	t := minTime.Add(time.Duration(randInt(0, seconds)) * time.Second)
    82  	return fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
    83  }
    84  
    85  func randTimestamp(min string, max string) string {
    86  	if len(min) == 0 {
    87  		year := time.Now().Year()
    88  		month := randInt(1, 12)
    89  		day := randInt(1, 28)
    90  		hour := randInt(0, 23)
    91  		min := randInt(0, 59)
    92  		sec := randInt(0, 59)
    93  		return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec)
    94  	}
    95  
    96  	minTime, _ := time.Parse(dateTimeFormat, min)
    97  	if len(max) == 0 {
    98  		t := minTime.Add(time.Duration(randInt(0, 365)) * 24 * time.Hour)
    99  		return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
   100  	}
   101  
   102  	maxTime, _ := time.Parse(dateTimeFormat, max)
   103  	seconds := int(maxTime.Sub(minTime).Seconds())
   104  	t := minTime.Add(time.Duration(randInt(0, seconds)) * time.Second)
   105  	return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
   106  }
   107  
   108  func randYear(min string, max string) string {
   109  	if len(min) == 0 || len(max) == 0 {
   110  		return fmt.Sprintf("%04d", time.Now().Year()-randInt(0, 10))
   111  	}
   112  
   113  	minTime, _ := time.Parse(yearFormat, min)
   114  	maxTime, _ := time.Parse(yearFormat, max)
   115  	seconds := int(maxTime.Sub(minTime).Seconds())
   116  	t := minTime.Add(time.Duration(randInt(0, seconds)) * time.Second)
   117  	return fmt.Sprintf("%04d", t.Year())
   118  }