• このエントリーをはてなブックマークに追加


メモ

テスト用のデータを準備するのにちょっと必要になったので.

こんな感じ

% perl randtime.pl --date=2014-03-01 -n 10
2014-03-01T01:29:36
2014-03-01T20:45:35
2014-03-01T08:47:38
2014-03-01T19:13:54
2014-03-01T16:20:50
2014-03-01T05:28:13
2014-03-01T12:06:45
2014-03-01T00:20:46
2014-03-01T08:15:11
2014-03-01T03:55:46
% perl randtime.pl --range=2014-03-01T10:00:00..2014-03-01T19:00:00 -n 30 | sort
2014-03-01T10:09:55
2014-03-01T10:12:52
2014-03-01T10:28:46
2014-03-01T11:03:32
2014-03-01T11:03:38
2014-03-01T11:04:24
2014-03-01T11:20:57
2014-03-01T11:24:16
2014-03-01T12:16:52
2014-03-01T12:18:29
2014-03-01T12:33:55
2014-03-01T12:56:04
2014-03-01T13:40:05
2014-03-01T13:52:12
2014-03-01T14:14:37
2014-03-01T14:35:33
2014-03-01T14:44:42
2014-03-01T15:19:40
2014-03-01T15:51:08
2014-03-01T16:43:50
2014-03-01T17:00:29
2014-03-01T17:01:16
2014-03-01T17:01:18
2014-03-01T17:09:50
2014-03-01T17:11:46
2014-03-01T17:28:47
2014-03-01T17:41:21
2014-03-01T17:59:01
2014-03-01T18:21:27
2014-03-01T18:35:03

スクリプト

=head1 NAME
 
randtime.pl - prints datetime randomly
 
=head1 SYNOPSIS
 
  randtime.pl <options>
 
  # randtime.pl -n 20
  # randtime.pl --date=2014-03-01 -n 20
  # randtime.pl --range=2014-01-01T00:00:00..2014-01-10T00:00:00 -n 50
 
=head1 OPTIONS
 
=over 4
 
=item --date=$date
 
=item --range=${datetime1}..${datetime2}
 
=item --n=$n
 
=item --help
 
=back
 
=cut
use 5.18.0;
use warnings;
use utf8;
use Getopt::Long qw/:config posix_default no_ignore_case bundling/;
use Time::Piece ();
use Pod::Usage;
 
my %opts = (
    date  => Time::Piece->localtime->date,
    range => undef,
    n     => 10,
    help  => 0,
);
GetOptions(
    'date=s'  => \$opts{date},
    'range=s' => \$opts{range},
    'n=i'     => \$opts{n},
    'h|help'  => \$opts{help},
) or pod2usage( 1 );
for ( keys %opts ) { delete $opts{$_}  unless defined $opts{$_} }
 
pod2usage( 0 )  if $opts{help};
 
 
sub t {
    my ($t) = @_;
    return ( $t =~ /^\d{10}$/ )
        ? Time::Piece::localtime( $t )
        : Time::Piece::localtime->strptime( $t, '%Y-%m-%dT%H:%M:%S' );
}
 
sub list {
    my ( $t1, $t2, $n) = @_;
    my @t;
    for my $i ( 1 .. $n ) {
        my $r = rand();
        my $t = int( $t1 + ($t2 - $t1) * $r );
        push @t, t( $t )->datetime;
    }
    return wantarray ? @t : \@t;
}
 
sub main {
    my %args = @_;
    my @r = exists $args{range}
        ? ( split /\.\./, $args{range} )
        : ( map "$args{date}T$_", '00:00:00', '23:59:59' );
    my ($t1, $t2) = map t( $_ )->epoch, @r;
 
    say $_  for list( $t1, $t2, $args{n} );
 
    return 0;
}
 
main( %opts );