Example #1
0
func (this *TimeFrame) parsePreviousNUnits(n int, units string) bool {
	switch units {
	case "minutes":
		this.Start = now.BeginningOfMinute().Add(time.Duration(-n) * time.Minute)
		this.End = now.BeginningOfMinute()
	case "hours":
		this.Start = now.BeginningOfHour().Add(time.Duration(-n) * time.Hour)
		this.End = now.BeginningOfHour()
	case "days":
		this.Start = now.BeginningOfDay().AddDate(0, 0, -n)
		this.End = now.BeginningOfDay()
	case "weeks":
		this.Start = now.BeginningOfWeek().AddDate(0, 0, -n*7)
		this.End = now.BeginningOfWeek()
	case "months":
		this.Start = now.BeginningOfMonth().AddDate(0, -n, 0)
		this.End = now.BeginningOfMonth()
	case "years":
		this.Start = now.BeginningOfYear().AddDate(-n, 0, 0)
		this.End = now.BeginningOfYear()
	default:
		return false
	}
	return true
}
Example #2
0
func (this *TimeFrame) parseRelUnits(rel string, units string) bool {
	if rel != "this" {
		return false
	}

	switch units {
	case "minute":
		this.Start = now.BeginningOfMinute()
	case "hour":
		this.Start = now.BeginningOfHour()
	case "day":
		this.Start = now.BeginningOfDay()
	case "week":
		this.Start = now.BeginningOfWeek()
	case "month":
		this.Start = now.BeginningOfMonth()
	case "year":
		this.Start = now.BeginningOfYear()
	default:
		return false
	}

	this.End = time.Now()
	return true
}
Example #3
0
func getPropperStartDate() time.Time {
	now.FirstDayMonday = true
	result := now.BeginningOfWeek()
	current := time.Now()
	if current.Weekday() == time.Saturday || current.Weekday() == time.Sunday {
		result = result.Add(7 * oneDay)
	}
	return result
}
Example #4
0
func GetDateRef(pattern string) (time.Time, error) {
	var dateRef time.Time
	now.FirstDayMonday = true
	switch pattern {
	case "BeginningOfMinute":
		return now.BeginningOfMinute(), nil
	case "BeginningOfHour":
		return now.BeginningOfHour(), nil
	case "BeginningOfDay":
		return now.BeginningOfDay(), nil
	case "BeginningOfWeek":
		return now.BeginningOfWeek(), nil
	case "BeginningOfMonth":
		return now.BeginningOfMonth(), nil
	case "BeginningOfQuarter":
		return now.BeginningOfQuarter(), nil
	case "BeginningOfYear":
		return now.BeginningOfYear(), nil
	}
	return dateRef, fmt.Errorf("Invalid pattern:%s", pattern)
}
Example #5
0
func getTasksHandler(r *http.Request, w http.ResponseWriter, user *models.Account) {

	/*
		tasklist := user.Tasks

		week := len(tasklist.Completed) / 7
		if week > 0 && len(tasklist.Completed)%7 == 0 &&
			tasklist.Last.After(now.BeginningOfWeek()) {
			week -= 1
		}
		list := make([]models.Task, 7)
		if week*7+7 <= len(models.Tasks)+1 {
			copy(list, models.Tasks[week*7:week*7+7])
		}
		for i, _ := range list {
			list[i].Status = tasklist.TaskStatus(list[i].Id)
			if list[i].Type == models.TaskGame && list[i].Status == "FINISH" {
				rec := &models.Record{Uid: user.Id}
				rec.FindByTask(list[i].Id)
				if rec.Game != nil {
					list[i].Desc = fmt.Sprintf("你在%s游戏中得了%d分",
						rec.Game.Name, rec.Game.Score)
				}
			}
		}
	*/
	count, _ := user.TaskRecordCount("", models.StatusFinish)
	week := count / 7

	var target, actual int

	last, _ := user.LastTaskRecord()
	// all weekly tasks are completed
	if week > 0 && count%7 == 0 && last.AuthTime.After(now.BeginningOfWeek()) {
		week -= 1
	}
	//log.Println("week", week)
	tasks := make([]models.Task, 7)
	if week*7+7 <= len(models.Tasks) {
		copy(tasks, models.Tasks[week*7:week*7+7])
	}

	for i, task := range tasks {
		tasks[i].Status = models.StatusNormal
		record := &models.Record{Uid: user.Id}

		if find, _ := record.FindByTask(task.Id); find {
			tasks[i].Status = record.Status
		}
		if task.Type == models.TaskGame && task.Status == models.StatusFinish &&
			record.Game != nil {
			tasks[i].Result = fmt.Sprintf("你在%s游戏中得了%d分",
				record.Game.Name, record.Game.Score)
		}
		if task.Type == models.TaskRunning {
			target += task.Distance
			if tasks[i].Status == models.StatusFinish && record.Sport != nil {
				actual += record.Sport.Distance
			}
		}
	}
	//log.Println(tasks)
	//random := rand.New(rand.NewSource(time.Now().Unix()))
	respData := map[string]interface{}{
		"week_id":              week + 1,
		"task_list":            tasks,
		"task_target_distance": target,
		"task_actual_distance": actual,
		//"week_desc": tips[random.Int()%len(tips)],
	}

	writeResponse(r.RequestURI, w, respData, nil)
}
Example #6
0
func updateSubstitutions() {
	currentDate := now.BeginningOfWeek()
	oneDay := time.Date(2015, 11, 30, 0, 0, 0, 0, time.UTC).Sub(time.Date(2015, 11, 29, 0, 0, 0, 0, time.UTC))

	//parsing each day od substitutions in next 10 days
	for i := 0; i < 10; i++ {
		data := getSubstitutionsForDate(currentDate)

		if data.DateStr != "" {
			db, err := sql.Open("mysql", sqlString)
			check(err)
			_, err = db.Exec("delete from substitutions where date='" + data.DateStr + "';")
			check(err)

			//parsing normal substitutions
			for _, substutution := range data.Substitutions {
				for _, substututionLesson := range substutution.SubstitutionLessons {
					stmtIns, err := db.Prepare("insert into substitutions(class, teacher, absent_teacher, subject, classroom, lesson, note, date) values (?, ?, ?, ?, ?, ?, ?, ?);")
					check(err)
					defer stmtIns.Close()

					_, err = stmtIns.Exec(
						parseSubstitutionsClass(substututionLesson.Class),
						substTeacherToSchTeacher(substututionLesson.Teacher),
						substTeacherToSchTeacher(substutution.AbsentTeacher),
						substututionLesson.Subject,
						substututionLesson.Classroom,
						strconv.Itoa(substututionLesson.Lesson()),
						substututionLesson.Note,
						data.DateStr,
					)
					check(err)
				}
			}

			//parsing subject exchange
			for _, exchange := range data.SubjectExchanges {
				stmtIns, err := db.Prepare("insert into substitutions(class, teacher, subject, classroom, lesson, note, date) values (?, ?, ?, ?, ?, ?, ?);")
				check(err)
				defer stmtIns.Close()
				_, err = stmtIns.Exec(
					parseSubstitutionsClass(exchange.Class),
					substTeacherToSchTeacher(exchange.Teacher),
					exchange.Subject,
					exchange.Classroom,
					strconv.Itoa(exchange.Lesson()),
					exchange.Note,
					data.DateStr,
				)
				check(err)

			}

			//parsing lesson exchange
			for _, exchange := range data.LessonExchanges {
				stmtIns, err := db.Prepare("insert into substitutions(class, teacher, absent_teacher, subject, classroom, lesson, note, date) values (?, ?, ?, ?, ?, ?, ?, ?);")
				check(err)
				defer stmtIns.Close()
				_, err = stmtIns.Exec(
					parseSubstitutionsClass(exchange.Class),
					substTeacherToSchTeacher(exchange.Teachers()[1]),
					substTeacherToSchTeacher(exchange.Teachers()[0]),
					exchange.Subject(),
					exchange.Classroom,
					strconv.Itoa(exchange.Lesson()),
					exchange.Note,
					data.DateStr,
				)
				check(err)
			}

			//parsing subject exchange
			for _, exchange := range data.ClassroomExchanges {
				stmtIns, err := db.Prepare("insert into substitutions(class, teacher, subject, classroom, lesson, note, date) values (?, ?, ?, ?, ?, ?, ?);")
				check(err)
				defer stmtIns.Close()
				_, err = stmtIns.Exec(
					parseSubstitutionsClass(exchange.Class),
					substTeacherToSchTeacher(exchange.Teacher),
					exchange.Subject,
					exchange.Classroom,
					strconv.Itoa(exchange.Lesson()),
					exchange.Note,
					data.DateStr,
				)
				check(err)
			}
		}

		//add 1 day
		currentDate = currentDate.Add(oneDay)
	}
}