<commit_before>// This file contains a few routines for parsing form values
package widget

import (
	"fmt"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
	"time"
	"github.com/skypies/util/date"
)

// {{{ FormValueDateRange

// This widget assumes the values 'date', 'range_from', and 'range_to'
func FormValueDateRange(r *http.Request) (s,e time.Time, err error) {
	err = nil

	switch r.FormValue("date") {
	case "today":
		s,_ = date.WindowForToday()
		e=s
	case "yesterday":
		s,_ = date.WindowForYesterday()
		e=s
	case "day":
		s = date.ArbitraryDatestring2MidnightPdt(r.FormValue("day"), "2006/01/02")
		e=s
	case "range":
		s = date.ArbitraryDatestring2MidnightPdt(r.FormValue("range_from"), "2006/01/02")
		e = date.ArbitraryDatestring2MidnightPdt(r.FormValue("range_to"), "2006/01/02")
		if s.After(e) { s,e = e,s }
	}
	
	e = e.Add(23*time.Hour + 59*time.Minute + 59*time.Second) // make sure e covers its whole day

	return
}

// }}}
// {{{ FormValueInt64

func FormValueInt64(r *http.Request, name string) int64 {
	val,_ := strconv.ParseInt(r.FormValue(name), 10, 64)
	return val
}

// }}}
// {{{ FormValueDuration

// As well as the normal syntax, we support "7d", where a day is 24*time.Hour.
func FormValueDuration(r *http.Request, name string) time.Duration {
	val,err := time.ParseDuration(r.FormValue(name))
	if err != nil {
		// Check for 'd', our own pseudo-duration
		bits := regexp.MustCompile("^([0-9])+d$").FindStringSubmatch(r.FormValue(name))
		if bits != nil && len(bits)==2 {
			num,_ := strconv.ParseInt(bits[1], 10, 64)
			val = time.Hour * 24 * time.Duration(num)
		}
	}

	return val
}

// }}}
// {{{ FormValueEpochTime

func FormValueEpochTime(r *http.Request, name string) time.Time {
	return time.Unix(FormValueInt64(r,name), 0)
}
<commit_msg>This pattern was used enough it was worth centralizing.<commit_after>// This file contains a few routines for parsing form values
package widget

import (
	"fmt"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
	"time"
	"github.com/skypies/util/date"
)

// {{{ FormValueDateRange

// This widget assumes the values 'date', 'range_from', and 'range_to'
func FormValueDateRange(r *http.Request) (s,e time.Time, err error) {
	err = nil

	switch r.FormValue("date") {
	case "today":
		s,_ = date.WindowForToday()
		e=s
	case "yesterday":
		s,_ = date.WindowForYesterday()
		e=s
	case "day":
		s = date.ArbitraryDatestring2MidnightPdt(r.FormValue("day"), "2006/01/02")
		e=s
	case "range":
		s = date.ArbitraryDatestring2MidnightPdt(r.FormValue("range_from"), "2006/01/02")
		e = date.ArbitraryDatestring2MidnightPdt(r.FormValue("range_to"), "2006/01/02")
		if s.After(e) { s,e = e,s }
	}
	
	e = e.Add(23*time.Hour + 59*time.Minute + 59*time.Second) // make sure e covers its whole day

	return
}

// }}}
// {{{ FormValue{Int64,IntWithDefault}

func FormValueInt64(r *http.Request, name string) int64 {
	val,_ := strconv.ParseInt(r.FormValue(name), 10, 64)
	return val
}

func FormValueIntWithDefault(r *http.Request, name string, dflt int) int {
	if val := FormValueInt64(r, name); val > 0 {
		return int(val)
	}
	return dflt
}

// }}}
// {{{ FormValueDuration

// As well as the normal syntax, we support "7d", where a day is 24*time.Hour.
func FormValueDuration(r *http.Request, name string) time.Duration {
	val,err := time.ParseDuration(r.FormValue(name))
	if err != nil {
		// Check for 'd', our own pseudo-duration
		bits := regexp.MustCompile("^([0-9])+d$").FindStringSubmatch(r.FormValue(name))
		if bits != nil && len(bits)==2 {
			num,_ := strconv.ParseInt(bits[1], 10, 64)
			val = time.Hour * 24 * time.Duration(num)
		}
	}

	return val
}

// }}}
// {{{ FormValueEpochTime

func FormValueEpochTime(r *http.Request, name string) time.Time {
	return time.Unix(FormValueInt64(r,name), 0)
}

{'fraction_non_alphanumeric': 0.16406869220607662, 'fraction_numerical': 0.031968295904887714, 'mean_word_length': 4.081879194630872, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4071891', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1456, 'n_words': 334}