Skip to content

Commit

Permalink
保留原始时区
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Dec 2, 2024
1 parent 40d537b commit 96244e8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
7 changes: 6 additions & 1 deletion protocols/utils.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ package protocols;
option go_package = "github.com/movsb/taoblog/proto";

message FormatTimeRequest {
repeated int32 unix = 1;
message Time {
int32 unix = 1;
string timezone = 2;
}
repeated Time times = 1;
// 设备时区。
string device = 2;
}

message FormatTimeResponse {
message Formatted {
string friendly = 1;
string original = 2;
string server = 3;
string device = 4;
}
Expand Down
21 changes: 18 additions & 3 deletions service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,28 @@ func NewUtils(instantNotifier notify.InstantNotifier) *Utils {
}

func (u *Utils) FormatTime(ctx context.Context, in *proto.FormatTimeRequest) (*proto.FormatTimeResponse, error) {
formatted := make([]*proto.FormatTimeResponse_Formatted, len(in.Unix))
for i, ts := range in.Unix {
formatted := make([]*proto.FormatTimeResponse_Formatted, len(in.Times))
for i, ts := range in.Times {
r := proto.FormatTimeResponse_Formatted{}
t := time.Unix(int64(ts), 0)
t := time.Unix(int64(ts.Unix), 0)
r.Friendly = timeago.Chinese.Format(t)
r.Server = t.In(fixedZone).Format(time.RFC3339)

if ts.Timezone != `` {
loc, err, _ := u.timeLocations.GetOrLoad(ctx, ts.Timezone, func(ctx context.Context, s string) (*time.Location, time.Duration, error) {
loc, err := time.LoadLocation(s)
// log.Println(`加载时区:`, s, loc, err)
return loc, time.Hour, err
})
if err != nil {
log.Println(err)
r.Original = r.Server
} else {
// log.Println(`时区:`, loc)
r.Original = t.In(loc).Format(time.RFC3339)
}
}

if in.Device != `` {
loc, err, _ := u.timeLocations.GetOrLoad(ctx, in.Device, func(ctx context.Context, s string) (*time.Location, time.Duration, error) {
loc, err := time.LoadLocation(s)
Expand Down
2 changes: 1 addition & 1 deletion theme/blog/statics/scripts/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ class Comment {
<div class="comment-meta">
<span class="${cmt.is_admin ? "author" : "nickname"}">${h2t(cmt.author)}</span>
${urlContent}
<time class="date" datetime="${date.toJSON()}" data-unix="${date.time}">${cmt.date_fuzzy}</time>
<time class="date" datetime="${date.toJSON()}" data-timezone="${date.zone}" data-unix="${date.time}">${cmt.date_fuzzy}</time>
</div>
${cmt.source_type === 'markdown'
? `<div class="comment-content html-content reset-list-style-type">${cmt.content}</div>`
Expand Down
12 changes: 9 additions & 3 deletions theme/blog/statics/scripts/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,12 @@ TaoBlog.vim = new __Vim();

function all() {
let times = document.querySelectorAll('time[data-unix]');
let stamps = Array.from(times).map(t => parseInt(t.getAttribute('data-unix')));
let stamps = Array.from(times).map(t => ({
unix: parseInt(t.dataset.unix),
timezone: t.dataset.timezone,
}));
let latest = 0;
stamps.forEach(t => { if (t > latest) latest = t; });
stamps.forEach(t => { if (t.unix > latest) latest = t.unix; });
return { times, stamps, latest };
}

Expand All @@ -247,7 +250,7 @@ async function format(stamps) {
let rsp = await fetch(path, {
method: 'POST',
body: JSON.stringify({
unix: stamps,
times: stamps,
device: timezone,
}),
});
Expand All @@ -273,6 +276,9 @@ let update = async function() {
if (f.device && f.device != f.server) {
title = `${title}\n${f.device}`;
}
if (f.original && f.original != f.server) {
title = `${title}\n${f.original}`;
}
t.title = title;
// console.log(title);
});
Expand Down

0 comments on commit 96244e8

Please sign in to comment.