Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert unix timestamp into ODBC datetime format

Posted on 2004-09-08
18
860 Views
Last Modified: 2010-05-18
Is there a simple way to convert a unix timestamp into std odbc date format ?  

I am extracting the date of my input file in my perl script
my $DateTime = ctime(stat($input)->mtime);

print $DateTime reads  "Sep  8 11:03:38 2004"
But when I can't insert this into sql server; is there a simple conversion routine to convert this into mm/dd/yyyy hh:mm:ss   format ???????????

Subir.
0
Comment
Question by:subirc
  • 10
  • 4
  • 3
18 Comments
 

Author Comment

by:subirc
ID: 12009648
Note: I know I can do this by writing my own sub-routine and do the conversion myself.......but is wondering if there is already a function.......!!!!!!!!!
0
 
LVL 84

Expert Comment

by:ozo
ID: 12010327
use POSIX;
my $DateTime = strftime("%m/%d/%Y %H:%M:%S",localtime stat($input)->mtime);
0
 

Author Comment

by:subirc
ID: 12010778
Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)

getting this error when I am running it..........any clues?

Thks!!!
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:subirc
ID: 12010800
Why do you have to have locatime in the strftime ????? Isn't stat($input)  already has the unformatted date string????
Just wondering.......
0
 

Author Comment

by:subirc
ID: 12011243
Ok this worked....finally.....I did manage to do it

use Date::Manip qw(ParseDate UnixDate);
my $date = ParseDate(ctime($stats->mtime));
my $DateTime = UnixDate($date, "%m/%d/%Y %H:%M:%S");


Thanks!!! Anyways
0
 
LVL 48

Accepted Solution

by:
Tintin earned 40 total points
ID: 12011532
Using Date::Manip module to obtain the date format you want it total overkill.

The following will work:

use POSIX 'strftime';
my $datetime = strftime "%D %T",localtime((stat $input)[9]);

0
 
LVL 84

Expert Comment

by:ozo
ID: 12336340
You seem to be pasing the wrong number of arguments to strftime.
How exactly did you call it?
0
 

Author Comment

by:subirc
ID: 12338875
my $date = ParseDate(ctime($stats->mtime));
my $DateTime = UnixDate($date, "%m/%d/%Y %H:%M:%S");

Thatz  what I have ....and  that  worked.......!!!!!!!!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12342369
subirc.

Your method may have worked, but as previously stated, the Date::Manip module is total overkill for this function as it is very big and slow.  Using the POSIX strftime function is *much* more efficient and it is a standard Perl module, which Date::Manip isn't.
0
 

Author Comment

by:subirc
ID: 12361861
But, I am getting an error when I am trying to use POSIX  what you suggested...........

Here's the error....
Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at convert.pl line 39
0
 
LVL 84

Expert Comment

by:ozo
ID: 12361984
Please show us your convert.pl line 39
0
 

Author Comment

by:subirc
ID: 12363269
use POSIX 'strftime';

my $datafile = $ARGV[0]            or die "Input file not specified\n";
my $outfile = (split /\./,$datafile)[0].".out";      #appending extension for output file
                                  #to the input file name(without extension)
open (DATA,"<$datafile")      or die "Could not open $datafile: $!\n";
open (OUT, ">$outfile")      or die "Could not open $outfile: $!\n";

#get file information
my $stats = stat($datafile);
my $UserId = getpwuid($stats->uid);
my $datetime = strftime "%D %T",localtime((stat $datafile)[9]);
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12363678
What OS and Perl version are you running?

The snippet you supplied works fine for me.
0
 

Author Comment

by:subirc
ID: 12363818
Version 5
0
 

Author Comment

by:subirc
ID: 12363835
It is cribbing right in the first line -------->>> use POSIX 'strftime'; (line 38)

Subirc
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12364236
There's no such thing a Perl version 5.

perl -v

will give you the correct version.

Also, what OS are you on.

It's not barfing at the

use POSIX 'strftime';

it's barfing at

my $datetime = strftime "%D %T",localtime((stat $datafile)[9]);

the error means that the strftime function is receiving incorrect parameters.

Try this sample script to see if you get errrors:

#!/usr/bin/perl
use POSIX 'strftime';
print strftime "%Y-%m-%d",localtime((stat '/etc/passwd')[9]);
0
 

Author Comment

by:subirc
ID: 12365540
I am using  5.6.1 built for i386-linux  

OS - linux


 It finally worked with my script.....reason it was failing because i also was using the following module. I commented that line & it worked.

use Time::localtime;

Somehow it confused the interpretor.  

Thanks  Experts!!!!!

This better be good as I spend so much extra time when I already had an answer.......just kidding :D
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl output collect 7 67
rename outfile before writing 2 74
Perl string replace for refred url 9 63
PERL - Find newest folder 12 150
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question