aboutsummaryrefslogtreecommitdiffstats
path: root/extensions/Mageia/lib/Util.pm
blob: 60447fa28ae25ae9c7d5773c38824b8271d0c184 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::Mageia::Util;

use 5.10.1;
use strict;
use warnings;

use parent qw(Exporter);

our @EXPORT = qw(compare_datetimes);

# This file can be loaded by your extension via 
# "use Bugzilla::Extension::Mageia::Util". You can put functions
# used by your extension in here. (Make sure you also list them in
# @EXPORT.)

# Return -1 if $t1 < $t2, 0 if $t1 == $t2, 1 if $t1 > $t2, undef if a date is invalid.
sub compare_datetimes {
    my ($t1, $t2) = @_;
    my (@time1, @time2);
    if ($t1 =~ /^(\d{4})[\.-](\d{2})[\.-](\d{2})(?: (\d{2}):(\d{2}):(\d{2}))?$/) {
        @time1 = ($1, $2, $3, $4, $5, $6);
    }
    if ($t2 =~ /^(\d{4})[\.-](\d{2})[\.-](\d{2})(?: (\d{2}):(\d{2}):(\d{2}))?$/) {
        @time2 = ($1, $2, $3, $4, $5, $6);
    }
    return undef unless scalar(@time1) && scalar(@time2);

    for my $i (0..5) {
        $t1 = $time1[$i] // 0;
        $t2 = $time2[$i] // 0;
        next if $t1 == $t2;
        return $t1 <=> $t2;
    }
    return 0;
}

1;