-
-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove gogs/gogs/pkg/tool dependency
this borrows some code from github.com/gogs/gogs/pkg/tool to avoid pulling it in as a dependency, along with many other indirect deps.
- Loading branch information
Rob Loranger
committed
Sep 11, 2019
1 parent
6b99d75
commit feba200
Showing
4 changed files
with
144 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package writefreely | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// Borrowed from github.com/gogs/gogs/pkg/tool | ||
|
||
// Seconds-based time units | ||
const ( | ||
Minute = 60 | ||
Hour = 60 * Minute | ||
Day = 24 * Hour | ||
Week = 7 * Day | ||
Month = 30 * Day | ||
Year = 12 * Month | ||
) | ||
|
||
func computeTimeDiff(diff int64) (int64, string) { | ||
diffStr := "" | ||
switch { | ||
case diff <= 0: | ||
diff = 0 | ||
diffStr = "now" | ||
case diff < 2: | ||
diff = 0 | ||
diffStr = "1 second" | ||
case diff < 1*Minute: | ||
diffStr = fmt.Sprintf("%d seconds", diff) | ||
diff = 0 | ||
|
||
case diff < 2*Minute: | ||
diff -= 1 * Minute | ||
diffStr = "1 minute" | ||
case diff < 1*Hour: | ||
diffStr = fmt.Sprintf("%d minutes", diff/Minute) | ||
diff -= diff / Minute * Minute | ||
|
||
case diff < 2*Hour: | ||
diff -= 1 * Hour | ||
diffStr = "1 hour" | ||
case diff < 1*Day: | ||
diffStr = fmt.Sprintf("%d hours", diff/Hour) | ||
diff -= diff / Hour * Hour | ||
|
||
case diff < 2*Day: | ||
diff -= 1 * Day | ||
diffStr = "1 day" | ||
case diff < 1*Week: | ||
diffStr = fmt.Sprintf("%d days", diff/Day) | ||
diff -= diff / Day * Day | ||
|
||
case diff < 2*Week: | ||
diff -= 1 * Week | ||
diffStr = "1 week" | ||
case diff < 1*Month: | ||
diffStr = fmt.Sprintf("%d weeks", diff/Week) | ||
diff -= diff / Week * Week | ||
|
||
case diff < 2*Month: | ||
diff -= 1 * Month | ||
diffStr = "1 month" | ||
case diff < 1*Year: | ||
diffStr = fmt.Sprintf("%d months", diff/Month) | ||
diff -= diff / Month * Month | ||
|
||
case diff < 2*Year: | ||
diff -= 1 * Year | ||
diffStr = "1 year" | ||
default: | ||
diffStr = fmt.Sprintf("%d years", diff/Year) | ||
diff = 0 | ||
} | ||
return diff, diffStr | ||
} | ||
|
||
// timeSincePro calculates the time interval and generate full user-friendly string. | ||
func timeSincePro(then time.Time) string { | ||
now := time.Now() | ||
diff := now.Unix() - then.Unix() | ||
|
||
if then.After(now) { | ||
return "future" | ||
} | ||
|
||
var timeStr, diffStr string | ||
for { | ||
if diff == 0 { | ||
break | ||
} | ||
|
||
diff, diffStr = computeTimeDiff(diff) | ||
timeStr += ", " + diffStr | ||
} | ||
return strings.TrimPrefix(timeStr, ", ") | ||
} | ||
|
||
func logn(n, b float64) float64 { | ||
return math.Log(n) / math.Log(b) | ||
} | ||
|
||
func humanateBytes(s uint64, base float64, sizes []string) string { | ||
if s < 10 { | ||
return fmt.Sprintf("%d B", s) | ||
} | ||
e := math.Floor(logn(float64(s), base)) | ||
suffix := sizes[int(e)] | ||
val := float64(s) / math.Pow(base, math.Floor(e)) | ||
f := "%.0f" | ||
if val < 10 { | ||
f = "%.1f" | ||
} | ||
|
||
return fmt.Sprintf(f+" %s", val, suffix) | ||
} | ||
|
||
// fileSize calculates the file size and generate user-friendly string. | ||
func fileSize(s int64) string { | ||
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"} | ||
return humanateBytes(uint64(s), 1024, sizes) | ||
} |