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
|
#!/usr/bin/env perl6
# FIXME: stdin support with lines()
# FIXME: --help and -h support
sub from-timestamp(Int \timestamp) {
sub formatter($_) {
sprintf '%04d-%02d-%02d %02d:%02d:%02d',
.year, .month, .day,
.hour, .minute, .second,
}
given DateTime.new(+timestamp, :&formatter) {
when .Date.DateTime == $_ { return .Date }
default { return $_ }
}
}
sub from-date-string(Str $date, Str $time?) {
my $d = Date.new($date);
if $time {
my ( $hour, $minute, $second ) = $time.split(':');
return DateTime.new(date => $d, :$hour, :$minute, :$second);
} else {
return $d.DateTime;
}
}
# FIXME: add test
multi sub convert(Int \timestamp) {
from-timestamp(+timestamp);
}
# FIXME: add test
multi sub convert(Str $date where { try Date.new($_) }, Str $time?) {
from-date-string($date, $time).posix;
}
# FIXME: add test
#| Convert timestamp to ISO date
multi sub MAIN(Int \timestamp) {
say convert(+timestamp);
}
# FIXME: add test
#| Convert ISO date to timestamp
multi sub MAIN(Str $date where { try Date.new($_) }, Str $time?) {
say convert($date, $time);
}
# multi sub MAIN() {
# for lines() -> $line {
# }
# }
#| Run internal tests
multi sub MAIN('test') is hidden-from-USAGE {
use Test;
plan 2;
subtest 'timestamp', {
plan 2;
is-deeply from-timestamp(1450915200), Date.new('2015-12-24'),
'Date';;
my $dt = from-timestamp(1450915201);
is $dt, "2015-12-24 00:00:01",
'DateTime with string formatting';
};
subtest 'from-date-string', {
plan 2;
is from-date-string('2015-12-24').posix, 1450915200,
'one argument';
is from-date-string('2015-12-24', '00:00:01').posix,
1450915201,
'two arguments';
};
}
|