Showing the current time with Dancer


In the first example we saw how to show a simple page that does not change between executions.

We now go a small step further and show a page that will show a different output every time we access it.

Still nothing fancy, just showing the current date and time.

We could of course use the built-in time function or the also built-in localtime function, but I wanted to show something a bit nicer. So we are using the DateTime module to generate an object representing the current timestamp and then we use the strftime method to create a nice-looking timestamp.

Dancer-wise we don't do much, we just return the resulting string.


examples/dancer/show_time/app.psgi
package App;
use Dancer2;
use DateTime;

get '/' => sub {
    my $dt = DateTime->now;
    return $dt->strftime( '%Y-%m-%d %H:%M:%S' );
};

App->to_app;