Skip to content

Commit

Permalink
Changed the log timestamp to RFC3339
Browse files Browse the repository at this point in the history
  Running the benchmark below does not show any differences:

  #!/bin/sh
  export MOJO_LOG_LEVEL=debug
  MOJO_PORT=${2:-3456}
  perl -Ilib examples/hello.pl daemon -l http://*:$MOJO_PORT -m production 2>stderr.log &
  MOJO_PID=$!
  sleep 1
  ../wrk/wrk -c 100 -d ${1:-10} http://localhost:$MOJO_PORT
  kill $MOJO_PID

 # "7075086" / v6.46:

 # This commit:
  • Loading branch information
Jan Henning Thorsen committed Feb 19, 2016
1 parent 7075086 commit 230e0ed
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
10 changes: 2 additions & 8 deletions lib/Mojo/Date.pm
Expand Up @@ -2,6 +2,7 @@ package Mojo::Date;
use Mojo::Base -base;
use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;

use Mojo::Util 'epoch_to_datetime';
use Time::Local 'timegm';

has epoch => sub {time};
Expand Down Expand Up @@ -52,14 +53,7 @@ sub parse {
(defined $epoch && ($epoch += $offset) >= 0) ? $epoch : undef);
}

sub to_datetime {

# RFC 3339 (1994-11-06T08:49:37Z)
my ($s, $m, $h, $day, $month, $year) = gmtime(my $epoch = shift->epoch);
my $str = sprintf '%04d-%02d-%02dT%02d:%02d:%02d', $year + 1900, $month + 1,
$day, $h, $m, $s;
return $str . ($epoch =~ /(\.\d+)$/ ? "$1Z" : 'Z');
}
sub to_datetime { epoch_to_datetime(shift->epoch) }

sub to_string {

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Log.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use Fcntl ':flock';
use Mojo::Util qw(deprecated encode monkey_patch);
use Mojo::Util qw(epoch_to_datetime deprecated encode monkey_patch);

has format => sub { \&_format };
has handle => sub {
Expand Down Expand Up @@ -56,7 +56,7 @@ sub new {
sub warn { shift->_log(warn => @_) }

sub _format {
'[' . localtime(shift) . '] [' . shift() . '] ' . join "\n", @_, '';
'[' . epoch_to_datetime(shift) . '] [' . shift() . '] ' . join "\n", @_, '';
}

sub _log { shift->emit('message', shift, @_) }
Expand Down
28 changes: 23 additions & 5 deletions lib/Mojo/Util.pm
Expand Up @@ -53,11 +53,12 @@ my %CACHE;

our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode deprecated dumper encode hmac_sha1_sum html_unescape md5_bytes),
qw(md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_cookie_header),
qw(split_header spurt squish steady_time tablify term_escape trim unindent),
qw(unquote url_escape url_unescape xml_escape xor_encode)
qw(decode deprecated dumper encode epoch_to_datetime hmac_sha1_sum),
qw(html_unescape md5_bytes md5_sum monkey_patch punycode_decode),
qw(punycode_encode quote secure_compare sha1_bytes sha1_sum slurp),
qw(split_cookie_header split_header spurt squish steady_time tablify),
qw(term_escape trim unindent unquote url_escape url_unescape xml_escape),
qw(xor_encode)
);

# DEPRECATED in Clinking Beer Mugs!
Expand Down Expand Up @@ -113,6 +114,12 @@ sub dumper {

sub encode { _encoding($_[0])->encode("$_[1]") }

sub epoch_to_datetime {
my ($s, $m, $h, $day, $month, $year) = gmtime(my $epoch = shift);
return sprintf '%04d-%02d-%02dT%02d:%02d:%02d%s', $year + 1900, $month + 1,
$day, $h, $m, $s, ($epoch && $epoch =~ /(\.\d+)$/ ? "$1Z" : 'Z');
}

sub hmac_sha1_sum { hmac_sha1_hex @_ }

sub html_unescape {
Expand Down Expand Up @@ -576,6 +583,17 @@ Dump a Perl data structure with L<Data::Dumper>.
Encode characters to bytes.
=head2 epoch_to_datetime
my $str = epoch_to_datetime($epoch);
my $str = epoch_to_datetime;
Converts an epoch time to a RFC 3339 datetime string. The epoch timestamp
defaults to "now".
# "1994-11-06T08:49:37Z".
epoch_to_datetime 784111777;
=head2 hmac_sha1_sum
my $checksum = hmac_sha1_sum $bytes, 'passw0rd';
Expand Down
19 changes: 14 additions & 5 deletions t/mojo/util.t
Expand Up @@ -12,11 +12,12 @@ use Mojo::DeprecationTest;

use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode dumper encode hmac_sha1_sum html_unescape md5_bytes md5_sum),
qw(monkey_patch punycode_decode punycode_encode quote secure_compare),
qw(secure_compare sha1_bytes sha1_sum slurp split_cookie_header),
qw(split_header spurt squish steady_time tablify term_escape trim unindent),
qw(unquote url_escape url_unescape xml_escape xor_encode);
qw(decode deprecated dumper encode epoch_to_datetime hmac_sha1_sum),
qw(html_unescape md5_bytes md5_sum monkey_patch punycode_decode),
qw(punycode_encode quote secure_compare sha1_bytes sha1_sum slurp),
qw(split_cookie_header split_header spurt squish steady_time tablify),
qw(term_escape trim unindent unquote url_escape url_unescape xml_escape),
qw(xor_encode);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -164,6 +165,14 @@ is decode('does_not_exist', ''), undef, 'decoding with invalid encoding worked';
eval { encode('does_not_exist', '') };
like $@, qr/Unknown encoding 'does_not_exist'/, 'right error';

# epoch_to_datetime
is epoch_to_datetime(1408567500), '2014-08-20T20:45:00Z',
'epoch_to_datetime 1408567500';
like epoch_to_datetime(Time::HiRes::time),
qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z$/, 'epoch_to_datetime hires';
like epoch_to_datetime(), qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/,
'epoch_to_datetime now';

# url_escape
is url_escape('business;23'), 'business%3B23', 'right URL escaped result';

Expand Down

0 comments on commit 230e0ed

Please sign in to comment.