public
Description: My personal script stash
Homepage: http://github.com/melo/scripts
Clone URL: git://github.com/melo/scripts.git
Search Repo:
Add current version of the x-datetime-converter script

Signed-off-by: Pedro Melo <melo@simplicidade.org>
melo (author)
Sat May 03 01:44:48 -0700 2008
commit  1d964767dead5655db56d4ebfc15bf4843d0137e
tree    0a226e281d332e8e7b4c1688926da1146a9dc105
parent  7186756985666d4709bfba61795418c44a9509b7
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,94 @@
0
+#!/usr/bin/perl -w
0
+
0
+use strict;
0
+use DateTime;
0
+
0
+# Format classes to use for display
0
+my @formats = qw( DateParse MySQL HTTP Mail RSS );
0
+
0
+# Check for availability of format classes
0
+my @available;
0
+FORMAT_CLASS:
0
+foreach my $format (@formats) {
0
+ my $class = "DateTime::Format::$format";
0
+
0
+ eval "require $class;";
0
+ next FORMAT_CLASS if $@;
0
+
0
+ push @available, {
0
+ name => $format,
0
+ class => $class,
0
+ can_parse => $class->can('parse_datetime'),
0
+ can_format => $class->can('format_datetime'),
0
+ };
0
+}
0
+
0
+# read dates
0
+while (my $t = <>) {
0
+ chomp($t);
0
+ my $dt;
0
+
0
+ if ($t =~ /^\d+$/) {
0
+ $dt = DateTime->from_epoch( epoch => $_ );
0
+ }
0
+ elsif ($t =~ /^(\d\d\d\d)[-\/](\d+)[-\/](\d+)$/) {
0
+ $dt = DateTime->new(
0
+ year => $1,
0
+ month => $2,
0
+ day => $3,
0
+ );
0
+ }
0
+ elsif ($t =~ /^(\d+)[-\/](\d+)[-\/](\d\d\d\d)$/) {
0
+ $dt = DateTime->new(
0
+ year => $3,
0
+ month => $2,
0
+ day => $1,
0
+ );
0
+ }
0
+ elsif ($t =~ /^(\d\d\d\d)[-\/](\d+)[-\/](\d+) (\d+):(\d+):(\d+)$/) {
0
+ $dt = DateTime->new(
0
+ year => $1,
0
+ month => $2,
0
+ day => $3,
0
+ hour => $4,
0
+ minute => $5,
0
+ second => $6,
0
+ );
0
+ }
0
+ elsif ($t =~ /^(\d+)[-\/](\d+)[-\/](\d\d\d\d) (\d+):(\d+):(\d+)$/) {
0
+ $dt = DateTime->new(
0
+ year => $3,
0
+ month => $2,
0
+ day => $1,
0
+ hour => $4,
0
+ minute => $5,
0
+ second => $6,
0
+ );
0
+ }
0
+ elsif ($t =~ /^(\d+):(\d+):(\d+)$/) {
0
+ $dt = DateTime->new(
0
+ hour => $1,
0
+ minute => $2,
0
+ second => $3,
0
+ );
0
+ }
0
+ else {
0
+ foreach my $helper (@available) {
0
+ next unless $helper->{can_parse};
0
+ eval { $dt = $helper->{class}->parse_datetime($t) };
0
+ last if $dt;
0
+ }
0
+ }
0
+
0
+ if ($dt) {
0
+ print " $t -- $dt ", $dt->epoch, " (hex ", sprintf('%0.4x', $dt->epoch) ,")\n";
0
+ foreach my $helper (@available) {
0
+ next unless $helper->{can_format};
0
+ print " $t -- format $helper->{name} is: ", $helper->{class}->format_datetime($dt), "\n";
0
+ }
0
+ }
0
+ else {
0
+ print " $t -- could not parse this\n"
0
+ }
0
+}

Comments

    No one has commented yet.