123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- package utils
- import (
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- func ParseDateAndWeek(dateText string) (string, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(strings.Split(dateText, " ")[0]))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
-
- year, week := reportDate.ISOWeek()
-
- shortYear := year % 100
- targetWeek := fmt.Sprintf("%02d年第%d周", shortYear, week)
- return targetWeek, nil
- }
- func ParseDateAndMonth(dateText string) ([]string, error) {
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(strings.Split(dateText, " ")[0]))
- if err != nil {
- return nil, fmt.Errorf("failed to parse report date: %v", err)
- }
- months := make([]string, 3)
- monthMap := map[string]string{
- "01": "1月F",
- "02": "2月G",
- "03": "3月H",
- "04": "4月J",
- "05": "5月K",
- "06": "6月M",
- "07": "7月N",
- "08": "8月Q",
- "09": "9月X",
- "10": "10月X",
- "11": "11月X",
- "12": "12月Z",
- }
- for i := 0; i < 3; i++ {
- month := reportDate.AddDate(0, i, 0).Format("01")
- months[i] = monthMap[month]
- }
- return months, nil
- }
- func ParseDateAndMonthColzaOil(dateText string) ([]string, error) {
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(strings.Split(dateText, " ")[0]))
- if err != nil {
- return nil, fmt.Errorf("failed to parse report date: %v", err)
- }
- months := make([]string, 3)
- monthMap := map[string]string{
- "01": "1月F",
- "02": "2月H",
- "03": "3月H",
- "04": "4月K",
- "05": "5月K",
- "06": "6月N",
- "07": "7月N",
- "08": "8月X",
- "09": "9月X",
- "10": "10月X",
- "11": "11月X",
- "12": "12月F",
- }
- for i := 0; i < 3; i++ {
- month := reportDate.AddDate(0, i, 0).Format("01")
- months[i] = monthMap[month]
- }
- return months, nil
- }
- func GetCurrentTime() string {
- return time.Now().Format("2006-01-02 15:04:05")
- }
- func ConvertTimeFormat(dateText string) (string, error) {
-
- reportDate, err := time.Parse("2006-01-02 15:04", strings.TrimSpace(dateText))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
- return reportDate.Format("2006-01-02"), nil
- }
- func GetNextThreeMonthsNoYear(dateText string) ([]string, error) {
-
- date, err := time.Parse("2006-01-02", dateText)
- if err != nil {
- return nil, fmt.Errorf("日期解析错误: %v", err)
- }
-
- var result []string
-
- for i := 0; i < 3; i++ {
- month := int(date.Month())
-
- result = append(result, fmt.Sprintf("%d月", month))
-
- date = date.AddDate(0, 1, 0)
- }
- return result, nil
- }
- func GetNextThreeMonthsLastDay(dateText string) ([]string, error) {
-
- date, err := time.Parse("2006-01-02", dateText)
- if err != nil {
- return nil, fmt.Errorf("日期解析错误: %v", err)
- }
-
- var result []string
-
- for i := 0; i < 3; i++ {
-
- nextMonth := date.AddDate(0, 1, 0)
-
- lastDay := nextMonth.AddDate(0, 0, -nextMonth.Day())
-
- result = append(result, lastDay.Format("2006-01-02"))
-
- date = date.AddDate(0, 1, 0)
- }
- return result, nil
- }
- func GetElementInSlice(dateTexts []string, month string) (string, error) {
- for _, dateText := range dateTexts {
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
- if strings.HasSuffix(reportDate.Format("2006-01"), month) {
- return dateText, nil
- }
- }
- return "", fmt.Errorf("未找到匹配的月份")
- }
- func StringToTime(dateText string) (time.Time, error) {
-
- reportDate, err := time.Parse("2006-01-02 15:04:05", strings.TrimSpace(dateText))
- if err != nil {
- return time.Time{}, fmt.Errorf("failed to parse report date: %v", err)
- }
- return reportDate, nil
- }
- func StringToTimeZero(dateText string) (time.Time, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return time.Time{}, fmt.Errorf("failed to parse report date: %v", err)
- }
- return reportDate, nil
- }
- func GetWeekdaysInSameWeek(dateStr string) ([]string, error) {
-
- t, err := time.Parse("2006-01-02", dateStr)
- if err != nil {
- return nil, err
- }
-
- weekday := t.Weekday()
-
- monday := t.AddDate(0, 0, -int(weekday)+1)
-
- var weekdays []string
- for i := 0; i < 5; i++ {
- day := monday.AddDate(0, 0, i)
- weekdays = append(weekdays, fmt.Sprintf("%d月%d日", day.Month(), day.Day()))
- }
- return weekdays, nil
- }
- func ConvertToDate(dateText string) (string, error) {
-
- currentYear := time.Now().Year()
-
- parts := strings.Split(dateText, "月")
- if len(parts) != 2 {
- return "", fmt.Errorf("日期格式错误")
- }
-
- month, err := strconv.Atoi(parts[0])
- if err != nil {
- return "", fmt.Errorf("月份解析错误: %v", err)
- }
- day, err := strconv.Atoi(strings.TrimSuffix(parts[1], "日"))
- if err != nil {
- return "", fmt.Errorf("日期解析错误: %v", err)
- }
-
- date := time.Date(currentYear, time.Month(month), day, 0, 0, 0, 0, time.Local)
-
- return date.Format("2006-01-02"), nil
- }
- func ConvertTimeFormatToYearMonthDay(dateText string) (string, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
-
- shortYear := reportDate.Year() % 100
- return fmt.Sprintf("%02d年%d月%d日", shortYear, reportDate.Month(), reportDate.Day()), nil
- }
- func GetCurrentYearAndLastYear(dateText string) ([]string, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return nil, fmt.Errorf("failed to parse report date: %v", err)
- }
- years := make([]string, 2)
- year := reportDate.Year()
-
- years[0] = fmt.Sprintf("%d年", year)
-
- years[1] = fmt.Sprintf("%d年", year-1)
- return years, nil
- }
- func ConvertTimeFormatToYearMonth(dateText string) ([]string, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return nil, fmt.Errorf("failed to parse report date: %v", err)
- }
- months := make([]string, 3)
- for i := 0; i < 3; i++ {
- month := reportDate.AddDate(0, i, 0).Format("2006年1月")
- months[i] = month
- }
- return months, nil
- }
- func GetYearMonthNoYear(dateText string) ([]string, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return nil, fmt.Errorf("failed to parse report date: %v", err)
- }
- months := make([]string, 3)
- for i := 0; i < 3; i++ {
- month := reportDate.AddDate(0, i, 0).Format("06年1月")
- months[i] = month
- }
- return months, nil
- }
- func GetCurrentYearAndNextYear(dateText string) ([]string, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return nil, fmt.Errorf("failed to parse report date: %v", err)
- }
- years := make([]string, 2)
- year := reportDate.Year()
-
- years[0] = fmt.Sprintf("%d/%02d年度", year-1, year%100)
-
- years[1] = fmt.Sprintf("%d/%02d年度", year, (year+1)%100)
- return years, nil
- }
- func IsSameMonth(dateText1, dateText2 string) (bool, error) {
-
- date1, err := time.Parse("2006-01-02", strings.TrimSpace(dateText1))
- if err != nil {
- return false, fmt.Errorf("failed to parse date1: %v", err)
- }
- date2, err := time.Parse("2006-01", strings.TrimSpace(dateText2))
- if err != nil {
- return false, fmt.Errorf("failed to parse date2: %v", err)
- }
- return date1.Year() == date2.Year() && date1.Month() == date2.Month(), nil
- }
- func GetLastDayOfMonth(dateText string) (string, error) {
-
- date, err := time.Parse("2006-01", strings.TrimSpace(dateText))
- if err != nil {
- return "", fmt.Errorf("failed to parse date: %v", err)
- }
-
- nextMonth := date.AddDate(0, 1, 0)
-
- lastDay := nextMonth.AddDate(0, 0, -1)
- return lastDay.Format("2006-01-02"), nil
- }
- func ConvertMonthToNumber(dateText string) (string, error) {
-
- trimmed := strings.TrimSuffix(strings.TrimSpace(dateText), "月")
-
- month, err := strconv.Atoi(trimmed)
- if err != nil {
- return "", fmt.Errorf("failed to parse month: %v", err)
- }
- return fmt.Sprintf("%02d", month), nil
- }
- func ConvertMonthToNumber1(dateText string) (string, error) {
-
- month, err := strconv.Atoi(dateText)
- if err != nil {
- return "", fmt.Errorf("failed to parse month: %v", err)
- }
- return fmt.Sprintf("%02d", month), nil
- }
- func GetNextThreeMonths(dateText string) ([]string, error) {
-
- date, err := time.Parse("2006-01-02", dateText)
- if err != nil {
- return nil, fmt.Errorf("日期解析错误: %v", err)
- }
-
- var result []string
-
- for i := 0; i < 3; i++ {
- year := date.Year() % 100
- month := int(date.Month())
-
- result = append(result, fmt.Sprintf("%d年%d月", year, month))
-
- date = date.AddDate(0, 1, 0)
- }
- return result, nil
- }
- func IsCurrentYear(dateText string) (bool, error) {
-
- trimmed := strings.TrimSuffix(strings.TrimSpace(dateText), "年度")
-
- parts := strings.Split(trimmed, "/")
- if len(parts) != 2 {
- return false, fmt.Errorf("invalid date format: %s", dateText)
- }
-
- startYear, err := strconv.Atoi(parts[0])
- if err != nil {
- return false, fmt.Errorf("failed to parse start year: %v", err)
- }
-
- currentYear := time.Now().Year()
-
- if currentYear == startYear+1 {
- return true, nil
- }
- return false, nil
- }
- func GetNextYearLastDay(dateText string) (string, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
-
- nextYear := reportDate.Year() + 1
-
- lastDay := time.Date(nextYear, reportDate.Month()+1, 0, 0, 0, 0, 0, reportDate.Location())
- return lastDay.Format("2006-01-02"), nil
- }
- func GetYearMonth(dateText string) (string, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
- return reportDate.Format("2006-01"), nil
- }
- func GetCurrentMonth(dateText string) (string, error) {
-
- reportDate, err := time.Parse("2006-01-02", strings.TrimSpace(dateText))
- if err != nil {
- return "", fmt.Errorf("failed to parse report date: %v", err)
- }
-
- month := reportDate.Month()
- return fmt.Sprintf("%d月", month), nil
- }
|