gpt4 book ai didi

krl - 时间戳的相对格式

转载 作者:行者123 更新时间:2023-12-02 00:35:19 24 4
gpt4 key购买 nike

我在过去两天编写了 CS 462 Office Hours 应用程序。 most recent iteration告诉用户下一个办公时间段是什么时候。现在,它只是将其格式化为“星期四(2 月 3 日)下午 3 点”。不过,我希望它更聪明一点,并说出诸如“今天下午 3 点”或“明天上午 10 点”之类的话。

这类似于 Twitter 在推文上的相对时间戳(它表示“3 分钟前”或“23 小时前”;除此之外它还会列出日期)。不过,就我而言,情况恰恰相反,因为我们正在处理 future 的时间。

基本上,它需要足够聪明才能知道某个事件是今天还是明天。除此之外,我只想显示日期和星期几。

有没有办法用 KRL 做到这一点?我是否只需要使用逻辑 like this并编写一个函数(或模块)?

最佳答案

这些是我拥有的功能:

// First define some constants to measuring relative time
now = time:now({"tz": "America/Denver"});
midnight = time:new("23:59:59.000-07:00");
tomorrow_midnight = time:add(midnight, {"days": 1});
yesterday_midnight = time:add(midnight, {"days": -1});

// Functions for calculating relative time
relativefuturedate = function(d){
ispast(d) => "today (past) at " + justtime(d)
| istoday(d) => "today at " + justtime(d)
| istomorrow(d) => "tomorrow at " + justtime(d)
| datetime(d);
};

istoday = function(d){
d > now && d <= midnight;
};

istomorrow = function(d){
not istoday(d) && d <= tomorrow_midnight;
};

ispast = function(d){
d < now;
};

isfuture = function(d){
d > now;
};

justtime = function(d){
time:strftime(d, "%l:%M %p");
};

datetime = function(d){
time:strftime(d, "%A (%B %e) at %l:%M %p");
};

这应该可以解决问题。现在我正在用这个规则测试它:

rule first_rule {
select when pageview ".*"
pre {
today_9 = time:new("2011-02-09T09:00:00.000-07:00");
today_12 = time:new("2011-02-09T12:00:00.000-07:00");
today_4 = time:new("2011-02-09T16:00:00.000-07:00");
tomorrow_9 = time:new("2011-02-10T09:00:00.000-07:00");
tomorrow_4 = time:new("2011-02-10T16:00:00.000-07:00");
nextday_9 = time:new("2011-02-11T09:00:00.000-07:00");

relative_now = relativefuturedate(now);
relative_today_9 = relativefuturedate(today_9);
relative_today_12 = relativefuturedate(today_12);
relative_today_4 = relativefuturedate(today_4);
relative_tomorrow_9 = relativefuturedate(tomorrow_9);
relative_tomorrow_4 = relativefuturedate(tomorrow_4);
relative_nextday_9 = relativefuturedate(nextday_9);

message = <<
Now: #{relative_now}<br />
Today at 9: #{relative_today_9}<br />
Today at 12: #{relative_today_12}<br />
Today at 4: #{relative_today_4}<br />
Tomorrow at 9: #{relative_tomorrow_9}<br />
Tomorrow at 4: #{relative_tomorrow_4}<br />
Day after tomorrow at 9: #{relative_nextday_9}<br />
>>;
}
notify("Time calculations:", message) with sticky=true;
}

但是,这似乎还没有奏效。我得到这个输出:

Incorrect relative times

谁能看出哪里出了问题?

关于krl - 时间戳的相对格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4901650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com