gitee.com/quant1x/gox@v1.21.2/api/datetime.go (about)

     1  package api
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"time"
     7  )
     8  
     9  const (
    10  	YearOnly = "2006" // 日期格式仅输出"年"
    11  )
    12  
    13  var (
    14  	ErrDateFormat = errors.New("日期格式无法确定")
    15  )
    16  
    17  // ParseTime 解析时间字符串
    18  func ParseTime(timestr string) (time.Time, error) {
    19  	s := strings.TrimSpace(timestr)
    20  	switch len(s) {
    21  	case len(DateFormat):
    22  		return time.ParseInLocation(DateFormat, s, time.Local)
    23  	case len(DateFormat2):
    24  		return time.ParseInLocation(DateFormat2, s, time.Local)
    25  	case len(DateFormat3):
    26  		return time.ParseInLocation(DateFormat3, s, time.Local)
    27  	case len(TimeFormat2):
    28  		return time.ParseInLocation(TimeFormat2, s, time.Local)
    29  	case len(TimeFormat):
    30  		return time.ParseInLocation(TimeFormat, s, time.Local)
    31  	case len(Timestamp):
    32  		return time.ParseInLocation(Timestamp, s, time.Local)
    33  	default:
    34  		return time.Time{}, ErrDateFormat
    35  	}
    36  }
    37  
    38  // DifferDays 计算天数差
    39  //
    40  //	从 t1 回到 t2 需要多少天
    41  func DifferDays(t1, t2 time.Time) int {
    42  	t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, time.Local)
    43  	t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local)
    44  
    45  	return int(t1.Sub(t2).Hours() / 24)
    46  }
    47  
    48  // DateZero t 的0点0分0秒
    49  func DateZero(t time.Time) time.Time {
    50  	return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
    51  }
    52  
    53  // IsWorkday 是否工作日
    54  func IsWorkday(t time.Time) bool {
    55  	weekDay := t.Weekday()
    56  	if weekDay == time.Sunday || weekDay == time.Saturday {
    57  		return false
    58  	}
    59  	return true
    60  }
    61  
    62  // GetMonthDay 获得当前月的初始和结束日期
    63  func GetMonthDay(date ...string) (string, string) {
    64  	now := time.Now()
    65  	if len(date) > 0 {
    66  		day, err := ParseTime(date[0])
    67  		if err == nil {
    68  			now = day
    69  		}
    70  	}
    71  	currentYear, currentMonth, _ := now.Date()
    72  	currentLocation := now.Location()
    73  
    74  	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
    75  	lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
    76  	f := firstOfMonth.Unix()
    77  	l := lastOfMonth.Unix()
    78  	return time.Unix(f, 0).Format("2006-01-02") + " 00:00:00", time.Unix(l, 0).Format("2006-01-02") + " 23:59:59"
    79  }
    80  
    81  // GetWeekDay 获得当前周的初始和结束日期
    82  func GetWeekDay(date ...string) (string, string) {
    83  	now := time.Now()
    84  	if len(date) > 0 {
    85  		day, err := ParseTime(date[0])
    86  		if err == nil {
    87  			now = day
    88  		}
    89  	}
    90  	offset := int(time.Monday - now.Weekday())
    91  	//周日做特殊判断 因为time.Monday = 0
    92  	if offset > 0 {
    93  		offset = -6
    94  	}
    95  
    96  	lastoffset := int(time.Saturday - now.Weekday())
    97  	//周日做特殊判断 因为time.Monday = 0
    98  	if lastoffset == 6 {
    99  		lastoffset = -1
   100  	}
   101  
   102  	firstOfWeek := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
   103  	lastOfWeeK := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, lastoffset+1)
   104  	f := firstOfWeek.Unix()
   105  	l := lastOfWeeK.Unix()
   106  	return time.Unix(f, 0).Format("2006-01-02") + " 00:00:00", time.Unix(l, 0).Format("2006-01-02") + " 23:59:59"
   107  }
   108  
   109  // GetQuarterDay 获得当前季度的初始和结束日期
   110  func GetQuarterDay(months ...int) (string, string) {
   111  	mn := 0
   112  	if len(months) > 0 {
   113  		mn = months[0]
   114  	}
   115  	now := time.Now().AddDate(0, -mn, 0)
   116  	year := now.Format(YearOnly)
   117  	month := int(now.Month())
   118  	var firstOfQuarter string
   119  	var lastOfQuarter string
   120  	if month >= 1 && month <= 3 {
   121  		//1月1号
   122  		firstOfQuarter = year + "-01-01 00:00:00"
   123  		lastOfQuarter = year + "-03-31 23:59:59"
   124  	} else if month >= 4 && month <= 6 {
   125  		firstOfQuarter = year + "-04-01 00:00:00"
   126  		lastOfQuarter = year + "-06-30 23:59:59"
   127  	} else if month >= 7 && month <= 9 {
   128  		firstOfQuarter = year + "-07-01 00:00:00"
   129  		lastOfQuarter = year + "-09-30 23:59:59"
   130  	} else {
   131  		firstOfQuarter = year + "-10-01 00:00:00"
   132  		lastOfQuarter = year + "-12-31 23:59:59"
   133  	}
   134  	return firstOfQuarter, lastOfQuarter
   135  }
   136  
   137  // GetQuarterDayByDate 通过给定的日期 获得日期所在上一个季度的初始和结束日期
   138  //
   139  //	diffQuarters 季度偏移数, 大于0前移diffQuarters个季度, 小于0后移diffQuarters个季度, 默认为当前季度
   140  func GetQuarterDayByDate(date string, diffQuarters ...int) (firstOfQuarter, lastOfQuarter string) {
   141  	diff := 1
   142  	if len(diffQuarters) > 0 {
   143  		diff = diffQuarters[0]
   144  	}
   145  	_, firstOfQuarter, lastOfQuarter = GetQuarterByDate(date, diff)
   146  	return firstOfQuarter, lastOfQuarter
   147  }
   148  
   149  // GetQuarterByDate 通过给定的日期 获得日期所在财报的季度、初始以及结束日期
   150  //
   151  //	diffQuarters 季度偏移数, 大于0前移diffQuarters个季度, 小于0后移diffQuarters个季度, 默认为当前季度
   152  func GetQuarterByDate(date string, diffQuarters ...int) (quarter, first, last string) {
   153  	diff := 0
   154  	if len(diffQuarters) > 0 {
   155  		diff = diffQuarters[0]
   156  	}
   157  	now, _ := ParseTime(date)
   158  	now = now.AddDate(0, -3*diff, 0)
   159  	year := now.Format(YearOnly)
   160  	month := int(now.Month())
   161  	var firstOfQuarter string
   162  	var lastOfQuarter string
   163  	if month >= 1 && month <= 3 {
   164  		//1月1号
   165  		firstOfQuarter = year + "-01-01 00:00:00"
   166  		lastOfQuarter = year + "-03-31 23:59:59"
   167  		quarter = year + "Q1"
   168  	} else if month >= 4 && month <= 6 {
   169  		firstOfQuarter = year + "-04-01 00:00:00"
   170  		lastOfQuarter = year + "-06-30 23:59:59"
   171  		quarter = year + "Q2"
   172  	} else if month >= 7 && month <= 9 {
   173  		firstOfQuarter = year + "-07-01 00:00:00"
   174  		lastOfQuarter = year + "-09-30 23:59:59"
   175  		quarter = year + "Q3"
   176  	} else {
   177  		firstOfQuarter = year + "-10-01 00:00:00"
   178  		lastOfQuarter = year + "-12-31 23:59:59"
   179  		quarter = year + "Q4"
   180  	}
   181  	return quarter, firstOfQuarter, lastOfQuarter
   182  }