<commit_before>// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package timeutil

import (
	"fmt"
	"strings"
	"time"
)

type periodicStart struct {
	dayOfWeek      int
	hourOfDay      int
	minuteOfHour   int
	secondOfMinute int
}

type Periodic struct {
	start    *periodicStart
	duration time.Duration
}

type Period struct {
	Start time.Time
	End   time.Time
}

// ParsePeriodic returns a Periodic specified as a start and duration.
func ParsePeriodic(start, duration string) (*Periodic, error) {
	var err error

	pc := &Periodic{}

	if pc.start, err = parseStart(start); err != nil {
		return nil, fmt.Errorf("unable to parse start: %v", err)
	}
	if pc.duration, err = time.ParseDuration(duration); err != nil {
		return nil, fmt.Errorf("unable to parse duration: %v", err)
	}
	if pc.duration < time.Duration(0) {
		return nil, fmt.Errorf("duration cannot be negative")
	}

	return pc, nil
}

var weekdays = map[string]int{
	"sun": int(time.Sunday),
	"mon": int(time.Monday),
	"tue": int(time.Tuesday),
	"wed": int(time.Wednesday),
	"thu": int(time.Thursday),
	"fri": int(time.Friday),
	"sat": int(time.Saturday),
}

// parseStart parses a string into a periodicStart.
func parseStart(start string) (*periodicStart, error) {
	ps := &periodicStart{}
	ps.dayOfWeek = -1
	f := strings.Fields(start)

	switch len(f) {
	case 1: // no day provided
	case 2:
		if dow, ok := weekdays[strings.ToLower(f[0])]; ok {
			ps.dayOfWeek = dow
		} else {
			return nil, fmt.Errorf("invalid day of week %q", f[0])<commit_msg>pkg/timeutil: check that duration does not exceed period<commit_after>// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package timeutil

import (
	"fmt"
	"strings"
	"time"
)

type periodicStart struct {
	dayOfWeek      int
	hourOfDay      int
	minuteOfHour   int
	secondOfMinute int
}

type Periodic struct {
	start    *periodicStart
	duration time.Duration
}

type Period struct {
	Start time.Time
	End   time.Time
}

// ParsePeriodic returns a Periodic specified as a start and duration.
func ParsePeriodic(start, duration string) (*Periodic, error) {
	var err error

	pc := &Periodic{}

	if pc.start, err = parseStart(start); err != nil {
		return nil, fmt.Errorf("unable to parse start: %v", err)
	}

	if pc.duration, err = time.ParseDuration(duration); err != nil {
		return nil, fmt.Errorf("unable to parse duration: %v", err)
	}

	if pc.duration < time.Duration(0) {
		return nil, fmt.Errorf("duration cannot be negative")
	}

	// check that the duration of the window does not exceed the period.
	if (pc.start.dayOfWeek == -1 && pc.duration >= 24*time.Hour) || pc.duration >= 7*24*time.Hour {
		return nil, fmt.Errorf("duration cannot exceed period")
	}

	return pc, nil
}

var weekdays = map[string]int{
	"sun": int(time.Sunday),
	"mon": int(time.Monday),
	"tue": int(time.Tuesday),
	"wed": int(time.Wednesday),
	"thu": int(time.Thursday),
	"fri": int(time.Friday),
	"sat": int(time.Saturday),
}

// parseStart parses a string into a periodicStart.
func parseStart(start string) (*periodicStart, error) {
	ps := &periodicStart{}
	ps.dayOfWeek = -1
	f := strings.Fields(start)

	switch len(f) {
	case 1: // no day provided
	case 2:
		if dow, ok := weekdays[strings.ToLower(f[0])]; ok {
			ps.dayOfWeek = dow
		} else {
			return nil, fmt.Errorf("invalid day of week %q", f[0])
{'fraction_non_alphanumeric': 0.1236288848263254, 'fraction_numerical': 0.007769652650822669, 'mean_word_length': 3.6913183279742765, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3592318', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1392, 'n_words': 504}