golang 年级转换入学年份

* map.txt

        '高三' => '03-2016',
	'高二' => '03-2017',
	'高一' => '03-2018',
	'初三' => '02-2016',
	'初二' => '02-2017',
	'初一' => '02-2018',
	'小六' => '01-2013',

	// 五四制的初中
	'六年级'=>'02-2018',
	'七年级'=>'02-2017',
	'八年级'=>'02-2016',
	'九年级'=>'02-2015',

	'高中' => '03-0000',
	'初中' => '02-0000',
	'小学' => '01-0000',

* format.php

// 用于生成test case

<?php

function fileForEachRow($path, $handler) {
    $handle = fopen($path, "r");
    if (! $handle ) {
        return;
    }
    while (($buffer = fgets($handle, 1024) ) !== false) {
        $buffer = trim($buffer);
        call_user_func($handler, $buffer);
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

fileForEachRow("map.txt", function($line) {
    $line = trim($line);
    
    $n = strlen($line);
    if ($n === 0) {
        return;
    }
    if (substr($line, 0, 2) === "//") {
        return;
    }
    // echo $line.PHP_EOL;
    $col = explode("=>", $line);
    for ($i = 0; $i < count($col); $i++) {
        $col[$i] = trim($col[$i], ", \t'\"");
    }
    $grade = $col[0];
    $zh = $col[1];
    printf("assert.Equal(t, ConvertGradeChinese2Gradeno(\"%s\", now), \"%s\")\n", $grade, $zh);

});

* converter.go

注意golang switch case语句自动添加break

如果不需要break,必须用fallthrough关键字

 

package util

import (
	"fmt"
	"github.com/pkg/errors"
	"time"
)

type Converter struct{}

var GRADE_NAME_TO_ID map[string]string

func (this *Converter) init() {
	if len(GRADE_NAME_TO_ID) == 0 {
		// GRADE_NAME_TO_ID = getMapGradeName2ID()
		GRADE_NAME_TO_ID = GetMapGradeName2IdByTime(time.Now())
	}
}

func (this *Converter) GradeChinese2Gradeno(grade string) string {
	this.init()
	return GRADE_NAME_TO_ID[grade]
}

func ConvertGradeChinese2Gradeno(grade string, date time.Time) string {
	// 不变的3种情况
	switch grade {
	case "高中": return "03-0000"
	case "初中": return "02-0000"
	case "小学": return "01-0000"
	default:

	}
	year := date.Year()
	month := int32(date.Month())

	// 每年7月1日0点更新 下半学年(上半年)
	if month >= 7 {
		year += 1
	}
	gmy := &GradeMonthYear{}
	gmy.getByRune(grade, int32(year))
	return gmy.toString()
}


type CompareFunc func(interface{}, interface{}) int32

func indexOf(a []interface{}, e interface{}, cmp CompareFunc) int32 {
	n := int32(len(a))
	var i int32 = 0
	for ; i < n; i++ {
		if cmp(e, a[i]) == 0 {
			return i
		}
	}
	return -1
}

type GradeMonthYear struct {
	Month int32
	Year  int32
}

func (this *GradeMonthYear) toString() string {
	return fmt.Sprintf("%02d-%04d", this.Month, this.Year)
}

func (this *GradeMonthYear) getByRune(ws string, year int32) error {

	zh := []rune{'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'}
	zh_Numbers := make([]interface{}, len(zh))
	for i, v := range zh {
		zh_Numbers[i] = v
	}
	runeCmp := func(i interface{}, i2 interface{}) int32 {
		a := i.(int32)
		b := i2.(int32)
		return a - b
	}
	r := []rune(ws)

	switch r[0] {
	case '高':
		this.Month = 3
	case '初':
		this.Month = 2
	case '小':
		this.Month = 1
	case '六': fallthrough
	case '七': fallthrough
	case '八': fallthrough
	case '九':
		gradeIndex54 := indexOf(zh_Numbers, r[0], runeCmp)
		this.Year = year - int32(gradeIndex54-5)
		this.Month = 2
		return nil
	default:
		errors.New("Unexpected grade name: " + ws)
	}
	gradeIndex := indexOf(zh_Numbers, r[1], runeCmp)
	if gradeIndex >= 0 {
		this.Year = year - gradeIndex
	} else {
		// 小学 高中 初中
		if r[1] != '年' {
			this.Year = 0
		}
	}

	return nil
}

func GetMapGradeName2IdByTime(time time.Time) map[string]string {
	var s = time.Format("2006-01-02 15:04:00")

	var year, month, day, hour, minute, second int32
	fmt.Sscanf(s, "%d-%d-%d %d:%d:%d:%d", &year, &month, &day, &hour, &minute, &second)
	// 每年7月1日0点更新 下半学年(上半年)
	if month >= 7 {
		year += 1
	}
	a := []string{"高三", "高二", "高一", "初三", "初二", "初一", "小六", "六年级", "七年级", "八年级", "九年级", "高中", "初中", "小学"}
	m := map[string]string{}

	for _, e := range a {
		gmy := GradeMonthYear{}
		gmy.getByRune(e, year)
		m[e] = gmy.toString()
	}

	return m
}

* converter_test.go

package util

import (
	"github.com/magiconair/properties/assert"
	"testing"
	"time"
)

func TestConvertGradeChinese2Gradeno(t *testing.T) {
	now := time.Now()

	assert.Equal(t, ConvertGradeChinese2Gradeno("高三", now), "03-2016")
	assert.Equal(t, ConvertGradeChinese2Gradeno("高二", now), "03-2017")
	assert.Equal(t, ConvertGradeChinese2Gradeno("高一", now), "03-2018")
	assert.Equal(t, ConvertGradeChinese2Gradeno("初三", now), "02-2016")
	assert.Equal(t, ConvertGradeChinese2Gradeno("初二", now), "02-2017")
	assert.Equal(t, ConvertGradeChinese2Gradeno("初一", now), "02-2018")
	assert.Equal(t, ConvertGradeChinese2Gradeno("小六", now), "01-2013")

	assert.Equal(t, ConvertGradeChinese2Gradeno("六年级", now), "02-2018")
	assert.Equal(t, ConvertGradeChinese2Gradeno("七年级", now), "02-2017")
	assert.Equal(t, ConvertGradeChinese2Gradeno("八年级", now), "02-2016")
	assert.Equal(t, ConvertGradeChinese2Gradeno("九年级", now), "02-2015")

	assert.Equal(t, ConvertGradeChinese2Gradeno("高中", now), "03-0000")
	assert.Equal(t, ConvertGradeChinese2Gradeno("初中", now), "02-0000")
	assert.Equal(t, ConvertGradeChinese2Gradeno("小学", now), "01-0000")
}

func TestGetMapGradeName2IdByTime(t *testing.T) {
	now := time.Now()
	gradeMap := GetMapGradeName2IdByTime(now)
	for k, v := range gradeMap {
		t.Logf("%s => %s\n", k, v)
	}

}

func TestGetMapGradeName2IdByTime_1(t *testing.T) {
	date := time.Date(2019, 7, 1, 0, 0, 0, 0, time.Local)
	gradeMap := GetMapGradeName2IdByTime(date)
	for k, v := range gradeMap {
		t.Logf("%s => %s\n", k, v)
	}
}

func TestGetMapGradeName2IdByTime_2(t *testing.T) {
	date := time.Date(2020, 6, 30, 23, 0, 0, 0, time.Local)
	gradeMap := GetMapGradeName2IdByTime(date)
	for k, v := range gradeMap {
		t.Logf("%s => %s\n", k, v)
	}
}

func TestGetMapGradeName2IdByTime_3(t *testing.T) {
	date := time.Date(2020, 7, 1, 0, 0, 0, 0, time.Local)
	gradeMap := GetMapGradeName2IdByTime(date)
	for k, v := range gradeMap {
		t.Logf("%s => %s\n", k, v)
	}
}

*  test:

go test -v

=== RUN   TestConvertGradeChinese2Gradeno
--- PASS: TestConvertGradeChinese2Gradeno (0.00s)
=== RUN   TestGetMapGradeName2IdByTime
--- PASS: TestGetMapGradeName2IdByTime (0.00s)
    converter_test.go:34: 高三 => 03-2016
    converter_test.go:34: 初一 => 02-2018
    converter_test.go:34: 八年级 => 02-2016
    converter_test.go:34: 九年级 => 02-2015
    converter_test.go:34: 小学 => 01-0000
    converter_test.go:34: 初三 => 02-2016
    converter_test.go:34: 高一 => 03-2018
    converter_test.go:34: 初二 => 02-2017
    converter_test.go:34: 小六 => 01-2013
    converter_test.go:34: 七年级 => 02-2017
    converter_test.go:34: 高二 => 03-2017
    converter_test.go:34: 六年级 => 02-2018
    converter_test.go:34: 高中 => 03-0000
    converter_test.go:34: 初中 => 02-0000
=== RUN   TestGetMapGradeName2IdByTime_1
--- PASS: TestGetMapGradeName2IdByTime_1 (0.00s)
    converter_test.go:43: 初一 => 02-2019
    converter_test.go:43: 高二 => 03-2018
    converter_test.go:43: 小六 => 01-2014
    converter_test.go:43: 六年级 => 02-2019
    converter_test.go:43: 八年级 => 02-2017
    converter_test.go:43: 九年级 => 02-2016
    converter_test.go:43: 高中 => 03-0000
    converter_test.go:43: 高三 => 03-2017
    converter_test.go:43: 七年级 => 02-2018
    converter_test.go:43: 初中 => 02-0000
    converter_test.go:43: 初三 => 02-2017
    converter_test.go:43: 初二 => 02-2018
    converter_test.go:43: 小学 => 01-0000
    converter_test.go:43: 高一 => 03-2019
=== RUN   TestGetMapGradeName2IdByTime_2
--- PASS: TestGetMapGradeName2IdByTime_2 (0.00s)
    converter_test.go:51: 八年级 => 02-2017
    converter_test.go:51: 初一 => 02-2019
    converter_test.go:51: 六年级 => 02-2019
    converter_test.go:51: 九年级 => 02-2016
    converter_test.go:51: 高三 => 03-2017
    converter_test.go:51: 初二 => 02-2018
    converter_test.go:51: 小六 => 01-2014
    converter_test.go:51: 七年级 => 02-2018
    converter_test.go:51: 高中 => 03-0000
    converter_test.go:51: 小学 => 01-0000
    converter_test.go:51: 高一 => 03-2019
    converter_test.go:51: 初三 => 02-2017
    converter_test.go:51: 高二 => 03-2018
    converter_test.go:51: 初中 => 02-0000
=== RUN   TestGetMapGradeName2IdByTime_3
--- PASS: TestGetMapGradeName2IdByTime_3 (0.00s)
    converter_test.go:59: 高一 => 03-2020
    converter_test.go:59: 九年级 => 02-2017
    converter_test.go:59: 小学 => 01-0000
    converter_test.go:59: 初一 => 02-2020
    converter_test.go:59: 八年级 => 02-2018
    converter_test.go:59: 高中 => 03-0000
    converter_test.go:59: 初中 => 02-0000
    converter_test.go:59: 高二 => 03-2019
    converter_test.go:59: 初三 => 02-2018
    converter_test.go:59: 小六 => 01-2015
    converter_test.go:59: 六年级 => 02-2020
    converter_test.go:59: 七年级 => 02-2019
    converter_test.go:59: 高三 => 03-2018
    converter_test.go:59: 初二 => 02-2019
PASS

Process finished with exit code 0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值