tp.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func renderYear() string {
  7. // Get Current time
  8. var ct time.Time
  9. var offset int
  10. ct, offset = getCurrentTime()
  11. // So what the following does is
  12. // Add one year to the current time (and call it next year)
  13. // Get the start of this year
  14. // Get the start of the next year
  15. // The New Year
  16. ny := ct.AddDate(1, 0, 0)
  17. // Get the start of this and next year
  18. t1 := startYear(ct.Year(), 1, 1)
  19. t2 := startYear(ny.Year(), 1, 1)
  20. // Subtract the current year in seconds from next year, in seconds.
  21. // This will leave us with the total year in seconds
  22. // I do this in case there is a leap-year,
  23. // or some dodgy science reason to add 30 seconds to a day.
  24. seconds := t2.Sub(t1).Seconds()
  25. // From the current time, subtract the start of this year, in seconds.
  26. // This should leave us with how far we are into the year, in seconds.
  27. tdiff := ct.Sub(t1).Seconds()
  28. // Calculate the percentages
  29. percentage := calcPercentageFloat64(seconds, tdiff+float64(offset))
  30. // Wrap things up
  31. var render string
  32. render = renderPercentage(percentage)
  33. render = render + fmt.Sprintf(" %.7f%%", percentage)
  34. return render
  35. }
  36. func renderDay() string {
  37. // Get Current time
  38. var ct time.Time
  39. var offset int
  40. ct, offset = getCurrentTime()
  41. // Get the Start of the Day
  42. sd := time.Date(ct.Year(), ct.Month(), ct.Day(), 0, 0, 0, 0, time.UTC)
  43. // Get the End of the Day
  44. ed := ct.AddDate(0, 0, 1)
  45. ed = time.Date(ed.Year(), ed.Month(), ed.Day(), 0, 0, 0, 0, time.UTC)
  46. // From the end of the day, subtract the end of the day to get the number of seconds.
  47. // Normally this would be 86400, thanks to science this may change.
  48. // Think Leap Seconds
  49. //
  50. // Get the current number of seconds into the day
  51. seconds_total := ed.Sub(sd).Seconds()
  52. seconds_tmp := ed.Sub(ct).Seconds()
  53. seconds_current := seconds_total - seconds_tmp
  54. // Calculate the percentages
  55. percentage := calcPercentageFloat64(seconds_total, seconds_current+float64(offset))
  56. // Wrap everything up
  57. var render string
  58. render = renderPercentage(percentage)
  59. render = render + fmt.Sprintf(" %.7f%%", percentage)
  60. return render
  61. }
  62. func getCurrentTime() (time.Time, int) {
  63. // Current Time
  64. ct := time.Now()
  65. // Get the offset from whatever the system timezone is
  66. _, offset := ct.Local().Zone()
  67. // Make sure that the time is in UTC, so the offset makes sense
  68. loc, _ := time.LoadLocation("UTC")
  69. ct = ct.In(loc)
  70. // Return things
  71. return ct, offset
  72. }
  73. // displayPercentage Print a string of blocks simulating percentages
  74. func renderPercentage(percentage float64) string {
  75. var counter int
  76. var balk string
  77. for percentage > 2 {
  78. balk = balk + "▓"
  79. percentage = percentage - 2
  80. counter++
  81. }
  82. if percentage < 2 {
  83. balk = balk + "▒"
  84. counter++
  85. }
  86. for counter < 50 {
  87. balk = balk + "░"
  88. counter++
  89. }
  90. return balk
  91. }
  92. // Get the percentages between two float64s
  93. func calcPercentageFloat64(full float64, part float64) float64 {
  94. return part / (full / 100)
  95. }
  96. // Get the start of the year.
  97. func startYear(year, month, day int) time.Time {
  98. return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
  99. }
  100. func main() {
  101. fmt.Println(renderDay())
  102. fmt.Println(renderYear())
  103. }