时间戳的相对格式

发布于 2024-10-15 16:41:59 字数 544 浏览 5 评论 0 原文

过去两天我编写了 CS 462 Office Hours 应用程序。 最近的迭代 告诉用户下一个办公时间段是什么时候。目前,它只是将其格式设置为“星期四(2 月 3 日)下午 3 点”。不过,我希望它能更聪明一点,比如说“今天下午 3 点”或“明天上午 10 点”。

这类似于 Twitter 推文上的相对时间戳(它显示“3 分钟前”或“23 小时前”;除此之外还列出了日期)。但就我而言,情况恰恰相反,因为我们面对的是未来。

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

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

I wrote the CS 462 Office Hours app over the past two days. The most recent iteration tells the user when the next office hour time block will be. Right now, it just formats it as "Thursday (February 3) at 3 PM." I'd like it to be a little smarter, though, and say something like "this afternoon at 3 PM" or "tomorrow at 10 AM".

This is similar to Twitter's relative timestamps on tweets (it says "3 minutes ago" or "23 hours ago;" beyond that it lists the date). In my case, though, it will be the opposite, since we're dealing with future times.

Basically, it needs to be smart enough to know that an event is today or tomorrow. Beyond that, I just want to display the date and day of the week.

Is there a way to do this with KRL? Do I just need to use logic like this and write a function (or module)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-10-22 16:41:59

这些是我拥有的功能:

// 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;
}

但是,这似乎还不起作用。我得到以下输出:

In Correctrelative times

任何人都可以看到出了什么问题吗?

These are the functions I have:

// 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");
};

This should do the trick. Right now I'm testing it with this rule:

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;
}

However, that doesn't seem to be working yet. I get this output:

Incorrect relative times

Can anyone see what's wrong?

可是我不能没有你 2024-10-22 16:41:59

目前 KRL 中没有内置功能来执行此操作。您可能需要编写一个函数或模块来执行此操作,如果/当您这样做时,我很乐意看到它。

There currently isn't built in functionality to do this in KRL. You will probably need to write a function or a module to do this and I would love to see it if/when you do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文