summaryrefslogtreecommitdiff
path: root/lib/JWebmail/View/Webmail.pm
blob: 778253df602f4cf19ffd39d73f966364e8b5a7cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package JWebmail::View::Webmail;

use Mojo::Base -base;

use POSIX qw(floor round log);


### template formatting functions

sub print_sizes10 {
    shift;
    my $var = shift || return '0 Byte';

    my $i = floor(((log($var)/log(10))+1e-5) / 3);
    my $expo = $i * 3;

    my @PREFIX;
    $PREFIX[0] = 'Byte';
    $PREFIX[1] = 'kByte';
    $PREFIX[2] = 'MByte';
    $PREFIX[3] = 'GByte';
    $PREFIX[4] = 'TByte';
    $PREFIX[5] = 'PByte';

    return sprintf('%.0f %s', $var / (10**$expo), $PREFIX[$i]);
}

sub print_sizes2 {
    shift;
    my $var = shift || return '0 Byte';

    my $i = floor(((log($var)/log(2))+1e-5) / 10);
    my $expo = $i * 10;
    my %PREFIX = (
        0 => 'Byte',
        1 => 'KiByte',
        2 => 'MiByte',
        3 => 'GiByte',
        4 => 'TiByte',
        5 => 'PiByte',
    );
    my $pref = $PREFIX{$i};
    return round($var / (2**$expo)) . " $pref";
}

my sub dgt { "([[:digit:]]{$_[0]})" }

sub parse_iso_date {
    my ($_self, $in) = @_;

    state $rx = do { my $re = dgt(4).'-'.dgt(2).'-'.dgt(2).'T'.dgt(2).':'.dgt(2).':'.dgt(2); qr/$re/a };

    my @d = $in =~ /$rx/;
    if (@d != 6) {
        # TODO
        warn "issue when parsing date '$in'";
    }
    return {
        year  => $d[0],
        month => $d[1],
        mday  => $d[2],
        hour  => $d[3],
        min   => $d[4],
        sec   => $d[5],
    };
}

1

__END__

=head1 VIEW METHODS

=head2 print_sizes10

A helper for templates used to format byte sizes.

  $app->helper(print_sizes10 => sub { shift; JWebmail::Plugin::Helper::print_sizes10(@_) });

  %= print_sizes10 12345 # => 12 kB

=head2 print_sizes2

A helper for templates used to format byte sizes.

  %= print_sizes10 12345 # => 12 KiB

This is not registered by default.

=head2 parse_iso_date