/
convert_date.tag
66 lines (61 loc) · 1.42 KB
/
convert_date.tag
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
UserTag convert-date Order days
UserTag convert-date PosNumber 1
UserTag convert-date addAttr
UserTag convert-date AttrAlias fmt format
UserTag convert-date HasEndTag
UserTag convert-date Interpolate
UserTag convert-date Routine <<EOR
sub {
my ($days, $opt, $text) = @_;
my @t;
if(! ref $opt) {
my $raw = $opt ? 1 : 0;
$opt = {};
$opt->{raw} = 1 if $raw;
}
my $fmt = $opt->{format} || '';
if($text =~ /^(\d\d\d\d)-(\d?\d)-(\d?\d)$/) {
$t[5] = $1 - 1900;
$t[4] = $2 - 1;
$t[3] = $3;
}
elsif($text =~ /\d/) {
$text =~ s/\D//g;
$text =~ /(\d\d\d\d)(\d\d)(\d\d)(?:(\d\d)(\d\d))?/;
$t[2] = $4 || undef;
$t[1] = $5 || undef;
$t[3] = $3;
$t[4] = $2 - 1;
$t[5] = $1;
$t[5] -= 1900;
}
else {
my $now = time();
if ($days) {
$now += $days * 86400;
}
@t = localtime($now);
}
if (defined $opt->{raw} and Vend::Util::is_yes($opt->{raw})) {
$fmt = $t[2] && $text ? '%Y%m%d%H%M' : '%Y%m%d';
}
if (! $fmt) {
if ($t[2]) {
$fmt = '%d-%b-%Y %I:%M%p';
} else {
$fmt = '%d-%b-%Y';
}
}
my ($current, $out);
if ($Scratch->{mv_locale}) {
$current = POSIX::setlocale(&POSIX::LC_TIME);
POSIX::setlocale(&POSIX::LC_TIME, $Scratch->{mv_locale});
$out = POSIX::strftime($fmt, @t);
POSIX::setlocale(&POSIX::LC_TIME, $current);
} else {
$out = POSIX::strftime($fmt, @t);
}
$out =~ s/\b0(\d)\b/$1/g if $opt->{zerofix};
return $out;
}
EOR