Re: split function
by ikegami (Patriarch) on Jan 27, 2011 at 03:38 UTC
|
I'm confused. What does it mean to translate code that fails to work?
[ Please use <p> at the start of paragraphs and <c>...</c> around computer text (code, data, output). The latter will make your code readable without you having to do any escaping or formatting. ]
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
date "+%H"
date "+%M"
| [reply] [Watch: Dir/Any] [d/l] |
Re: split function
by Anonymous Monk on Jan 27, 2011 at 07:56 UTC
|
#!/usr/bin/perl --
use strict;
use warnings;
use Time::Piece;
my $t = localtime;
print $t, "\n", $t->strftime("%a %b %d %H:%M:%S %Y"),"\n";
print $t->strftime('%H'),"\n";
print $t->strftime('%M'),"\n";
__END__
Wed Jan 26 23:57:47 2011
Wed Jan 26 23:57:47 2011
23
57
See Time::Piece | [reply] [Watch: Dir/Any] [d/l] |
|
Thank you, is there a way of substracting 20 minutes from currentt time. I need to convert tht time format into like 01/26/2011 03:29:00 and pass to the database api but I need to substract 20 minutes from the current time and submit the date in that format.
| [reply] [Watch: Dir/Any] |
|
use DateTime qw( );
my $dt = DateTime->now( time_zone => 'local' );
$dt->subtract( minutes => 20 );
print($dt->strftime("%d/%m/%Y %H:%M:%S"), "\n");
DateTime
| [reply] [Watch: Dir/Any] [d/l] |
|
Sure, visit the Time::Piece documentation, and read all about it
| [reply] [Watch: Dir/Any] |
|
Thank you, this is great!
Is there a way of printing current time in the format:
01/27/2011 20:30:00
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: split function
by rowdog (Curate) on Jan 27, 2011 at 03:23 UTC
|
$ date|awk '{print $4}'|awk -F: '{print $1$2}'|cut -c3-4
19
$ perl -e 'print( (localtime)[1], "\n" )'
19
I don't really understand awk so I might be mistaken.
If you're looking for something other than minutes, consult localtime.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: split function
by bart (Canon) on Jan 27, 2011 at 11:34 UTC
|
If all you want is minutes and seconds of time of day, just look at localtime. You can easily extract minutes and seconds, as integers, from the return value in list context.
In addition, if you want more elaborate formatting, you can also look at strftime in POSIX. You can just pipe the results from localtime into it, like this:
use POSIX qw(strftime);
my $format = "Current time minutes: %M, seconds: %S\n";
print strftime $format, localtime;
| [reply] [Watch: Dir/Any] [d/l] [select] |