// parseTime takes a Neptune Apex date/time string with a timezone and
// returns a time.Time struct.
760407
func parseTime(val string, tz float64) (time.Time, error) {
	// Magic time constant from https://golang.org/pkg/time/#Parse
	const TimeLayout = "01/02/2006 15:04:05 -0700"

	// Timezone offset needs to be explicit
	sign := '+'
	if tz < 0 {
		sign = '-'
	}

	// Build a time string with the timezone in a format Go can parse.
	tzs := fmt.Sprintf("%c%04d", sign, int(math.Abs(tz))*100)
	ts := fmt.Sprintf("%s %s", val, tzs)
	t, err := time.Parse(TimeLayout, ts)
	if err != nil {
		return time.Now(), fmt.Errorf("unable to parse %q (%v)", ts, err)
	}
	return t, nil
}
774340
func TimestampToTime(timestamp *timestamp.Timestamp) time.Time {
	if timestamp == nil {
		return time.Unix(0, 0).UTC()
	}
	return time.Unix(
		timestamp.Seconds,
		int64(timestamp.Nanos),
	).UTC()
}
715010
func ParseTimeFromInt64(sc *stmtctx.StatementContext, num int64) (Time, error) {
	return parseDateTimeFromNum(sc, num)
}
8180
func ParseTime(s string) (Time, error) {
	if s == "" {
		return Time{}, nil
	}
	if !timeRe.MatchString(s) {
		return Time{}, fmt.Errorf("parse time: expected hhmmss.ss format, got '%s'", s)
	}
	hour, _ := strconv.Atoi(s[:2])
	minute, _ := strconv.Atoi(s[2:4])
	second, _ := strconv.ParseFloat(s[4:], 64)
	whole, frac := math.Modf(second)
	return Time{true, hour, minute, int(whole), int(round(frac * 1000))}, nil
}
169623
func ParseTimestampFromNum(num int64) (Time, error) {
	return ParseTimeFromNum(num, mysql.TypeTimestamp, DefaultFsp)
}
923214
func ParseInput(input [6]string) *crontime {
	var result crontime
	// Check the syntax of the input
	for i := 0; i != len(input); i++ {
		if checkSyntax(input[i]) == false {
			log.Panicln("Invalid config at Line", i) // TODO be more helpful
		}
	}
	// Parse Input like 23-05
	for i := 0; i != len(input); i++ {
		if strings.Contains(input[i], "-") {
			result.parseRange(input[i], i)
		}
	}
	// Parse Input like 05,23,17
	for i := 0; i != len(input); i++ {
		if strings.Contains(input[i], ",") {
			result.parseIRange(input[i], i)
		}
	}
	// Parse input like */05
	for i := 0; i != len(input); i++ {
		if strings.Contains(input[i], "*/") {
			result.parsePeriodic(input[i], i)
		}
	}
	// Parse input like *
	for i := 0; i != len(input); i++ {
		if input[i] == "*" {
			result.parseIgnore(i)
		}
	}
	//Parse single Numbers (05 or 23)
	for i := 0; i != len(input); i++ {
		if !strings.ContainsAny(input[i], "-*/,") {
			tempary := make([]int, 1)
			temp, err := strconv.ParseInt(input[i], 10, 0)
			tempary[0] = int(temp)
			if err != nil {
				panic(err)
			}
			switch i {
			case 0:
				result.second = tempary
			case 1:
				result.minute = tempary
			case 2:
				result.hour = tempary
			case 3:
				result.dow = tempary
			case 4:
				result.dom = tempary
			case 5:
				result.month = tempary
			}
		}
	}
	// Do a sanity check, eg. there is no 32th Day in any Month
	result.checkValues()

	// Makes timestamp generation easier
	sort.Ints(result.second)
	sort.Ints(result.minute)
	sort.Ints(result.hour)
	sort.Ints(result.dow)
	sort.Ints(result.dom)
	sort.Ints(result.month)

	return &result
}
1270782
func ParseTimestampFromNum(sc *stmtctx.StatementContext, num int64) (Time, error) {
	return ParseTimeFromNum(sc, num, mysql.TypeTimestamp, DefaultFsp)
}
1222758
func (p *Parser) parseTimestamp() (time.Time, error) {
	var ts time.Time
	var err error
	var tsFmtLen int
	var sub []byte

	tsFmts := []string{
		time.Stamp,
		time.RFC3339,
	}
	// if timestamps starts with numeric try formats with different order
	// it is more likely that timestamp is in RFC3339 format then
	if c := p.buff[p.cursor]; c > '0' && c < '9' {
		tsFmts = []string{
			time.RFC3339,
			time.Stamp,
		}
	}

	found := false
	for _, tsFmt := range tsFmts {
		tsFmtLen = len(tsFmt)

		if p.cursor+tsFmtLen > p.l {
			continue
		}

		sub = p.buff[p.cursor : tsFmtLen+p.cursor]
		ts, err = time.ParseInLocation(tsFmt, string(sub), p.location)
		if err == nil {
			found = true
			break
		}
	}

	if !found {
		p.cursor = len(time.Stamp)

		// XXX : If the timestamp is invalid we try to push the cursor one byte
		// XXX : further, in case it is a space
		if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
			p.cursor++
		}

		return ts, syslogparser.ErrTimestampUnknownFormat
	}

	fixTimestampIfNeeded(&ts)

	p.cursor += tsFmtLen

	if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
		p.cursor++
	}

	return ts, nil
}
690074
func IsTime(str string, format string) bool {
	_, err := time.Parse(format, str)
	return err == nil
}
853356
func (k *Key) parseTimesFormat(format string, strs []string, addInvalid, returnOnInvalid bool) ([]time.Time, error) {
	vals := make([]time.Time, 0, len(strs))
	for _, str := range strs {
		val, err := time.Parse(format, str)
		if err != nil && returnOnInvalid {
			return nil, err
		}
		if err == nil || addInvalid {
			vals = append(vals, val)
		}
	}
	return vals, nil
}
393060
func formatPAXTime(ts time.Time) (s string) {
	secs, nsecs := ts.Unix(), ts.Nanosecond()
	if nsecs == 0 {
		return strconv.FormatInt(secs, 10)
	}

	// If seconds is negative, then perform correction.
	sign := ""
	if secs < 0 {
		sign = "-"             // Remember sign
		secs = -(secs + 1)     // Add a second to secs
		nsecs = -(nsecs - 1E9) // Take that second away from nsecs
	}
	return strings.TrimRight(fmt.Sprintf("%s%d.%09d", sign, secs, nsecs), "0")
}
41286
func (s *QueryFilter) SetDeltaTime(v *DeltaTime) *QueryFilter {
	s.DeltaTime = v
	return s
}
772088
func UnmarshalDateTime(s string) (result time.Time, err error) {
	dateStr, timeStr, zoneStr, err := splitCompleteDateTimeZone(s)
	if err != nil {
		return
	}

	if len(zoneStr) != 0 {
		err = fmt.Errorf("soap datetime: unexpected timezone in %q", s)
		return
	}

	year, month, day, err := parseDateParts(dateStr)
	if err != nil {
		return
	}

	var hour, minute, second int
	if len(timeStr) != 0 {
		hour, minute, second, err = parseTimeParts(timeStr)
		if err != nil {
			return
		}
	}

	result = time.Date(year, time.Month(month), day, hour, minute, second, 0, localLoc)
	return
}
1110829
func (s *Server) ConfigTime() time.Time {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.configTime
}
666465
func parseDockerTimestamp(s string) (time.Time, error) {
	// Timestamp returned by Docker is in time.RFC3339Nano format.
	return time.Parse(time.RFC3339Nano, s)
}
68195
func StatATimeAsTime(st *syscall.Stat_t) time.Time {
	// The int64 conversions ensure the line compiles for 32-bit systems as well.
	return time.Unix(int64(st.Atim.Sec), int64(st.Atim.Nsec)) // nolint: unconvert
}
150565
func ParseDockerTimestamp(s string) (time.Time, error) {
	// Timestamp returned by Docker is in time.RFC3339Nano format.
	return time.Parse(time.RFC3339Nano, s)
}