Relative formatting of timestamps

I wrote the Office 462 Office Hours app in the last two days. the most recent iteration tells the user when the next block of time will be in the watch office. Now he just formats it as "Thursday (February 3) at 15:00." I would like it to be a little smarter, and say something like "this afternoon at three in the morning" or "tomorrow at 10 in the morning."

This is similar to Twitter timestamps on tweets (it says “3 minutes ago” or “23 hours ago”, except that it lists the date). In my case, however, it will be the other way around, since we are dealing with future tenses.

In principle, he should be smart enough to know that this event is today or tomorrow. In addition, I just want to display the date and day of the week.

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

+3
source share
2 answers

These are the functions that 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");
};

That should do the trick. I am testing this rule now:

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, it does not seem to be working yet. I get this output:

Incorrect relative times

Can anyone see what is wrong?

+2
source

There are currently no built-in functions in KRL for this. You will probably need to write a function or module to do this, and I would be pleased to see this if / when you do it.

+1
source

Source: https://habr.com/ru/post/1790054/


All Articles