forked from vieter-v/vieter
				
			Actually possibly kinda decent cron next func
							parent
							
								
									e6033f9ab4
								
							
						
					
					
						commit
						1116fee3fc
					
				|  | @ -1,9 +1,7 @@ | |||
| module cron | ||||
| 
 | ||||
| import git | ||||
| import datatypes | ||||
| import time | ||||
| import rand | ||||
| 
 | ||||
| struct ScheduledBuild { | ||||
| 	repo      git.GitRepo | ||||
|  | @ -15,12 +13,8 @@ fn (r1 ScheduledBuild) < (r2 ScheduledBuild) bool { | |||
| } | ||||
| 
 | ||||
| pub fn cron(conf Config) ? { | ||||
| 	mut queue := datatypes.MinHeap<time.Time>{} | ||||
| 
 | ||||
| 	ce := parse_expression('0 3') ? | ||||
| 	t := time.parse('2002-01-01 00:00:00') ? | ||||
| 
 | ||||
| 	println(t) | ||||
| 	t2 := ce.next(t) ? | ||||
| 	println(t2) | ||||
| } | ||||
|  |  | |||
|  | @ -38,17 +38,17 @@ pub fn (ce &CronExpression) next(ref time.Time) ?time.Time { | |||
| 		month_index++ | ||||
| 	} | ||||
| 
 | ||||
| 	if month_index < ce.months.len { | ||||
| 	if month_index < ce.months.len && sref.month == ce.months[month_index] { | ||||
| 		for day_index < ce.days.len && sref.day > ce.days[day_index] { | ||||
| 			day_index++ | ||||
| 		} | ||||
| 
 | ||||
| 		if day_index < ce.days.len { | ||||
| 		if day_index < ce.days.len && ce.days[day_index] == sref.day { | ||||
| 			for hour_index < ce.hours.len && sref.hour > ce.hours[hour_index] { | ||||
| 				hour_index++ | ||||
| 			} | ||||
| 
 | ||||
| 			if hour_index < ce.hours.len { | ||||
| 			if hour_index < ce.hours.len && ce.hours[hour_index] == sref.hour { | ||||
| 				// Minute is the only value where we explicitely make sure we | ||||
| 				// can't match sref's value exactly. This is to ensure we only | ||||
| 				// return values in the future. | ||||
|  |  | |||
|  | @ -2,38 +2,27 @@ module cron | |||
| 
 | ||||
| import time { parse } | ||||
| 
 | ||||
| fn util_test_time(exp string, t1_str string, t2_str string) ? { | ||||
| 	ce := parse_expression(exp) ? | ||||
| 	t1 := parse(t1_str) ? | ||||
| 	t2 := parse(t2_str) ? | ||||
| 
 | ||||
| 	t3 := ce.next(t1) ? | ||||
| 
 | ||||
| 	assert t2.year == t3.year | ||||
| 	assert t2.month == t3.month | ||||
| 	assert t2.day == t3.day | ||||
| 	assert t2.hour == t3.hour | ||||
| 	assert t2.minute == t3.minute | ||||
| } | ||||
| 
 | ||||
| fn test_next_simple() ? { | ||||
| 	ce := parse_expression('0 3') ? | ||||
| 	t := parse('2002-01-01 00:00:00') ? | ||||
| 	t2 := ce.next(t) ? | ||||
| 	// Very simple | ||||
| 	util_test_time('0 3', '2002-01-01 00:00:00', '2002-01-01 03:00:00') ? | ||||
| 
 | ||||
| 	assert t2.year == 2002 | ||||
| 	assert t2.month == 1 | ||||
| 	assert t2.day == 1 | ||||
| 	assert t2.hour == 3 | ||||
| 	assert t2.minute == 0 | ||||
| } | ||||
| 
 | ||||
| fn test_next_identical() ? { | ||||
| 	ce := parse_expression('0 3') ? | ||||
| 	t := parse('2002-01-01 03:00:00') ? | ||||
| 	t2 := ce.next(t) ? | ||||
| 
 | ||||
| 	assert t2.year == 2002 | ||||
| 	assert t2.month == 1 | ||||
| 	assert t2.day == 2 | ||||
| 	assert t2.hour == 3 | ||||
| 	assert t2.minute == 0 | ||||
| } | ||||
| 
 | ||||
| fn test_next_next_day() ? { | ||||
| 	ce := parse_expression('0 3') ? | ||||
| 	t := parse('2002-01-01 04:00:00') ? | ||||
| 	t2 := ce.next(t) ? | ||||
| 
 | ||||
| 	assert t2.year == 2002 | ||||
| 	assert t2.month == 1 | ||||
| 	assert t2.day == 2 | ||||
| 	assert t2.hour == 3 | ||||
| 	assert t2.minute == 0 | ||||
| 	// Overlap to next day | ||||
| 	util_test_time('0 3', '2002-01-01 03:00:00', '2002-01-02 03:00:00') ? | ||||
| 	util_test_time('0 3', '2002-01-01 04:00:00', '2002-01-02 03:00:00') ? | ||||
| 
 | ||||
| 	util_test_time('0 3/4', '2002-01-01 04:00:00', '2002-01-01 07:00:00') ? | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue