Um, why not just call it in both contexts? Just get the current time using time(), then pass it to localtime:
my $time = time;
my $ctime = localtime($time);
my @timeArray = localtime($time);
Or, see the POSIX module's strftime() time formatting routine that would allow you to take @timeArray above and format it however you wish. | [reply] [d/l] [select] |
A warning on doing this kind of thing. Notice that bikeNomad determined the time only once; the other time values are based on it. That's very important 'cause if you do it this way:
my $time = time;
my $ctime = localtime;
my @timeArray = localtime;
which might seem more "natural", you leave yourself open to a very subtle and obsure bug: What if the second changes between one of those calls? It might look like an "off-by-one" error, but it only happens "sometimes". Maybe nothing serious happens. But what if midnight occurs between one of those calls? Depending on what you're doing with @timeArray and $ctime you will get inconsistent results. This could hurt. I have seen it happen.
Have fun,
Carl Forde | [reply] [d/l] |
You have to call it twice, or generate your own scalar version
from the list output. What happens is, if you call localtime
in a string context, perl knows this and localtime generates
a string, whereas if you call it in list context, you get
the raw list output. There is a module called Timelocal that
lets you convert a date to seconds since the Epoch, but really
the best thing to do probably is to store the output of
time() for the time you want, and then feed it through
localtime like so
$str = localtime($storedtime);
@date = localtime($storedtime);
- Ant | [reply] [d/l] |
I found Date::Format and used the strftime() function to convert from list to string. However, I would very much like to be able to convert from the string to the list without writing my own function. | [reply] |
Look at Date::Parse which will parse a time/date string.
| [reply] |