开发者

Relative formatting for timestamps

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 a开发者_StackOverflowt 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)?


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:

Relative formatting for timestamps

Can anyone see what's wrong?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜