@@ -163,13 +163,14 @@ func timeZoneOffset(f *Faker) float32 {
163163 return float32 (value )
164164}
165165
166- // javaDateFormatToGolangDateFormat converts java date format into go date format
167- func javaDateFormatToGolangDateFormat (format string ) string {
166+ // javaDateTimeFormatToGolangFormat converts java date/time format into go date/time format
167+ func javaDateTimeFormatToGolangFormat (format string ) string {
168168 format = strings .Replace (format , "ddd" , "_2" , - 1 )
169169 format = strings .Replace (format , "dd" , "02" , - 1 )
170170 format = strings .Replace (format , "d" , "2" , - 1 )
171171
172172 format = strings .Replace (format , "HH" , "15" , - 1 )
173+ format = strings .Replace (format , "H" , "15" , - 1 )
173174
174175 format = strings .Replace (format , "hh" , "03" , - 1 )
175176 format = strings .Replace (format , "h" , "3" , - 1 )
@@ -213,7 +214,7 @@ func javaDateFormatToGolangDateFormat(format string) string {
213214func addDateTimeLookup () {
214215 AddFuncLookup ("date" , Info {
215216 Display : "Date" ,
216- Category : "time " ,
217+ Category : "datetime " ,
217218 Description : "Representation of a specific day, month, and year, often used for chronological reference" ,
218219 Example : "2006-01-02T15:04:05Z07:00" ,
219220 Output : "string" ,
@@ -265,14 +266,14 @@ func addDateTimeLookup() {
265266 return f .Date ().Format (time .RFC3339 ), nil
266267 }
267268
268- return f .Date ().Format (javaDateFormatToGolangDateFormat (format )), nil
269+ return f .Date ().Format (javaDateTimeFormatToGolangFormat (format )), nil
269270 }
270271 },
271272 })
272273
273274 AddFuncLookup ("daterange" , Info {
274275 Display : "Date Range" ,
275- Category : "time " ,
276+ Category : "datetime " ,
276277 Description : "Random date between two ranges" ,
277278 Example : "1995-06-15T14:30:00Z" ,
278279 Output : "string" ,
@@ -310,7 +311,7 @@ func addDateTimeLookup() {
310311 if err != nil {
311312 return nil , err
312313 }
313- format = javaDateFormatToGolangDateFormat (format )
314+ format = javaDateTimeFormatToGolangFormat (format )
314315
315316 startdate , err := info .GetString (m , "startdate" )
316317 if err != nil {
@@ -336,7 +337,7 @@ func addDateTimeLookup() {
336337
337338 AddFuncLookup ("pastdate" , Info {
338339 Display : "PastDate" ,
339- Category : "time " ,
340+ Category : "datetime " ,
340341 Description : "Date that has occurred before the current moment in time" ,
341342 Example : "2007-01-24 13:00:35.820738079 +0000 UTC" ,
342343 Output : "time" ,
@@ -351,7 +352,7 @@ func addDateTimeLookup() {
351352
352353 AddFuncLookup ("futuredate" , Info {
353354 Display : "FutureDate" ,
354- Category : "time " ,
355+ Category : "datetime " ,
355356 Description : "Date that has occurred after the current moment in time" ,
356357 Example : "2107-01-24 13:00:35.820738079 +0000 UTC" ,
357358 Output : "time" ,
@@ -366,7 +367,7 @@ func addDateTimeLookup() {
366367
367368 AddFuncLookup ("nanosecond" , Info {
368369 Display : "Nanosecond" ,
369- Category : "time " ,
370+ Category : "datetime " ,
370371 Description : "Unit of time equal to one billionth (10^-9) of a second" ,
371372 Example : "196446360" ,
372373 Output : "int" ,
@@ -381,7 +382,7 @@ func addDateTimeLookup() {
381382
382383 AddFuncLookup ("second" , Info {
383384 Display : "Second" ,
384- Category : "time " ,
385+ Category : "datetime " ,
385386 Description : "Unit of time equal to 1/60th of a minute" ,
386387 Example : "43" ,
387388 Output : "int" ,
@@ -396,7 +397,7 @@ func addDateTimeLookup() {
396397
397398 AddFuncLookup ("minute" , Info {
398399 Display : "Minute" ,
399- Category : "time " ,
400+ Category : "datetime " ,
400401 Description : "Unit of time equal to 60 seconds" ,
401402 Example : "34" ,
402403 Output : "int" ,
@@ -411,7 +412,7 @@ func addDateTimeLookup() {
411412
412413 AddFuncLookup ("hour" , Info {
413414 Display : "Hour" ,
414- Category : "time " ,
415+ Category : "datetime " ,
415416 Description : "Unit of time equal to 60 minutes" ,
416417 Example : "8" ,
417418 Output : "int" ,
@@ -426,7 +427,7 @@ func addDateTimeLookup() {
426427
427428 AddFuncLookup ("day" , Info {
428429 Display : "Day" ,
429- Category : "time " ,
430+ Category : "datetime " ,
430431 Description : "24-hour period equivalent to one rotation of Earth on its axis" ,
431432 Example : "12" ,
432433 Output : "int" ,
@@ -441,7 +442,7 @@ func addDateTimeLookup() {
441442
442443 AddFuncLookup ("weekday" , Info {
443444 Display : "Weekday" ,
444- Category : "time " ,
445+ Category : "datetime " ,
445446 Description : "Day of the week excluding the weekend" ,
446447 Example : "Friday" ,
447448 Output : "string" ,
@@ -456,7 +457,7 @@ func addDateTimeLookup() {
456457
457458 AddFuncLookup ("month" , Info {
458459 Display : "Month" ,
459- Category : "time " ,
460+ Category : "datetime " ,
460461 Description : "Division of the year, typically 30 or 31 days long" ,
461462 Example : "1" ,
462463 Output : "string" ,
@@ -471,7 +472,7 @@ func addDateTimeLookup() {
471472
472473 AddFuncLookup ("monthstring" , Info {
473474 Display : "Month String" ,
474- Category : "time " ,
475+ Category : "datetime " ,
475476 Description : "String representation of a month name" ,
476477 Example : "September" ,
477478 Output : "string" ,
@@ -486,7 +487,7 @@ func addDateTimeLookup() {
486487
487488 AddFuncLookup ("year" , Info {
488489 Display : "Year" ,
489- Category : "time " ,
490+ Category : "datetime " ,
490491 Description : "Period of 365 days, the time Earth takes to orbit the Sun" ,
491492 Example : "1900" ,
492493 Output : "int" ,
@@ -501,7 +502,7 @@ func addDateTimeLookup() {
501502
502503 AddFuncLookup ("timezone" , Info {
503504 Display : "Timezone" ,
504- Category : "time " ,
505+ Category : "datetime " ,
505506 Description : "Region where the same standard time is used, based on longitudinal divisions of the Earth" ,
506507 Example : "Kaliningrad Standard Time" ,
507508 Output : "string" ,
@@ -516,7 +517,7 @@ func addDateTimeLookup() {
516517
517518 AddFuncLookup ("timezoneabv" , Info {
518519 Display : "Timezone Abbreviation" ,
519- Category : "time " ,
520+ Category : "datetime " ,
520521 Description : "Abbreviated 3-letter word of a timezone" ,
521522 Example : "KST" ,
522523 Output : "string" ,
@@ -531,7 +532,7 @@ func addDateTimeLookup() {
531532
532533 AddFuncLookup ("timezonefull" , Info {
533534 Display : "Timezone Full" ,
534- Category : "time " ,
535+ Category : "datetime " ,
535536 Description : "Full name of a timezone" ,
536537 Example : "(UTC+03:00) Kaliningrad, Minsk" ,
537538 Output : "string" ,
@@ -546,7 +547,7 @@ func addDateTimeLookup() {
546547
547548 AddFuncLookup ("timezoneoffset" , Info {
548549 Display : "Timezone Offset" ,
549- Category : "time " ,
550+ Category : "datetime " ,
550551 Description : "The difference in hours from Coordinated Universal Time (UTC) for a specific region" ,
551552 Example : "-5" ,
552553 Output : "float32" ,
@@ -561,7 +562,7 @@ func addDateTimeLookup() {
561562
562563 AddFuncLookup ("timezoneregion" , Info {
563564 Display : "Timezone Region" ,
564- Category : "time " ,
565+ Category : "datetime " ,
565566 Description : "Geographic area sharing the same standard time" ,
566567 Example : "America/Alaska" ,
567568 Output : "string" ,
@@ -574,4 +575,125 @@ func addDateTimeLookup() {
574575 Generate : func (f * Faker , m * MapParams , info * Info ) (any , error ) { return timeZoneRegion (f ), nil },
575576 })
576577
578+ AddFuncLookup ("time" , Info {
579+ Display : "Time" ,
580+ Category : "datetime" ,
581+ Description : "Random time string in the specified format" ,
582+ Example : "14:30:25" ,
583+ Output : "string" ,
584+ Aliases : []string {
585+ "time string" , "clock time" , "time format" , "time value" , "hour minute second" ,
586+ },
587+ Keywords : []string {
588+ "time" , "clock" , "hour" , "minute" , "second" , "format" , "24-hour" , "12-hour" , "am" , "pm" ,
589+ },
590+ Params : []Param {
591+ {
592+ Field : "format" ,
593+ Display : "Format" ,
594+ Type : "string" ,
595+ Default : "HH:mm:ss" ,
596+ Options : []string {"HH:mm:ss" , "HH:mm" , "hh:mm:ss a" , "hh:mm a" , "H:mm" , "h:mm a" },
597+ Description : "Time format string. Supports Java time format patterns or Go time format patterns" ,
598+ },
599+ },
600+ Generate : func (f * Faker , m * MapParams , info * Info ) (any , error ) {
601+ format , err := info .GetString (m , "format" )
602+ if err != nil {
603+ return nil , err
604+ }
605+
606+ // Convert java format to golang format
607+ golangFormat := javaDateTimeFormatToGolangFormat (format )
608+
609+ // Create a time with today's date but random time
610+ t := time .Date (2000 , 1 , 1 , hour (f ), minute (f ), second (f ), nanoSecond (f ), time .UTC )
611+
612+ return t .Format (golangFormat ), nil
613+ },
614+ })
615+
616+ AddFuncLookup ("timerange" , Info {
617+ Display : "Time Range" ,
618+ Category : "datetime" ,
619+ Description : "Random time string between start and end times" ,
620+ Example : "10:15:30" ,
621+ Output : "string" ,
622+ Aliases : []string {
623+ "time interval" , "time span" , "time window" , "between times" , "bounded time" ,
624+ },
625+ Keywords : []string {
626+ "timerange" , "range" , "between" , "time" , "start" , "end" , "bounds" , "limits" , "window" ,
627+ },
628+ Params : []Param {
629+ {
630+ Field : "starttime" ,
631+ Display : "Start Time" ,
632+ Type : "string" ,
633+ Default : "00:00:00" ,
634+ Description : "Start time string in the specified format" ,
635+ },
636+ {
637+ Field : "endtime" ,
638+ Display : "End Time" ,
639+ Type : "string" ,
640+ Default : "23:59:59" ,
641+ Description : "End time string in the specified format" ,
642+ },
643+ {
644+ Field : "format" ,
645+ Display : "Format" ,
646+ Type : "string" ,
647+ Default : "HH:mm:ss" ,
648+ Options : []string {"HH:mm:ss" , "HH:mm" , "hh:mm:ss a" , "hh:mm a" , "H:mm" , "h:mm a" },
649+ Description : "Time format string. Supports Java time format patterns or Go time format patterns" ,
650+ },
651+ },
652+ Generate : func (f * Faker , m * MapParams , info * Info ) (any , error ) {
653+ format , err := info .GetString (m , "format" )
654+ if err != nil {
655+ return nil , err
656+ }
657+
658+ startTime , err := info .GetString (m , "starttime" )
659+ if err != nil {
660+ return nil , err
661+ }
662+
663+ endTime , err := info .GetString (m , "endtime" )
664+ if err != nil {
665+ return nil , err
666+ }
667+
668+ // Convert java format to golang format
669+ golangFormat := javaDateTimeFormatToGolangFormat (format )
670+
671+ // Parse start and end times
672+ start , err := time .Parse (golangFormat , startTime )
673+ if err != nil {
674+ // If parsing fails, use a default start time
675+ start = time .Date (2000 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
676+ }
677+
678+ end , err := time .Parse (golangFormat , endTime )
679+ if err != nil {
680+ // If parsing fails, use a default end time
681+ end = time .Date (2000 , 1 , 1 , 23 , 59 , 59 , 999999999 , time .UTC )
682+ }
683+
684+ // Generate random time between start and end
685+ startNano := start .UnixNano ()
686+ endNano := end .UnixNano ()
687+
688+ if startNano > endNano {
689+ startNano , endNano = endNano , startNano
690+ }
691+
692+ randomNano := int64 (number (f , int (startNano ), int (endNano )))
693+ randomTime := time .Unix (0 , randomNano ).UTC ()
694+
695+ return randomTime .Format (golangFormat ), nil
696+ },
697+ })
698+
577699}
0 commit comments