Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date/time stamp 1

Status
Not open for further replies.

MTarkington

Programmer
Feb 14, 2001
73
US
I am having a problem trying to figure out how the localtime function works. I have researched the forum, and some other resources, but I don't understand how to format the date when inserting it into a variable.

Here's what I'm trying to do: When a record is created via the Perl program, I am inserting the data into an Oracle database. This is where I want to insert a date/time stamp. I want it to be in the format 'YYYYMMDDHH24MISS', for the ease of inserting it into Oracle.

Now, what is the syntax to actually make the localtime function give me the above format for today's date? I have tried everything I can think of, but using Perl, my knowledge is limited.

Any help would be appreciated.

Thanks in advance,
Mark
 
Try This:
$timestamp = sprintf ("%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d",
(localtime)[5] + 1900, # Year
(localtime)[4] + 1, # Month
(localtime)[3], # Day
(localtime)[2], # Hour
(localtime)[1], # Minute
(localtime)[0] # Second
);
 
as always, TMTOWTDI ;-)
Code:
#!/usr/local/bin/perl
@dt = localtime;
$dt[5] += 1900;
$dt[4] += 1;
foreach (0,1,2,3,4) 
{if (length($dt[$_])< 2) { $dt[$_] = '0'.$dt[$_]; } }
$time_stamp = &quot;$dt[5]$dt[4]$dt[3]$dt[2]$dt[1]$dt[0]&quot;;
print &quot;$time_stamp\n&quot;;


If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
The perldocs are your friend :)

perldoc perl (to get started)

perldoc -f localtime (to look up info on localtime function)

Also, for help in *what* you are trying to do, the Perl Cookbook is an excellent reference.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top