Created
March 5, 2017 12:35
-
-
Save xiaokangwang/762301be0d858829423cb3530f493df0 to your computer and use it in GitHub Desktop.
Convert Calander formart, from CUEB's version to a Google Calander importable formart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"fmt" | |
"os" | |
"regexp" | |
"strconv" | |
"strings" | |
"time" | |
) | |
import "github.com/davecgh/go-spew/spew" | |
func main() { | |
var sch Schtool | |
f, _ := os.Open("in.csv") | |
csvr := csv.NewReader(f) | |
r, _ := csvr.ReadAll() | |
sch.LoadDem(r) | |
sch.SetPtm(true, "20-Feb-2017", 17) | |
sch.Analy() | |
o := sch.GenerateOut() | |
fo, _ := os.Create("out.csv") | |
csvw := csv.NewWriter(fo) | |
csvw.WriteAll(o) | |
fo.Close() | |
} | |
type Schtool struct { | |
class [][]string | |
reverseWeek bool | |
startTime string | |
classA [][]class | |
maxweek int | |
} | |
type class struct { | |
name, teacherName string | |
stweek int | |
enweek int | |
even, odd, stub bool | |
location string | |
} | |
func (st *Schtool) LoadDem(class [][]string) { | |
st.class = class | |
} | |
func (st *Schtool) SetPtm(reverseWeek bool, startTime string, maxweek int) { | |
st.reverseWeek = reverseWeek | |
st.startTime = startTime | |
st.maxweek = maxweek | |
} | |
func (st *Schtool) Analy() { | |
resu := make([][]class, 0, 7) | |
for _, item := range st.class { | |
resur := make([]class, 0, 7) | |
for _, itemx := range item { | |
classs := st.genClassFromDescrib(itemx) | |
resur = append(resur, classs) | |
} | |
resu = append(resu, resur) | |
} | |
st.classA = resu | |
} | |
func (st *Schtool) GenerateOut() [][]string { | |
headraw := strings.Split("Subject,Start Date,Start Time,End Date,End Time,Private,All Day Event,Location", ",") | |
rescand := make([][]string, 0) | |
rescand = append(rescand, headraw) | |
//weeks loop | |
var currentWeek = 1 | |
var baset, _ = time.ParseInLocation("2-Jan-2006", st.startTime, time.FixedZone("CST", int((time.Hour*8).Seconds()))) | |
for currentWeek <= st.maxweek { | |
var even = currentWeek%2 == 0 | |
var odd = currentWeek%2 == 1 | |
for index, item := range st.classA { | |
var baset2 = baset.Add(time.Hour * time.Duration(24*int64(index))) | |
for index2, item2 := range item { | |
if (even && item2.even) || (odd && item2.odd) { | |
nxr := make([]string, 0) | |
st1 := fmt.Sprintf("%v|%v", item2.name, item2.teacherName) | |
nxr = append(nxr, st1) | |
StT, stTE := getTimeWithNum(index2, baset2) | |
a, b := genPairFromTime(StT) | |
nxr = append(nxr, a, b) | |
c, d := genPairFromTime(stTE) | |
nxr = append(nxr, c, d) | |
nxr = append(nxr, "False", "False") | |
nxr = append(nxr, item2.location) | |
rescand = append(rescand, nxr) | |
//spew.Dump(nxr) | |
} | |
} | |
} | |
currentWeek++ | |
baset = baset.Add(24 * 7 * time.Hour) | |
} | |
return rescand | |
} | |
func genPairFromTime(t time.Time) (string, string) { | |
a := t.Format("1/2/2006") | |
b := t.Format("3:04 PM") | |
return a, b | |
} | |
func getTimeWithNum(seq int, t time.Time) (time.Time, time.Time) { | |
var thisTS, thisTE time.Time | |
switch seq { | |
case 0: | |
thisTS = t.Add(time.Hour * 8) | |
thisTE = t.Add(time.Hour * 9).Add(time.Minute * 50) | |
case 1: | |
thisTS = t.Add(time.Hour * 10).Add(time.Minute * 10) | |
thisTE = t.Add(time.Hour * 12) | |
case 2: | |
thisTS = t.Add(time.Hour * 13).Add(time.Minute * 30) | |
thisTE = t.Add(time.Hour * 15).Add(time.Minute * 20) | |
case 3: | |
thisTS = t.Add(time.Hour * 15).Add(time.Minute * 40) | |
thisTE = t.Add(time.Hour * 17).Add(time.Minute * 30) | |
} | |
return thisTS, thisTE | |
} | |
func (st *Schtool) genClassFromDescrib(describ string) class { | |
if len(describ) <= 5 { | |
var cuc class | |
cuc.stub = true | |
return cuc | |
} | |
cur := strings.Split(describ, "\n") | |
spew.Dump(cur) | |
cur = cur[1:len(cur)] | |
//spew.Dump(cur) | |
var cuc class | |
cuc.name = cur[0] | |
actualname := strings.Split(cur[1], "(")[0] | |
cuc.teacherName = actualname | |
cuc.location = cur[3] | |
//weeks | |
var re = regexp.MustCompile(`((?:[0-9])+)-((?:[0-9])+)(?:\[|\()([单双])?周`) | |
result := re.FindStringSubmatch(cur[2]) | |
cuc.stweek, _ = strconv.Atoi(result[1]) | |
cuc.enweek, _ = strconv.Atoi(result[2]) | |
switch result[3] { | |
case "单": | |
cuc.odd = true | |
case "双": | |
cuc.even = true | |
default: | |
cuc.odd = true | |
cuc.even = true | |
} | |
spew.Dump(cuc) | |
return cuc | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toCalc(input) { | |
//tranpose array | |
var TranspInput = input[0].map(function(col, i) { | |
return input.map(function(row) { | |
return row[i] | |
}) | |
}); | |
return TranspInput; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment