aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2018-11-19 22:13:33 -0200
committerEuAndreh <eu@euandre.org>2018-11-19 22:13:33 -0200
commitf18cf58b1c5d579d765b9854f1857a4e381560db (patch)
tree090044f8a2c164ad14849cd9ad64d94c98fc24ff
parentRun dotfiles test under =,r=. (diff)
downloaddotfiles-f18cf58b1c5d579d765b9854f1857a4e381560db.tar.gz
dotfiles-f18cf58b1c5d579d765b9854f1857a4e381560db.tar.xz
Add initial implementation of =autotime=.
Missing: - a few more tests; - stdin support; - better CLI.
-rwxr-xr-xscripts/autotime76
1 files changed, 76 insertions, 0 deletions
diff --git a/scripts/autotime b/scripts/autotime
new file mode 100755
index 0000000..fb01392
--- /dev/null
+++ b/scripts/autotime
@@ -0,0 +1,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';
+ };
+}