brianvoe / gofakeit

Random fake data generator written in go
MIT License
4.49k stars 263 forks source link

Date Parsing Error with Single-Digit Months in CreatedFormat Field #346

Closed SuSuSoo closed 7 months ago

SuSuSoo commented 7 months ago

Description

When attempting to parse dates in the CreatedFormat field within the Foo struct, the program is encountering an error when the date string contains single-digit months. The error message is as follows:

Error: parsing time "1978-3-15" as "2006-01-02": cannot parse "3-15" as "01" 

This suggests that the parsing function expects a two-digit month format, but is receiving a single-digit month instead.

Steps to Reproduce

  1. Define a struct Foo with a CreatedFormat field expected to handle date strings.
  2. Attempt to parse a date string like "1993-7-19" using the expected "2006-01-02" format.
  3. Observe the parsing error due to the single-digit month.

Expected Behavior

The date parsing should be able to handle both single-digit and two-digit months and days, padding them with zeros when necessary to match the "2006-01-02" format.

Actual Behavior

The date parsing fails and returns an error when encountering single-digit months or days without leading zeros, which does not align with the expected format.

Possible Fix

It might be necessary to adjust the date generation or parsing logic to accommodate single-digit months and days by automatically padding them with leading zeros to fit the expected date format.

Environment

Thank you for your assistance!

import "github.com/brianvoe/gofakeit/v6"

// Create structs with random injected data
type Foo struct {
    Str           string
    Int           int
    Pointer       *int
    Name          string         `fake:"{firstname}"`         // Any available function all lowercase
    Sentence      string         `fake:"{sentence:3}"`        // Can call with parameters
    RandStr       string         `fake:"{randomstring:[hello,world]}"`
    Number        string         `fake:"{number:1,10}"`       // Comma separated for multiple values
    Regex         string         `fake:"{regex:[abcdef]{5}}"` // Generate string from regex
    Map           map[string]int `fakesize:"2"`
    Array         []string       `fakesize:"2"`
    ArrayRange    []string       `fakesize:"2,6"`
    Bar           Bar
    Skip          *string        `fake:"skip"`                // Set to "skip" to not generate data for
    SkipAlt       *string        `fake:"-"`                   // Set to "-" to not generate data for
    Created       time.Time                                   // Can take in a fake tag as well as a format tag
    CreatedFormat time.Time      `fake:"{year}-{month}-{day}" format:"2006-01-02"`
}

type Bar struct {
    Name    string
    Number  int
    Float   float32
}

// Pass your struct as a pointer
var f Foo
gofakeit.Struct(&f)

fmt.Println(f.Str)              // hrukpttuezptneuvunh
fmt.Println(f.Int)              // -7825289004089916589
fmt.Println(*f.Pointer)         // -343806609094473732
fmt.Println(f.Name)             // fred
fmt.Println(f.Sentence)         // Record river mind.
fmt.Println(f.RandStr)          // world
fmt.Println(f.Number)           // 4
fmt.Println(f.Regex)            // cbdfc
fmt.Println(f.Map)              // map[PxLIo:52 lxwnqhqc:846]
fmt.Println(f.Array)            // cbdfc
fmt.Printf("%+v", f.Bar)        // {Name:QFpZ Number:-2882647639396178786 Float:1.7636692e+37}
fmt.Println(f.Skip)             // <nil>
fmt.Println(f.Created.String()) // 1908-12-07 04:14:25.685339029 +0000 UTC

// Supported formats
// int, int8, int16, int32, int64,
// uint, uint8, uint16, uint32, uint64,
// float32, float64,
// bool, string,
// array, pointers, map
// time.Time // If setting time you can also set a format tag
// Nested Struct Fields and Embedded Fields
brianvoe commented 7 months ago

Its kinda out of my hands considering all im using is the core date parser and formatter. Open to any ideas or prs if you want to send something in.