tp.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "time"
  7. )
  8. func renderYear() string {
  9. // Get Current time
  10. var ct time.Time
  11. var offset int
  12. ct, offset = getCurrentTime()
  13. // So what the following does is
  14. // Add one year to the current time (and call it next year)
  15. // Get the start of this year
  16. // Get the start of the next year
  17. // The New Year
  18. ny := ct.AddDate(1, 0, 0)
  19. // Get the start of this and next year
  20. t1 := startYear(ct.Year(), 1, 1)
  21. t2 := startYear(ny.Year(), 1, 1)
  22. // Subtract the current year in seconds from next year, in seconds.
  23. // This will leave us with the total year in seconds
  24. // I do this in case there is a leap-year,
  25. // or some dodgy science reason to add 30 seconds to a day.
  26. seconds := t2.Sub(t1).Seconds()
  27. // From the current time, subtract the start of this year, in seconds.
  28. // This should leave us with how far we are into the year, in seconds.
  29. tdiff := ct.Sub(t1).Seconds()
  30. // Calculate the percentages
  31. percentage := calcPercentageFloat64(seconds, tdiff+float64(offset))
  32. // Wrap things up
  33. var render string
  34. render = renderPercentage(percentage)
  35. render = render + fmt.Sprintf(" %.7f%%", percentage)
  36. return render
  37. }
  38. func renderDay() string {
  39. // Get Current time
  40. var ct time.Time
  41. var offset int
  42. ct, offset = getCurrentTime()
  43. // Get the Start of the Day
  44. sd := time.Date(ct.Year(), ct.Month(), ct.Day(), 0, 0, 0, 0, time.UTC)
  45. // Get the End of the Day
  46. ed := ct.AddDate(0, 0, 1)
  47. ed = time.Date(ed.Year(), ed.Month(), ed.Day(), 0, 0, 0, 0, time.UTC)
  48. // From the end of the day, subtract the end of the day to get the number of seconds.
  49. // Normally this would be 86400, thanks to science this may change.
  50. // Think Leap Seconds
  51. //
  52. // Get the current number of seconds into the day
  53. seconds_total := ed.Sub(sd).Seconds()
  54. seconds_tmp := ed.Sub(ct).Seconds()
  55. seconds_current := seconds_total - seconds_tmp
  56. // Calculate the percentages
  57. percentage := calcPercentageFloat64(seconds_total, seconds_current+float64(offset))
  58. // Wrap everything up
  59. var render string
  60. render = renderPercentage(percentage)
  61. render = render + fmt.Sprintf(" %.7f%%", percentage)
  62. return render
  63. }
  64. func renderCustom(t1 time.Time, t2 time.Time) string {
  65. var ct time.Time
  66. var offset int
  67. ct, offset = getCurrentTime()
  68. seconds := t2.Sub(t1).Seconds()
  69. tdiff := ct.Sub(t1).Seconds()
  70. percentage := calcPercentageFloat64(seconds, tdiff+float64(offset))
  71. var render string
  72. render = renderPercentage(percentage)
  73. render = render + fmt.Sprintf(" %.7f%%", percentage)
  74. return render
  75. }
  76. func getCurrentTime() (time.Time, int) {
  77. // Declarations
  78. var offset int
  79. var loc *time.Location
  80. var err error
  81. // Current Time
  82. ct := time.Now()
  83. // Get the offset from whatever the system timezone is
  84. _, offset = ct.Local().Zone()
  85. // Make sure that the time is in UTC, so the offset makes sense
  86. loc, err = time.LoadLocation("UTC")
  87. if err != nil {
  88. log.Fatal(err)
  89. }
  90. ct = ct.In(loc)
  91. // Return things
  92. return ct, offset
  93. }
  94. // displayPercentage Print a string of blocks simulating percentages
  95. func renderPercentage(percentage float64) string {
  96. var counter int
  97. var balk string
  98. for percentage > 2 {
  99. balk = balk + "▓"
  100. percentage = percentage - 2
  101. counter++
  102. }
  103. if percentage < 2 {
  104. balk = balk + "▒"
  105. counter++
  106. }
  107. for counter < 50 {
  108. balk = balk + "░"
  109. counter++
  110. }
  111. return balk
  112. }
  113. // Get the percentages between two float64s
  114. func calcPercentageFloat64(full float64, part float64) float64 {
  115. var percentage float64
  116. percentage = part / (full / 100)
  117. if percentage >= 100 {
  118. percentage = percentage - 100
  119. }
  120. return percentage
  121. }
  122. // Get the start of the year.
  123. func startYear(year, month, day int) time.Time {
  124. return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
  125. }
  126. func main() {
  127. var dayFlag = flag.Bool("day", true, "Display progress of the day.")
  128. var yearFlag = flag.Bool("year", true, "Display progress of this year.")
  129. var customFlag = flag.Bool("custom", false, "Display a custom range")
  130. var customStart = flag.String("custom-start", "", "Start of the custom range")
  131. var customStop = flag.String("custom-stop", "", "Start of the custom range")
  132. flag.Parse()
  133. if *dayFlag {
  134. fmt.Println(renderDay())
  135. }
  136. if *yearFlag {
  137. fmt.Println(renderYear())
  138. }
  139. if *customFlag {
  140. bt, err := time.Parse(time.RFC3339, *customStart)
  141. if err != nil {
  142. log.Fatalln("Error parsing start time: ", err)
  143. }
  144. pa, err := time.Parse(time.RFC3339, *customStop)
  145. if err != nil {
  146. log.Fatalln("Error parsing stop time: ", err)
  147. }
  148. fmt.Println(renderCustom(bt, pa))
  149. }
  150. }