tp.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. // hours := t2.Sub(t1).Hours()
  72. // tdiff = ct.Sub(t1).Hours()
  73. // hdiff := hours - tdiff
  74. // fmt.Printf("Uren te gaan: %v\nDagen te gaan: %v\nJaren te gaan: %v\n", hdiff, hdiff/24, (hdiff/24)/365)
  75. var render string
  76. render = renderPercentage(percentage)
  77. render = render + fmt.Sprintf(" %.7f%%", percentage)
  78. return render
  79. }
  80. func getCurrentTime() (time.Time, int) {
  81. // Declarations
  82. var offset int
  83. var loc *time.Location
  84. var err error
  85. // Current Time
  86. ct := time.Now()
  87. // Get the offset from whatever the system timezone is
  88. _, offset = ct.Local().Zone()
  89. // Make sure that the time is in UTC, so the offset makes sense
  90. loc, err = time.LoadLocation("UTC")
  91. if err != nil {
  92. log.Fatal(err)
  93. }
  94. ct = ct.In(loc)
  95. // Return things
  96. return ct, offset
  97. }
  98. // displayPercentage Print a string of blocks simulating percentages
  99. func renderPercentage(percentage float64) string {
  100. var counter int
  101. var balk string
  102. for percentage > 2 {
  103. balk = balk + "▓"
  104. percentage = percentage - 2
  105. counter++
  106. }
  107. if percentage < 2 {
  108. balk = balk + "▒"
  109. counter++
  110. }
  111. for counter < 50 {
  112. balk = balk + "░"
  113. counter++
  114. }
  115. return balk
  116. }
  117. // Get the percentages between two float64s
  118. func calcPercentageFloat64(full float64, part float64) float64 {
  119. var percentage float64
  120. percentage = part / (full / 100)
  121. if percentage >= 100 {
  122. percentage = percentage - 100
  123. }
  124. return percentage
  125. }
  126. // Get the start of the year.
  127. func startYear(year, month, day int) time.Time {
  128. return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
  129. }
  130. func main() {
  131. var dayFlag = flag.Bool("day", true, "Display progress of the day.")
  132. var yearFlag = flag.Bool("year", true, "Display progress of this year.")
  133. var customFlag = flag.Bool("custom", false, "Display a custom range")
  134. var customStart = flag.String("custom-start", "", "Start of the custom range")
  135. var customStop = flag.String("custom-stop", "", "Start of the custom range")
  136. flag.Parse()
  137. if *dayFlag {
  138. fmt.Println(renderDay())
  139. }
  140. if *yearFlag {
  141. fmt.Println(renderYear())
  142. }
  143. if *customFlag {
  144. bt, err := time.Parse(time.RFC3339, *customStart)
  145. if err != nil {
  146. log.Fatalln("Error parsing start time: ", err)
  147. }
  148. pa, err := time.Parse(time.RFC3339, *customStop)
  149. if err != nil {
  150. log.Fatalln("Error parsing stop time: ", err)
  151. }
  152. fmt.Println(renderCustom(bt, pa))
  153. }
  154. }