Guys, I am a beginner in Perl, and I have a small query.
I have written a script which selects a line from a file and extracts the date part of it. This date, I want to Parse (in anyway) to obtain the Epoch time. The error which I get and the code which I have written is as follows. I have tried a number of ways to parse the date.
If I give ParseDate("
"), it works, but if I give the line which I read from the file, it does not work. Please help.
Error which I get:
The 'year' parameter (undef) to DateTime::new was an 'undef', which is
+ not one of the allowed types: scalar
at /usr/local/lib/perl/5.10.1/DateTime.pm line 200
DateTime::new(undef, 'year', undef, 'month', 1, 'day', 1, 'hour',
+1, ...) called at smsDone line 60
The Code which I have written
$file_name = "sms.vmg";
tie my @array, 'Tie::File', $file_name or die("Unable to open file \"$
+file_name\": $!\n");
my $year = substr $array[13], 22, 8;
my $month = substr $array[13], 16, 2;
my $date = substr $array[13], 10, 4;
my $hour = substr $array[13], 32, 4;
my $minutes = substr $array[13], 38, 4;
my $seconds = substr $array[13], 44, 4;
$dt = DateTime->new(
year=>$year,
month=>$month,
day=>$date,
hour=>$hour,
minute=>$minutes,
second=>$seconds,
nanosecond=>0, time_zone => 'Asia/Calcutta');
$epoch_time = $dt->epoch * 1000;
print "\nEpoch Time: ".$epoch_time;
Update: I missed adding ... the date is in the format "dd.mm.yyyy hh:mm:ss". For example "21.06.2010 07:06:11"
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.