summaryrefslogtreecommitdiffstats
path: root/nicedate.pm
blob: b9b06d244b86bf1a8d89ce730f2f4dfc6acc8aea (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use POSIX qw/strftime/;

my @mname = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
             'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
my @dname = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday',
             'Saturday','Sunday');
my @ord = ( "th", "st", "nd", "rd", "th",
            "th", "th", "th", "th", "th",
            "th", "th", "th", "th", "th",
            "th", "th", "th", "th", "th" );
 
my %mnum = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5,
             Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11 );

sub reltime {
    my $then_t  = $_[0];

    my $now_t = time();
    my @now = gmtime( $now_t );
    my @yday = gmtime( $now_t - 86400 );
    my @then = gmtime( $then_t );
    my $diff = $now_t - $then_t;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = @then;

    if ( $diff > 200*86400 ) {
        # another year
        $str = strftime( "%Y-%m-%d", @then );
    }
    elsif ( $diff > 5*86400 ) {
        # not last week
        $str = sprintf( "%d%s %s %02d:%02d", 
                        $mday, $ord[ $mday % 20 ], $mname[$mon],
                        $hour, $min);
    }
    else {
        # last week

        my $day;
        if ($then[3] == $now[3]) {
            $day = "Today";
        }
        elsif ($then[3] == $yday[3]) {
            $day = "Yesterday";
        }
        else {
            $day = $dname[$wday];
        }
        $str = sprintf( "%s %02d:%02d", $day, $hour, $min );
    }
    
    return $str;
}

sub reldate {
    my $then_t  = $_[0];

    my $now_t = time();
    my @now = gmtime( $now_t );
    my @yday = gmtime( $now_t - 86400 );
    my @nday = gmtime( $now_t + 86400 );
    my @then = gmtime( $then_t );
    my $diff = abs($now_t - $then_t);
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = @then;

    if ( $diff > 200*86400 ) {
        # another year
        $str = strftime( "%Y-%m-%d", @then );
    }
    elsif ( $diff > 5*86400 ) {
        # not last week
        $str = sprintf( "%d%s %s", $mday, $ord[ $mday % 20 ], $mname[$mon] );
    }
    elsif ($mday != $now[3]) {
        if ( $mday == $yday[3] ) {
            # yesterday
            $str = "yesterday";
        }
        elsif ( $mday == $nday[3] ) {
            # yesterday
            $str = "tomorrow";
        }
        else {
            # last week
            $str = $dname[$wday];
        }
    }
    else {
        # same day
        $str = "today";
    }
    
    return $str;
}

sub nicedate {
    my $indate = $_[0]; # format YYYY-MM-DD
    my $thisyear = $_[1];

    ($tyear, $tmonth, $tday)=split("-", $indate);

    $nice = sprintf("%d %s", $tday, $mname[$tmonth-1]);

#    if($tyear != $thisyear) {
#        $tyear =~ s/^19//g;
#        $nice="$nice $tyear";
#    }
    return $nice;
}