Example #1
0
			timeToCompare: time.Date(2010, 2, 5, 1, 0, 0, 0, time.UTC),
			result:        true,
		},
		{
			description:   "between the start and stop time but the stop time is before the start time",
			start:         time.Date(2010, 1, 5, 5, 0, 0, 0, time.UTC),
			stop:          time.Date(2010, 1, 5, 1, 0, 0, 0, time.UTC),
			timeToCompare: time.Date(2010, 2, 5, 4, 0, 0, 0, time.UTC),
			result:        false,
		},

		{
			description:   "between the start and stop time but the compare time is in a different timezone",
			start:         time.Date(2010, 1, 5, 2, 0, 0, 0, mst),
			stop:          time.Date(2010, 1, 5, 6, 0, 0, 0, mst),
			timeToCompare: time.Date(2010, 2, 5, 1, 1, 0, 0, pst),
			result:        true,
		},
	}

	for _, testCase := range cases {
		capturedTestCase := testCase // closures (╯°□°)╯︵ ┻━┻
		description := fmt.Sprintf("returns %t if the time to compare is %s", capturedTestCase.result, capturedTestCase.description)

		It(description, func() {
			result := between.Between(capturedTestCase.start, capturedTestCase.stop, capturedTestCase.timeToCompare)
			Ω(result).Should(Equal(capturedTestCase.result))
		})
	}
})
Example #2
0
func main() {
	currentTime := time.Now().UTC()
	var request models.CheckRequest

	err := json.NewDecoder(os.Stdin).Decode(&request)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error decoding payload: "+err.Error())
		os.Exit(1)
	}

	versions := []models.Version{}
	start := request.Source.Start
	stop := request.Source.Stop
	interval := request.Source.Interval
	incrementVersion := false

	lastCheckedAt := request.Version.Time.UTC()

	validateConfig(start, stop, interval)

	if start != "" && stop != "" {
		startTime, err := ParseTime(start)
		if err != nil {
			fmt.Fprintln(os.Stderr, "invalid start time: "+start+"; "+err.Error())
			os.Exit(1)
		}

		stopTime, err := ParseTime(stop)
		if err != nil {
			fmt.Fprintln(os.Stderr, "invalid stop time: "+stop+"; "+err.Error())
			os.Exit(1)
		}

		if between.Between(startTime, stopTime, currentTime) {
			if lastCheckedAt.IsZero() {
				incrementVersion = true
			} else {
				// This means we have a config that runs once within a given time range.
				// In that case, we set our interval to be the max time from that range
				// so it only runs once.
				if interval == "" {
					if startTime.After(stopTime) {
						stopTime = stopTime.Add(24 * time.Hour)
					}

					interval = stopTime.Sub(startTime).String()
				}

				intervalHasPassed, err := IntervalHasPassed(interval, request.Version.Time, currentTime)

				if err != nil {
					fmt.Fprintln(os.Stderr, "invalid interval: "+interval+"; "+err.Error())
					os.Exit(1)
				}

				if intervalHasPassed {
					incrementVersion = true
				}
			}
		}
	} else if interval != "" {
		intervalHasPassed, err := IntervalHasPassed(interval, request.Version.Time, currentTime)
		if err != nil {
			fmt.Fprintln(os.Stderr, "invalid interval: "+interval+"; "+err.Error())
			os.Exit(1)
		}

		if intervalHasPassed {
			incrementVersion = true
		}
	}

	if incrementVersion {
		versions = append(versions, models.Version{Time: currentTime})
	}

	json.NewEncoder(os.Stdout).Encode(versions)
}