public
Description: My personal script stash
Homepage: http://github.com/melo/scripts
Clone URL: git://github.com/melo/scripts.git
Search Repo:
commit  d7e1261d9b05a79c8b221dafd1aa6dff200999bd
tree    cad1647a2815ff333956ce4dfa84b2aab11096c0
parent  f9e111a6b6d511d6123c223237ebc5f833612a5e
scripts / bin / x-datetime-converter
100755 122 lines (105 sloc) 2.634 kb
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/perl -w
 
use strict;
use warnings;
 
require_module('DateTime');
 
# Format classes to use for display
my @formats = qw( DateParse MySQL HTTP Mail RSS );
 
# Check for availability of format classes
my @available;
FORMAT_CLASS:
foreach my $format (@formats) {
  my $class = "DateTime::Format::$format";
 
  next FORMAT_CLASS unless optional_module($class);
  
  push @available, {
    name => $format,
    class => $class,
    can_parse => $class->can('parse_datetime'),
    can_format => $class->can('format_datetime'),
  };
}
 
# read dates
while (my $t = <>) {
  chomp($t);
  my $dt;
 
  if ($t =~ /^\d+$/) {
    $dt = DateTime->from_epoch( epoch => $t );
  }
  elsif ($t =~ /^(\d\d\d\d)[-\/](\d+)[-\/](\d+)$/) {
    $dt = DateTime->new(
        year => $1,
        month => $2,
        day => $3,
    );
  }
  elsif ($t =~ /^(\d+)[-\/](\d+)[-\/](\d\d\d\d)$/) {
    $dt = DateTime->new(
        year => $3,
        month => $2,
        day => $1,
    );
  }
  elsif ($t =~ /^(\d\d\d\d)[-\/](\d+)[-\/](\d+) (\d+):(\d+):(\d+)$/) {
    $dt = DateTime->new(
        year => $1,
        month => $2,
        day => $3,
        hour => $4,
        minute => $5,
        second => $6,
    );
  }
  elsif ($t =~ /^(\d+)[-\/](\d+)[-\/](\d\d\d\d) (\d+):(\d+):(\d+)$/) {
    $dt = DateTime->new(
        year => $3,
        month => $2,
        day => $1,
        hour => $4,
        minute => $5,
        second => $6,
    );
  }
  elsif ($t =~ /^(\d+):(\d+):(\d+)$/) {
    $dt = DateTime->new(
        hour => $1,
        minute => $2,
        second => $3,
    );
  }
  else {
    foreach my $helper (@available) {
      next unless $helper->{can_parse};
      eval { $dt = $helper->{class}->parse_datetime($t) };
      last if $dt;
    }
  }
 
  if ($dt) {
    print " $t -- $dt ", $dt->epoch, " (hex ", sprintf('%0.4x', $dt->epoch) ,")\n";
    foreach my $helper (@available) {
      next unless $helper->{can_format};
      print " $t -- format $helper->{name} is: ", $helper->{class}->format_datetime($dt), "\n";
    }
  }
  else {
    print " $t -- could not parse this\n"
  }
}
 
 
#####################################
# Deal with required/optional modules
 
sub require_module {
  my $module = shift;
  
  eval "require $module";
  if (my $e = $@) {
    print STDERR "FATAL: $0 requires the Perl module '$module'.\n\n";
    print STDERR "You can install it with:\n\n";
    print STDERR " cpan $module\n\n";
    exit(1);
  }
  $module->import(@_);
}
 
sub optional_module {
  my $module = shift;
  
  eval "require $module";
  return 0 if $@;
  
  $module->import(@_);
  return 1;
}