• 12-Dates and Times
    • 优化 User 的 LastSeen
    • 优化 Post 的 Timestamp
    • Links

    12-Dates and Times

    在本章,我们将讨论下 Go 处理日期和时间的方法,以及优化 User 及 Post 对象中的时间对象

    本章的GitHub链接为: Source, Diff, Zip

    优化 User 的 LastSeen

    Flask-Mega 主要是用了它灵活的模板功能,以及 Moment.js 来在前端实现了时间日期的优化,与之不同,我们主要采用的是后端来实现同样的功能

    model/user.go

    1. ...
    2. // FormattedLastSeen func
    3. func (u *User) FormattedLastSeen() string {
    4. return u.LastSeen.Format("2006-01-02 15:04:05")
    5. }
    6. ...

    templates/content/profile.html

    1. ...
    2. <p>Last seen on: {{ .ProfileUser.FormattedLastSeen }}</p>
    3. ...

    Notice: 虽然我也比较喜欢其他语言的 %Y-%m-%d %H:%M:%S” layout 形式,但是无奈 Go 就是 “2006-01-02 15:04:05” 这样的layout,没有道理可讲,我有时候甚至怕它搞错 01 和 02 哪个是月 哪个表示 日

    12-01

    本小节 Diff

    优化 Post 的 Timestamp

    我们目标是显示成 user said 35 minutes ago: post message 这样的形式,这就需要我们对时间日期进行转换

    简单点,我们就在 utils.go 中实现一个 FromTime 函数

    model/utils.go

    1. ...
    2. const (
    3. minute = 1
    4. hour = minute * 60
    5. day = hour * 24
    6. month = day * 30
    7. year = day * 365
    8. quarter = year / 4
    9. )
    10. // FromDuration returns a friendly string representing an approximation of the
    11. // given duration
    12. func FromDuration(d time.Duration) string {
    13. seconds := round(d.Seconds())
    14. if seconds < 30 {
    15. return "less than a minute"
    16. }
    17. if seconds < 90 {
    18. return "1 minute"
    19. }
    20. minutes := div(seconds, 60)
    21. if minutes < 45 {
    22. return fmt.Sprintf("%0d minutes", minutes)
    23. }
    24. hours := div(minutes, 60)
    25. if minutes < day {
    26. return fmt.Sprintf("about %s", pluralize(hours, "hour"))
    27. }
    28. if minutes < (42 * hour) {
    29. return "1 day"
    30. }
    31. days := div(hours, 24)
    32. if minutes < (30 * day) {
    33. return pluralize(days, "day")
    34. }
    35. months := div(days, 30)
    36. if minutes < (45 * day) {
    37. return "about 1 month"
    38. }
    39. if minutes < (60 * day) {
    40. return "about 2 months"
    41. }
    42. if minutes < year {
    43. return pluralize(months, "month")
    44. }
    45. rem := minutes % year
    46. years := minutes / year
    47. if rem < (3 * month) {
    48. return fmt.Sprintf("about %s", pluralize(years, "year"))
    49. }
    50. if rem < (9 * month) {
    51. return fmt.Sprintf("over %s", pluralize(years, "year"))
    52. }
    53. years++
    54. return fmt.Sprintf("almost %s", pluralize(years, "year"))
    55. }
    56. // FromTime returns a friendly string representing the approximate difference
    57. // from the given time and time.Now()
    58. func FromTime(t time.Time) string {
    59. now := time.Now()
    60. var d time.Duration
    61. var suffix string
    62. if t.Before(now) {
    63. d = now.Sub(t)
    64. suffix = "ago"
    65. } else {
    66. d = t.Sub(now)
    67. suffix = "from now"
    68. }
    69. return fmt.Sprintf("%s %s", FromDuration(d), suffix)
    70. }
    71. func pluralize(i int, s string) string {
    72. var buf bytes.Buffer
    73. buf.WriteString(fmt.Sprintf("%d %s", i, s))
    74. if i != 1 {
    75. buf.WriteString("s")
    76. }
    77. return buf.String()
    78. }
    79. func round(f float64) int {
    80. return int(math.Floor(f + .50))
    81. }
    82. func div(numerator int, denominator int) int {
    83. rem := numerator % denominator
    84. result := numerator / denominator
    85. if rem >= (denominator / 2) {
    86. result++
    87. }
    88. return result
    89. }

    现在我们可以使用 FromTime 函数实现我们的功能了

    model/post.go

    1. ...
    2. // FormattedTimeAgo func
    3. func (p *Post) FormattedTimeAgo() string {
    4. return FromTime(*p.Timestamp)
    5. }
    6. ...

    templates/content/index.html & profile.html & explore.html

    1. ...
    2. {{range .Posts}}
    3. <table class="table table-hover">
    4. <tr valign="top">
    5. <td width="36px"><img src="{{.User.Avatar}}&s=36"></td>
    6. <td><a href="/user/{{.User.Username}}">{{ .User.Username }}</a> said {{.FormattedTimeAgo}}:<br>{{ .Body }}</td>
    7. </tr>
    8. </table>
    9. {{end}}
    10. ...

    12-02

    本小节 Diff

    写在最后: 突然发现模板可以使用 类的 Function, 所以前面 User 的 Avatar 字段貌似就没有那么必要了,直接用个function来实现,而且好处是不用去关心更改邮箱后的 SetAvatar,不过我任性,就不改了,我也是才发现 Go 模板还有这个妙用的

    • 目录
    • 上一节: 11-Facelift
    • 下一节: 13-Javascript-Magic