Esempio n. 1
0
func (d *DatabaseHandler) UpdateHourInfo(place_id string, t time.Time, hour int, weekDay time.Weekday) (err error) {
	oldHour := &Hour{}
	dayString := strings.ToLower(weekDay.String()[0:3])
	if err = d.dbmap.SelectOne(oldHour, selectHour, place_id, dayString, hour); err != nil {
		log.Printf("[database][error] : Could not get hour for place_id: %s, %s", place_id, err)
	}

	newHour := &Hour{}
	if err = d.dbmap.SelectOne(newHour, selectHourInfo, dayString, hour, t, weekDay, hour, place_id); err != nil {
		log.Printf("[database][error] : Could not get hour info: %s", err)
		return
	}

	newHour.UpdatedAt = null.TimeFrom(time.Now())

	if oldHour.Id.Valid {
		newHour.Id = oldHour.Id
		log.Printf("Update hour: %d", oldHour.Id.Int64)
		if _, err = d.dbmap.Update(newHour); err != nil {
			log.Printf("[database][error] : Could not update hour: %s", err)
		}
	} else {
		log.Printf("Insert hour")
		if err = d.dbmap.Insert(newHour); err != nil {
			log.Printf("[database][error] : Could not insert hour: %s", err)
		}
	}
	return
}
Esempio n. 2
0
func (d *DatabaseHandler) UpdateDayInfo(place_id string, t time.Time, weekDay time.Weekday) (err error) {
	oldDay := &Day{}
	dayString := strings.ToLower(weekDay.String()[0:3])
	if err = d.dbmap.SelectOne(oldDay, selectDay, place_id, dayString); err != nil {
		log.Printf("[database][error] : Could not get day for place_id: %s, %v", place_id, err)
	}

	newDay := &Day{}
	if err = d.dbmap.SelectOne(newDay, selectDayInfo, dayString, t, weekDay, place_id); err != nil {
		log.Printf("[database][error] : Could not get day info: %s", err)
		return
	}

	newDay.UpdatedAt = null.TimeFrom(time.Now())

	if oldDay.Id.Valid {
		newDay.Id = oldDay.Id
		log.Printf("Update day: %d", newDay.Id.Int64)
		if _, err = d.dbmap.Update(newDay); err != nil {
			log.Printf("[database][error] : Could not update day: %s", err)
		}
	} else {
		log.Printf("Insert day")
		if err = d.dbmap.Insert(newDay); err != nil {
			log.Printf("[database][error] : Could not insert day: %s", err)
		}
	}
	return
}
Esempio n. 3
0
// Find a preceding weekday, same year
func searchBackwardsForDaySameYear(date time.Time, weekday time.Weekday) (time.Time, error) {
	// Start with the day before the given date
	current := date.AddDate(0, 0, -1)

	// Stay within the same year
	for current.Year() == date.Year() {
		// Check if it's the given weekday
		if current.Weekday() == weekday {
			// Found one
			return current, nil
		}

		// Go the previous day
		current = current.AddDate(0, 0, -1)
	}

	return date, errors.New("Could not find an earlier " + weekday.String() + " the same year!")
}
Esempio n. 4
0
// Find a later weekday, same month
func searchForwardsForDaySameMonth(date time.Time, weekday time.Weekday) (time.Time, error) {
	// Start with the day after the given date
	current := date.AddDate(0, 0, 1)

	// Stay within the same month
	for current.Month() == date.Month() {
		// Check if it's the given weekday
		if current.Weekday() == weekday {
			// Found one
			return current, nil
		}

		// Go the next day
		current = current.AddDate(0, 0, 1)
	}

	return date, errors.New("Could not find a later " + weekday.String() + " the same month!")
}
Esempio n. 5
0
// Finds the US name for a day of the week.
// Note that time.Weekday starts at 0 with Sunday, not Monday.
func (nc USCalendar) DayName(day time.Weekday) string {
	return day.String()
}