{{- $date := .date -}} {{- $now := .now | default now -}} {{- $mode := .mode | default "relative" -}} {{- $shift := 28800000000000 -}} {{- $s := ($date.Add $shift).UTC -}} {{- $n := ($now.Add $shift).UTC -}} {{- $weeks := slice "日" "一" "二" "三" "四" "五" "六" -}} {{- $wmap := dict "Sunday" 0 "Monday" 1 "Tuesday" 2 "Wednesday" 3 "Thursday" 4 "Friday" 5 "Saturday" 6 -}} {{- $dayS := int (dateFormat "2" $s) -}} {{- $monthS := int (dateFormat "1" $s) -}} {{- $yearS := int (dateFormat "2006" $s) -}} {{- $dayN := int (dateFormat "2" $n) -}} {{- $monthN := int (dateFormat "1" $n) -}} {{- $yearN := int (dateFormat "2006" $n) -}} {{- $hourS := $s.Hour -}} {{- $ts := $date.Unix -}} {{- $nowTs := $now.Unix -}} {{- if eq $mode "iso" -}} {{- dateFormat "2006-01-02" $date.UTC -}} {{- else if eq $mode "page" -}} {{- printf "%d 年 %d 月 %d 日" $yearS $monthS $dayS -}} {{- else if eq $mode "period" -}} {{- if or (ge $hourS 23) (lt $hourS 1) -}}午夜 {{- else if lt $hourS 3 -}}凌晨 {{- else if lt $hourS 5 -}}黎明 {{- else if lt $hourS 7 -}}清晨 {{- else if lt $hourS 9 -}}早晨 {{- else if lt $hourS 11 -}}上午 {{- else if lt $hourS 13 -}}中午 {{- else if lt $hourS 15 -}}午后 {{- else if lt $hourS 17 -}}下午 {{- else if lt $hourS 19 -}}傍晚 {{- else if lt $hourS 21 -}}晚上 {{- else -}}深夜 {{- end -}} {{- else if eq $mode "chinese" -}} {{- $period := partial "time.html" (dict "date" $date "now" $now "mode" "period") -}} {{- if and (eq $yearS $yearN) (eq $monthS $monthN) (eq $dayS $dayN) -}} {{- $period -}} {{- else if eq $yearS $yearN -}} {{- printf "%d 月 %d 日 %s" $monthS $dayS $period -}} {{- else -}} {{- printf "%d 年 %d 月 %d 日 %s" $yearS $monthS $dayS $period -}} {{- end -}} {{- else if eq $mode "relative" -}} {{- $diff := sub $nowTs $ts -}} {{- if lt $diff 60 -}}刚刚 {{- else if lt $diff 1800 -}}{{- partial "informal-num.html" (div $diff 60) -}}分钟前 {{- else if lt $diff 3600 -}}半小时前 {{- else if lt $diff 86400 -}}{{- partial "informal-num.html" (div $diff 3600) -}}小时前 {{- else -}} {{- $targetStart := (time.AsTime (printf "%04d-%02d-%02dT00:00:00Z" $yearS $monthS $dayS)).Unix -}} {{- $todayStart := (time.AsTime (printf "%04d-%02d-%02dT00:00:00Z" $yearN $monthN $dayN)).Unix -}} {{- $diffDays := sub $todayStart $targetStart -}} {{- $wdS := index $wmap ($s.Weekday | printf "%s") -}} {{- $wdN := index $wmap ($n.Weekday | printf "%s") -}} {{- if eq $diffDays 86400 -}}昨天 {{- else if eq $diffDays 172800 -}}前天 {{- else -}} {{- $weekOffset := $wdN -}}{{- if eq $weekOffset 0 -}}{{- $weekOffset = 7 -}}{{- end -}} {{- $weekStart := sub $todayStart (mul 86400 $weekOffset) -}} {{- if le $weekStart $targetStart -}}周{{- index $weeks $wdS -}} {{- else if le (sub $weekStart 604800) $targetStart -}}上周{{- index $weeks $wdS -}} {{- else -}} {{- $threeWeeks := sub $todayStart (mul 86400 (add 21 $weekOffset)) -}} {{- if le $threeWeeks $targetStart -}}{{- partial "informal-num.html" (div (sub $nowTs $targetStart) 604800) -}}周前 {{- else -}} {{- $lmYear := $yearN -}}{{- $lmMonth := sub $monthN 1 -}} {{- if eq $lmMonth 0 -}}{{- $lmYear = sub $yearN 1 -}}{{- $lmMonth = 12 -}}{{- end -}} {{- $lastMonthStart := (time.AsTime (printf "%04d-%02d-01T00:00:00Z" $lmYear $lmMonth)).Unix -}} {{- if le $lastMonthStart $targetStart -}}上个月 {{- else -}} {{- $yearStart := (time.AsTime (printf "%04d-01-01T00:00:00Z" $yearN)).Unix -}} {{- if le $yearStart $targetStart -}} {{- $months := sub $monthN $monthS -}} {{- if gt $dayS $dayN -}}{{- $months = sub $months 1 -}}{{- end -}} {{- if lt $months 0 -}}{{- $months = 0 -}}{{- end -}} {{- partial "informal-num.html" $months -}}个月前 {{- else -}} {{- $lastYearStart := (time.AsTime (printf "%04d-01-01T00:00:00Z" (sub $yearN 1))).Unix -}} {{- if le $lastYearStart $targetStart -}}去年 {{- else -}} {{- $beforeLastYearStart := (time.AsTime (printf "%04d-01-01T00:00:00Z" (sub $yearN 2))).Unix -}} {{- if le $beforeLastYearStart $targetStart -}}前年 {{- else -}} {{- $years := sub $yearN $yearS -}} {{- if gt (add (mul 30 (add $monthS 1)) $dayS) (add (mul 30 (add $monthN 1)) $dayN) -}} {{- $years = sub $years 1 -}} {{- end -}} {{- if lt $years 1 -}}{{- $years = 1 -}}{{- end -}} {{- partial "informal-num.html" $years -}}年前 {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}}