Home > Archive > PERL Beginners > December 2007 > Date::manip query
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| im using Date::Manip to convert dates and times eg 2007:08:02 12:23
to allow me to sort them, which it does .
but I cant see how to get the number back into a human -readable
format
print scalar localtime($var{STARTTIME}); prints the long string . is
there a better way to get just the bits I want , or do I have to
parse the string I get from print scalar localtime output ?
| |
| Rob Dixon 2007-12-16, 7:03 pm |
| pauld wrote:
>
> im using Date::Manip to convert dates and times eg 2007:08:02 12:23
> to allow me to sort them, which it does .
> but I cant see how to get the number back into a human -readable
> format
>
> print scalar localtime($var{STARTTIME}); prints the long string . is
> there a better way to get just the bits I want , or do I have to
> parse the string I get from print scalar localtime output ?
Have you read the documentation for Date::Manip?
The UnixDate() function will do what you want. Something like
print UnixDate($date, '%Y:%m:%d %H:%M');
for instance, which will reproduce the date in the format you have
shown.
HTH,
Rob
| |
| John W . Krahn 2007-12-16, 7:03 pm |
| On Sunday 16 December 2007 04:31, pauld wrote:
>
> im using Date::Manip to convert dates and times eg 2007:08:02 12:23
> to allow me to sort them, which it does .
> but I cant see how to get the number back into a human -readable
> format
Use the UnixDate() function that comes with Date::Manip.
> print scalar localtime($var{STARTTIME}); prints the long string . is
> there a better way to get just the bits I want , or do I have to
> parse the string I get from print scalar localtime output ?
Use localtime in list context instead.
perldoc -f localtime
John
--
use Perl;
program
fulfillment
| |
|
| im importing data from an excel spreadsheet into an array of hashes.
the date is initially converted using Date::Format::Excel.
for this bit
{START} = unix start time .{START_DS} = string that I use to convert
to unixtime with
my $var=0;my $va_length=@daylistsorted;
while ($var<$va_length)
{
print "${$daylistsorted[$var]}{TH} ";
print 'from ';
print ${$daylistsorted[$var]}{START};
print ' to '.${$daylistsorted[$var]}{END_DS};
print " duration ";print int((${$daylistsorted[$var]}{END}-$
{$daylistsorted[$var]}{START})/60);
if (exists( ${$daylistsorted[$var+1]}{TH} ) )
{
print "\tinterval to next start "; print int ((${$daylistsorted[$var
+1]}{START}-${$daylistsorted[$var]}{END})/60);print " \n";
}
$var++;
} }
Sat 04-08-2007
=========================
1 from 1186220100 to 2007:08:04 10:33 duration 58 interval to next
start 34
4 from 1186225620 to 2007:08:04 13:29 duration 142 interval to
next start 26
and when i change it to
#print ${$daylistsorted[$var]}{START};
print UnixDate(${$daylistsorted[$var]}{START},
'%Y:%m:%d %H:%M');
I get this
Sat 04-08-2007
=========================
1 from to 2007:08:04 10:33 duration 58 interval to next start
34
4 from to 2007:08:04 13:29 duration 142 interval to next start
26
with both dates as strings
Sat 04-08-2007
=========================
1 from 2007:08:04 09:35 to 2007:08:04 10:33 duration 58 interval
to next start 34
4 from 2007:08:04 11:07 to 2007:08:04 13:29 duration 142 interval
to next start 26
| |
| davidfilmer@gmail.com 2007-12-17, 10:05 pm |
| On Dec 17, 3:22 am, pdcoo...@blueyonder.co.uk (Pauld) wrote:
> my $var=0;my $va_length=@daylistsorted;
> while ($var<$va_length)
> {
> print "${$daylistsorted[$var]}{TH} ";
> print 'from ';
> print ${$daylistsorted[$var]}{START};
> print ' to '.${$daylistsorted[$var]}{END_DS};
> print " duration ";print int((${$daylistsorted[$var]}{END}-$
> {$daylistsorted[$var]}{START})/60);
It's unusual in Perl to need to access an array element by its index
number. This is one of those times, though, when it is useful to use
an index because you need to p ahead at the next item in the
array. But you only need the index for the next item, not for the
current item, so you can clean up things a bit with something like
this (untested, and posted without much effort to parse or understand
the objective of the code, and using printf instead of a bunch of
concat'ed strings):
my $index = 0;
foreach my $day( @daylistsorted ) {
printf (
"%s from $s to %s duration %s %s\n",
$day{'TH'},
UnixDate($day{'START'}, '%Y:%m:%d %H:%M'),
UnixDate($day{'END_DS'}, '%Y:%m:%d %H:%M'),
int(($day{END} - $day{START})/60);
(exists( ${$daylistsorted[$index+1]}{TH} ) )
? "\tinterval to next start "
.int (( ${$daylistsorted[$index+1]}{START}
-$day{END} )/60)
: ''
);
$index++;
}
--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)
| |
| John W . Krahn 2007-12-18, 4:01 am |
| On Monday 17 December 2007 15:40, davidfilmer@gmail.com wrote:
>
> On Dec 17, 3:22 am, pdcoo...@blueyonder.co.uk (Pauld) wrote:
>
> It's unusual in Perl to need to access an array element by its index
> number. This is one of those times, though, when it is useful to use
> an index because you need to p ahead at the next item in the
> array. But you only need the index for the next item, not for the
> current item, so you can clean up things a bit with something like
> this (untested, and posted without much effort to parse or understand
> the objective of the code, and using printf instead of a bunch of
> concat'ed strings):
>
> my $index = 0;
> foreach my $day( @daylistsorted ) {
The way it is *usually* done is:
foreach my $index ( 0 .. $#daylistsorted ) {
> printf (
> "%s from $s to %s duration %s %s\n",
> $day{'TH'},
An array element can hold a hash reference but not a hash itself:
$day->{ TH },
> UnixDate($day{'START'}, '%Y:%m:%d %H:%M'),
$day->{ START }
> UnixDate($day{'END_DS'}, '%Y:%m:%d %H:%M'),
$day->{ END_DS }
> int(($day{END} - $day{START})/60);
$day->{ END } - $day->{ START }
> (exists( ${$daylistsorted[$index+1]}{TH} ) )
> ? "\tinterval to next start "
> .int (( ${$daylistsorted[$index+1]}{START}
> -$day{END} )/60)
$day->{ END }
>
> : ''
>
> );
> $index++;
> }
John
--
use Perl;
program
fulfillment
| |
| Chas. Owens 2007-12-18, 4:01 am |
| On Dec 18, 2007 1:05 AM, John W. Krahn <krahnj@telus.net> wrote:
snip[color=darkred]
snip
You missed the usage of $s instead of %s. I always get bitten by that.
| |
| Rob Dixon 2007-12-18, 7:00 pm |
| pauld wrote:
>
> im importing data from an excel spreadsheet into an array of hashes.
> the date is initially converted using Date::Format::Excel.
>
> for this bit
>
> {START} = unix start time .{START_DS} = string that I use to convert
> to unixtime with
>
> my $var=0;my $va_length=@daylistsorted;
> while ($var<$va_length)
> {
> print "${$daylistsorted[$var]}{TH} ";
> print 'from ';
> print ${$daylistsorted[$var]}{START};
> print ' to '.${$daylistsorted[$var]}{END_DS};
> print " duration ";print int((${$daylistsorted[$var]}{END}-$
> {$daylistsorted[$var]}{START})/60);
>
> if (exists( ${$daylistsorted[$var+1]}{TH} ) )
> {
> print "\tinterval to next start "; print int ((${$daylistsorted[$var
> +1]}{START}-${$daylistsorted[$var]}{END})/60);print " \n";
> }
> $var++;
> } }
>
> Sat 04-08-2007
> =========================
> 1 from 1186220100 to 2007:08:04 10:33 duration 58 interval to next
> start 34
> 4 from 1186225620 to 2007:08:04 13:29 duration 142 interval to
> next start 26
>
> and when i change it to
> #print ${$daylistsorted[$var]}{START};
> print UnixDate(${$daylistsorted[$var]}{START},
'%Y:%m:%d %H:%M');
>
> I get this
>
> Sat 04-08-2007
> =========================
> 1 from to 2007:08:04 10:33 duration 58 interval to next start
> 34
> 4 from to 2007:08:04 13:29 duration 142 interval to next start
> 26
>
>
> with both dates as strings
>
>
> Sat 04-08-2007
> =========================
> 1 from 2007:08:04 09:35 to 2007:08:04 10:33 duration 58 interval
> to next start 34
> 4 from 2007:08:04 11:07 to 2007:08:04 13:29 duration 142 interval
> to next start 26
Your START and END fields appear to be seconds since epoch, whereas the
END_DS field is in the form '%Y:%m:%d %H:%M' as you described in your
earlier post. Neither of these are anything to do with the Date::Manip
module and consequently the answers you have had were irrelevant.
To display a time value such as this in a custom format you can use the
strftime() function from the POSIX module. The program below exemplifies
this.
However, unless I am misunderstanding you you already have the START
time as a string in START_DS. Isn't this sufficient?
HTH,
Rob
use strict;
use warnings;
use POSIX qw(strftime);
my $date_time = 1186220100;
print strftime('%Y:%m:%d %H:%M', localtime($date_time));
**OUTPUT**
2007:08:04 10:35
| |
|
| the END_DS field is the date field that I want - but as I couldnt
get it back from the seconds since epoch field I included it.
IMHO it would be tideir to just use the (numerical) date-seconds and
convert it back as necessary . i used the Date::Manip function
Date_SecsSince1970($m,$d,$y,$h,$mn,$s); to get the date& time into a
format I can sort it by. i was wanting to use Date::manip to extract
the bits i want ( HH:MM) for the final display. i can do it by
splitting the text formatted value (START_DS) but i thought it would
be neater ( and i'd learnt something ) by doing it using the module
| |
| Gunnar Hjalmarsson 2007-12-18, 7:00 pm |
| pauld wrote:
> im using Date::Manip to convert dates and times eg 2007:08:02 12:23
> to allow me to sort them,
Why are you doing that?
C:\home>type test.pl
@dates = ( '2007:08:02 12:23', '2007:10:21 04:40',
'2007:06:05 16:08', '2007:09:11 22:20', );
print "$_\n" for sort @dates;
C:\home>test.pl
2007:06:05 16:08
2007:08:02 12:23
2007:09:11 22:20
2007:10:21 04:40
C:\home>
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Pauld 2007-12-19, 10:03 pm |
| im sorting it on a key of the hash
my @daylistsorted = sort { $$a{'START_DS'} <=> $$b{'START_DS'} }
@daylist;
generates a
Argument "2007:09:30 13:41" isn't numeric in numeric comparison (<=> )
at ./518573
error
my @daylistsorted = sort { $$a{'START'} <=> $$b{'START'} } @daylist;
works
| |
| Tom Phoenix 2007-12-19, 10:03 pm |
| On 12/19/07, pauld <pdcooper@blueyonder.co.uk> wrote:
> im sorting it on a key of the hash
> my @daylistsorted = sort { $$a{'START_DS'} <=> $$b{'START_DS'} }
> @daylist;
> generates a
> Argument "2007:09:30 13:41" isn't numeric in numeric comparison (<=> )
> at ./518573
Have you tried using a string comparison when you want to compare
strings? In Perl, that's the cmp operator.
http://perldoc.perl.org/perlop.html
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
| |
| Gunnar Hjalmarsson 2007-12-19, 10:03 pm |
| pauld wrote:
> Gunnar Hjalmarsson wrote:
>
> im sorting it on a key of the hash
> my @daylistsorted = sort { $$a{'START_DS'} <=> $$b{'START_DS'} }
> @daylist;
> generates a
> Argument "2007:09:30 13:41" isn't numeric in numeric comparison (<=> )
> at ./518573
My point is that the format "yyyy:mm:dd hh:mm" is sortable without
conversion. However, you need to sort lexically, i.e. use the cmp
operator instead of the <=> operator.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
|
|
|
|