forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/brianvoe/gofakeit/v6/time.go

785 lines
15 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package gofakeit
import (
"math/rand"
"strconv"
"strings"
"time"
)
// Date will generate a random time.Time struct
2024-04-26 19:30:35 +00:00
func Date() time.Time { return date(globalFaker.Rand) }
// Date will generate a random time.Time struct
2024-04-26 19:30:35 +00:00
func (f *Faker) Date() time.Time { return date(f.Rand) }
func date(r *rand.Rand) time.Time {
2024-04-26 19:30:35 +00:00
return time.Date(year(r), time.Month(month(r)), day(r), hour(r), minute(r), second(r), nanoSecond(r), time.UTC)
2024-04-26 19:30:35 +00:00
}
// FutureDate will generate a random past time.Time struct
2024-04-26 19:30:35 +00:00
func PastDate() time.Time { return pastDate(globalFaker.Rand) }
// FutureDate will generate a random past time.Time struct
2024-04-26 19:30:35 +00:00
func (f *Faker) PastDate() time.Time { return pastDate(f.Rand) }
func pastDate(r *rand.Rand) time.Time {
2024-04-26 19:30:35 +00:00
return time.Now().Add(time.Hour * -time.Duration(number(r, 1, 12)))
2024-04-26 19:30:35 +00:00
}
// FutureDate will generate a random future time.Time struct
2024-04-26 19:30:35 +00:00
func FutureDate() time.Time { return futureDate(globalFaker.Rand) }
// FutureDate will generate a random future time.Time struct
2024-04-26 19:30:35 +00:00
func (f *Faker) FutureDate() time.Time { return futureDate(f.Rand) }
func futureDate(r *rand.Rand) time.Time {
2024-04-26 19:30:35 +00:00
return time.Now().Add(time.Hour * time.Duration(number(r, 1, 12)))
2024-04-26 19:30:35 +00:00
}
// DateRange will generate a random time.Time struct between a start and end date
2024-04-26 19:30:35 +00:00
func DateRange(start, end time.Time) time.Time { return dateRange(globalFaker.Rand, start, end) }
// DateRange will generate a random time.Time struct between a start and end date
2024-04-26 19:30:35 +00:00
func (f *Faker) DateRange(start, end time.Time) time.Time { return dateRange(f.Rand, start, end) }
func dateRange(r *rand.Rand, start time.Time, end time.Time) time.Time {
2024-04-26 19:30:35 +00:00
return time.Unix(0, int64(number(r, int(start.UnixNano()), int(end.UnixNano())))).UTC()
2024-04-26 19:30:35 +00:00
}
// NanoSecond will generate a random nano second
2024-04-26 19:30:35 +00:00
func NanoSecond() int { return nanoSecond(globalFaker.Rand) }
// NanoSecond will generate a random nano second
2024-04-26 19:30:35 +00:00
func (f *Faker) NanoSecond() int { return nanoSecond(f.Rand) }
func nanoSecond(r *rand.Rand) int { return number(r, 0, 999999999) }
// Second will generate a random second
2024-04-26 19:30:35 +00:00
func Second() int { return second(globalFaker.Rand) }
// Second will generate a random second
2024-04-26 19:30:35 +00:00
func (f *Faker) Second() int { return second(f.Rand) }
func second(r *rand.Rand) int { return number(r, 0, 59) }
// Minute will generate a random minute
2024-04-26 19:30:35 +00:00
func Minute() int { return minute(globalFaker.Rand) }
// Minute will generate a random minute
2024-04-26 19:30:35 +00:00
func (f *Faker) Minute() int { return minute(f.Rand) }
func minute(r *rand.Rand) int { return number(r, 0, 59) }
// Hour will generate a random hour - in military time
2024-04-26 19:30:35 +00:00
func Hour() int { return hour(globalFaker.Rand) }
// Hour will generate a random hour - in military time
2024-04-26 19:30:35 +00:00
func (f *Faker) Hour() int { return hour(f.Rand) }
func hour(r *rand.Rand) int { return number(r, 0, 23) }
// Day will generate a random day between 1 - 31
2024-04-26 19:30:35 +00:00
func Day() int { return day(globalFaker.Rand) }
// Day will generate a random day between 1 - 31
2024-04-26 19:30:35 +00:00
func (f *Faker) Day() int { return day(f.Rand) }
func day(r *rand.Rand) int { return number(r, 1, 31) }
// WeekDay will generate a random weekday string (Monday-Sunday)
2024-04-26 19:30:35 +00:00
func WeekDay() string { return weekDay(globalFaker.Rand) }
// WeekDay will generate a random weekday string (Monday-Sunday)
2024-04-26 19:30:35 +00:00
func (f *Faker) WeekDay() string { return weekDay(f.Rand) }
func weekDay(r *rand.Rand) string { return time.Weekday(number(r, 0, 6)).String() }
// Month will generate a random month int
2024-04-26 19:30:35 +00:00
func Month() int { return month(globalFaker.Rand) }
// Month will generate a random month int
2024-04-26 19:30:35 +00:00
func (f *Faker) Month() int { return month(f.Rand) }
func month(r *rand.Rand) int { return number(r, 1, 12) }
// MonthString will generate a random month string
2024-04-26 19:30:35 +00:00
func MonthString() string { return monthString(globalFaker.Rand) }
// MonthString will generate a random month string
2024-04-26 19:30:35 +00:00
func (f *Faker) MonthString() string { return monthString(f.Rand) }
func monthString(r *rand.Rand) string { return time.Month(number(r, 1, 12)).String() }
// Year will generate a random year between 1900 - current year
2024-04-26 19:30:35 +00:00
func Year() int { return year(globalFaker.Rand) }
// Year will generate a random year between 1900 - current year
2024-04-26 19:30:35 +00:00
func (f *Faker) Year() int { return year(f.Rand) }
func year(r *rand.Rand) int { return number(r, 1900, time.Now().Year()) }
// TimeZone will select a random timezone string
2024-04-26 19:30:35 +00:00
func TimeZone() string { return timeZone(globalFaker.Rand) }
// TimeZone will select a random timezone string
2024-04-26 19:30:35 +00:00
func (f *Faker) TimeZone() string { return timeZone(f.Rand) }
func timeZone(r *rand.Rand) string { return getRandValue(r, []string{"timezone", "text"}) }
// TimeZoneFull will select a random full timezone string
2024-04-26 19:30:35 +00:00
func TimeZoneFull() string { return timeZoneFull(globalFaker.Rand) }
// TimeZoneFull will select a random full timezone string
2024-04-26 19:30:35 +00:00
func (f *Faker) TimeZoneFull() string { return timeZoneFull(f.Rand) }
func timeZoneFull(r *rand.Rand) string { return getRandValue(r, []string{"timezone", "full"}) }
// TimeZoneRegion will select a random region style timezone string, e.g. "America/Chicago"
2024-04-26 19:30:35 +00:00
func TimeZoneRegion() string { return timeZoneRegion(globalFaker.Rand) }
// TimeZoneRegion will select a random region style timezone string, e.g. "America/Chicago"
2024-04-26 19:30:35 +00:00
func (f *Faker) TimeZoneRegion() string { return timeZoneRegion(f.Rand) }
func timeZoneRegion(r *rand.Rand) string { return getRandValue(r, []string{"timezone", "region"}) }
// TimeZoneAbv will select a random timezone abbreviation string
2024-04-26 19:30:35 +00:00
func TimeZoneAbv() string { return timeZoneAbv(globalFaker.Rand) }
// TimeZoneAbv will select a random timezone abbreviation string
2024-04-26 19:30:35 +00:00
func (f *Faker) TimeZoneAbv() string { return timeZoneAbv(f.Rand) }
func timeZoneAbv(r *rand.Rand) string { return getRandValue(r, []string{"timezone", "abr"}) }
// TimeZoneOffset will select a random timezone offset
2024-04-26 19:30:35 +00:00
func TimeZoneOffset() float32 { return timeZoneOffset(globalFaker.Rand) }
// TimeZoneOffset will select a random timezone offset
2024-04-26 19:30:35 +00:00
func (f *Faker) TimeZoneOffset() float32 { return timeZoneOffset(f.Rand) }
func timeZoneOffset(r *rand.Rand) float32 {
2024-04-26 19:30:35 +00:00
value, _ := strconv.ParseFloat(getRandValue(r, []string{"timezone", "offset"}), 32)
2024-04-26 19:30:35 +00:00
return float32(value)
2024-04-26 19:30:35 +00:00
}
// javaDateFormatToGolangDateFormat converts java date format into go date format
2024-04-26 19:30:35 +00:00
func javaDateFormatToGolangDateFormat(format string) string {
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "ddd", "_2", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "dd", "02", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "d", "2", -1)
format = strings.Replace(format, "HH", "15", -1)
format = strings.Replace(format, "hh", "03", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "h", "3", -1)
format = strings.Replace(format, "mm", "04", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "m", "4", -1)
format = strings.Replace(format, "ss", "05", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "s", "5", -1)
format = strings.Replace(format, "yyyy", "2006", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "yy", "06", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "y", "06", -1)
format = strings.Replace(format, "SSS", "000", -1)
format = strings.Replace(format, "a", "pm", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "aa", "PM", -1)
format = strings.Replace(format, "MMMM", "January", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "MMM", "Jan", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "MM", "01", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "M", "1", -1)
format = strings.Replace(format, "ZZ", "-0700", -1)
if !strings.Contains(format, "Z07") {
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "Z", "-07", -1)
2024-04-26 19:30:35 +00:00
}
format = strings.Replace(format, "zz:zz", "Z07:00", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "zzzz", "Z0700", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "z", "MST", -1)
format = strings.Replace(format, "EEEE", "Monday", -1)
2024-04-26 19:30:35 +00:00
format = strings.Replace(format, "E", "Mon", -1)
return format
2024-04-26 19:30:35 +00:00
}
func addDateTimeLookup() {
2024-04-26 19:30:35 +00:00
AddFuncLookup("date", Info{
Display: "Date",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Representation of a specific day, month, and year, often used for chronological reference",
Example: "2006-01-02T15:04:05Z07:00",
Output: "string",
2024-04-26 19:30:35 +00:00
Params: []Param{
2024-04-26 19:30:35 +00:00
{
Field: "format",
Display: "Format",
Type: "string",
Default: "RFC3339",
Options: []string{"ANSIC", "UnixDate", "RubyDate", "RFC822", "RFC822Z", "RFC850", "RFC1123", "RFC1123Z", "RFC3339", "RFC3339Nano"},
2024-04-26 19:30:35 +00:00
Description: "Date time string format output. You may also use golang time format or java time format",
},
},
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
format, err := info.GetString(m, "format")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
switch format {
2024-04-26 19:30:35 +00:00
case "ANSIC":
2024-04-26 19:30:35 +00:00
return Date().Format(time.ANSIC), nil
2024-04-26 19:30:35 +00:00
case "UnixDate":
2024-04-26 19:30:35 +00:00
return Date().Format(time.UnixDate), nil
2024-04-26 19:30:35 +00:00
case "RubyDate":
2024-04-26 19:30:35 +00:00
return Date().Format(time.RubyDate), nil
2024-04-26 19:30:35 +00:00
case "RFC822":
2024-04-26 19:30:35 +00:00
return Date().Format(time.RFC822), nil
2024-04-26 19:30:35 +00:00
case "RFC822Z":
2024-04-26 19:30:35 +00:00
return Date().Format(time.RFC822Z), nil
2024-04-26 19:30:35 +00:00
case "RFC850":
2024-04-26 19:30:35 +00:00
return Date().Format(time.RFC850), nil
2024-04-26 19:30:35 +00:00
case "RFC1123":
2024-04-26 19:30:35 +00:00
return Date().Format(time.RFC1123), nil
2024-04-26 19:30:35 +00:00
case "RFC1123Z":
2024-04-26 19:30:35 +00:00
return Date().Format(time.RFC1123Z), nil
2024-04-26 19:30:35 +00:00
case "RFC3339":
2024-04-26 19:30:35 +00:00
return Date().Format(time.RFC3339), nil
2024-04-26 19:30:35 +00:00
case "RFC3339Nano":
2024-04-26 19:30:35 +00:00
return Date().Format(time.RFC3339Nano), nil
2024-04-26 19:30:35 +00:00
default:
2024-04-26 19:30:35 +00:00
if format == "" {
2024-04-26 19:30:35 +00:00
return Date().Format(time.RFC3339), nil
2024-04-26 19:30:35 +00:00
}
return Date().Format(javaDateFormatToGolangDateFormat(format)), nil
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("daterange", Info{
Display: "DateRange",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Random date between two ranges",
Example: "2006-01-02T15:04:05Z07:00",
Output: "string",
2024-04-26 19:30:35 +00:00
Params: []Param{
2024-04-26 19:30:35 +00:00
{
Field: "startdate",
Display: "Start Date",
Type: "string",
Default: "1970-01-01",
2024-04-26 19:30:35 +00:00
Description: "Start date time string",
},
2024-04-26 19:30:35 +00:00
{
Field: "enddate",
Display: "End Date",
Type: "string",
Default: time.Now().Format("2006-01-02"),
2024-04-26 19:30:35 +00:00
Description: "End date time string",
},
2024-04-26 19:30:35 +00:00
{
Field: "format",
Display: "Format",
Type: "string",
Default: "yyyy-MM-dd",
2024-04-26 19:30:35 +00:00
Description: "Date time string format",
},
},
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
format, err := info.GetString(m, "format")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
format = javaDateFormatToGolangDateFormat(format)
startdate, err := info.GetString(m, "startdate")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
startDateTime, err := time.Parse(format, startdate)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
enddate, err := info.GetString(m, "enddate")
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
endDateTime, err := time.Parse(format, enddate)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return nil, err
2024-04-26 19:30:35 +00:00
}
return DateRange(startDateTime, endDateTime).Format(format), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("pasttime", Info{
Display: "PastTime",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Date that has occurred before the current moment in time",
Example: "2007-01-24 13:00:35.820738079 +0000 UTC",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return pastDate(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("futuretime", Info{
Display: "FutureTime",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Date that has occurred after the current moment in time",
Example: "2107-01-24 13:00:35.820738079 +0000 UTC",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return futureDate(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("nanosecond", Info{
Display: "Nanosecond",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Unit of time equal to One billionth (10^-9) of a second",
Example: "196446360",
Output: "int",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return nanoSecond(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("second", Info{
Display: "Second",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Unit of time equal to 1/60th of a minute",
Example: "43",
Output: "int",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return second(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("minute", Info{
Display: "Minute",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Unit of time equal to 60 seconds",
Example: "34",
Output: "int",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return minute(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("hour", Info{
Display: "Hour",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Unit of time equal to 60 minutes",
Example: "8",
Output: "int",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return hour(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("day", Info{
Display: "Day",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "24-hour period equivalent to one rotation of Earth on its axis",
Example: "12",
Output: "int",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return day(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("weekday", Info{
Display: "Weekday",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Day of the week excluding the weekend",
Example: "Friday",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return weekDay(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("month", Info{
Display: "Month",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Division of the year, typically 30 or 31 days long",
Example: "1",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return month(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("monthstring", Info{
Display: "Month String",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "String Representation of a month name",
Example: "September",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return monthString(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("year", Info{
Display: "Year",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Period of 365 days, the time Earth takes to orbit the Sun",
Example: "1900",
Output: "int",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return year(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("timezone", Info{
Display: "Timezone",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Region where the same standard time is used, based on longitudinal divisions of the Earth",
Example: "Kaliningrad Standard Time",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return timeZone(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("timezoneabv", Info{
Display: "Timezone Abbreviation",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Abbreviated 3-letter word of a timezone",
Example: "KST",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return timeZoneAbv(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("timezonefull", Info{
Display: "Timezone Full",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Full name of a timezone",
Example: "(UTC+03:00) Kaliningrad, Minsk",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return timeZoneFull(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("timezoneoffset", Info{
Display: "Timezone Offset",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "The difference in hours from Coordinated Universal Time (UTC) for a specific region",
Example: "3",
Output: "float32",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return timeZoneOffset(r), nil
2024-04-26 19:30:35 +00:00
},
})
AddFuncLookup("timezoneregion", Info{
Display: "Timezone Region",
Category: "time",
2024-04-26 19:30:35 +00:00
Description: "Geographic area sharing the same standard time",
Example: "America/Alaska",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return timeZoneRegion(r), nil
2024-04-26 19:30:35 +00:00
},
})
}