github.com/tawesoft/golib/v2@v2.10.0/legacy/humanize/examples_test.go (about) 1 package humanize_test 2 3 import ( 4 "fmt" 5 "time" 6 7 humanizex "github.com/tawesoft/golib/v2/legacy/humanize" 8 "golang.org/x/text/language" 9 ) 10 11 func Example_simple() { 12 mustInt64 := func(v int64, err error) int64 { 13 if err != nil { panic(err) } 14 return v 15 } 16 17 hEnglish := humanizex.NewHumanizer(language.English) 18 hDanish := humanizex.NewHumanizer(language.Danish) 19 hBengali := humanizex.NewHumanizer(language.Bengali) 20 21 // prints 1.5 KiB 22 fmt.Println(hEnglish.FormatBytesIEC(1024 + 512)) 23 24 // prints 1,5 KiB 25 fmt.Println(hDanish.FormatBytesIEC(1024 + 512)) 26 27 // prints ১.৫ KiB 28 fmt.Println(hBengali.FormatBytesIEC(1024 + 512)) 29 30 // prints 1536 31 fmt.Println(mustInt64(hEnglish.ParseBytesIEC("1.5 KiB"))) 32 33 // Output: 34 // 1.5 KiB 35 // 1,5 KiB 36 // ১.৫ KiB 37 // 1536 38 } 39 40 func Example_customFactors() { 41 factors := humanizex.Factors{ 42 Factors: []humanizex.Factor{ 43 {1, humanizex.Unit{"millicenton", "millicenton"}, humanizex.FactorModeReplace}, 44 {60, humanizex.Unit{"centon", "centon"}, humanizex.FactorModeReplace}, 45 {60 * 60, humanizex.Unit{"centar", "centar"}, humanizex.FactorModeReplace}, 46 {24 * 60 * 60, humanizex.Unit{"cycle", "cycle"}, humanizex.FactorModeReplace}, 47 {7 * 24 * 60 * 60, humanizex.Unit{"secton", "secton"}, humanizex.FactorModeReplace}, 48 {28 * 24 * 60 * 60, humanizex.Unit{"sectar", "sectar"}, humanizex.FactorModeReplace}, 49 {365 * 24 * 60 * 60, humanizex.Unit{"yahren", "yahren"}, humanizex.FactorModeReplace}, 50 {100 * 365 * 24 * 60 * 60, humanizex.Unit{"centauron", "centauron"}, humanizex.FactorModeReplace}, 51 }, 52 Components: 2, 53 } 54 55 h := humanizex.NewHumanizer(language.English) 56 57 est := float64((2 * 365 * 24 * 60 * 60) + 1) 58 59 fmt.Printf("Hey, I'll be with you in %s. Watch out for toasters!\n", 60 h.Format(est, humanizex.Unit{"millicenton", "millicenton"}, factors).Utf8) 61 62 // Output: 63 // Hey, I'll be with you in 2 yahren 1 millicenton. Watch out for toasters! 64 } 65 66 func Example_customDurations() { 67 plural := func (x float64) string { 68 if x > 0.99 && x < 1.01 { return "" } 69 return "s" 70 } 71 72 duration := (2 * time.Hour) + (20 * time.Second) 73 74 // prints "Basic time: 2 h 20 s" 75 fmt.Printf("Basic time: %s\n", humanizex.NewHumanizer(language.English).FormatDuration(duration)) 76 77 // Get the raw format parts 78 parts := humanizex.FormatParts( 79 duration.Seconds(), 80 humanizex.CommonUnits.Second, 81 humanizex.CommonFactors.Time, 82 ) 83 84 // prints "Nice time: 2 hours and 20 seconds ago" 85 fmt.Printf("Nice time: ") 86 if (len(parts) == 1) && (parts[0].Unit.Utf8 == "s") { 87 fmt.Printf("just now\n") 88 } else { 89 for i, part := range parts { 90 fmt.Printf("%d", int(part.Magnitude + 0.5)) 91 92 if part.Unit.Utf8 == "y" { 93 fmt.Printf(" year%s", plural(part.Magnitude)) 94 } else if part.Unit.Utf8 == "d" { 95 fmt.Printf(" day%s", plural(part.Magnitude)) 96 } else if part.Unit.Utf8 == "h" { 97 fmt.Printf(" hour%s", plural(part.Magnitude)) 98 } else if part.Unit.Utf8 == "min" { 99 fmt.Printf(" minute%s", plural(part.Magnitude)) 100 } else if part.Unit.Utf8 == "s" { 101 fmt.Printf(" second%s", plural(part.Magnitude)) 102 } 103 104 if i + 1 < len(parts) { 105 fmt.Printf(" and ") 106 } else { 107 fmt.Printf(" ago\n") 108 } 109 } 110 } 111 112 // Output: 113 // Basic time: 2 h 20 s 114 // Nice time: 2 hours and 20 seconds ago 115 }