problem with converting time using perl


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users problem with converting time using perl
# 1  
Old 05-09-2011
problem with converting time using perl

Hello,

I have an AIX 5.3 system and i created a script to get the last login of users.
The script goes like this:
Code:
 
LAST_LOGIN=`lsuser -a time_last_login $cur_user` 
TIME_LOGIN=`perl -e 'print scalar localtime("$LAST_LOGIN")'`

Actually what i do in these two lines is to set a variable (LAST_LOGIN) which is the return of
Code:
lsuser -a time_last_login

command. The reason i am doing this is because the return of this command is in a form like this 1304942474 which is not a human date and time and i need to convert it later on.
That is what i am doing in the second line using perl -e to convert this value. The weird thing is that if i simply run in the command line the following command
Code:
perl -e 'print scalar localtime(1304942474)'

i get the output Mon May 9 15:01:14 2011 which is correct i suppose. But when i run it through a script replacing the value (1304942474) with ("$LAST_LOGIN") then i get the output
Thu Jan 1 02:00:00 1970 which is obviously wrong.

Any idea what is causing this behaviour and how i should do it to work properly?

Thank you for your attention
# 2  
Old 05-09-2011
You have prevented interpolation of $LAST_LOGIN in the Perl call and so it sees it as an undeclared perl variable to be autovified to null, "" oe 0 depending on how it is accessed, try calling the following with the LAST_LOGIN value set in the shell rather than substituting the value.
Code:
perl  -Mstrict -e 'print scalar localtime ($LAST_LOGIN)'

To access environment variables in Perl scripts use the %ENV hash in your script
Code:
perl   -e 'print scalar localtime ($ENV{LAST_LOGIN)}'


Last edited by Skrynesaver; 05-09-2011 at 09:59 AM.. Reason: Ma spulin is turable
# 3  
Old 05-09-2011
Thank you for your help

i tried
Code:
perl   -e 'print scalar localtime ($ENV{LAST_LOGIN)}'

and it worked fine!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting time format as Integer to seconds

Hello, How can we convert date like format 20181004171050 in seconds ? I can able to convert till date but failing for HHMMSS. date -d "20181004" "+%s" output as 1538596800 . But when i add hhmmss it is failing date -d "20181004172000" "+%s" result Invalid date Kindly guide. Regards (16 Replies)
Discussion started by: sadique.manzar
16 Replies

2. Shell Programming and Scripting

Converting seconds to time

I have a list of time spans in seconds, and want to compute the time span as hh:mm:nn I am coding in bash and have coded the following. However, the results are wrong as "%.0f" rounds the values. Example: ftm: 25793.5 tmspan(hrs,min,sec): 7.16 429.89 25793.50 hh: 7 mm: 10 ss:... (2 Replies)
Discussion started by: kristinu
2 Replies

3. UNIX for Dummies Questions & Answers

Converting Epoch time

I have a Raspberry Pi that logs some temperatures using Onewire. Data is collected with RRDTool. The command sudo rrdtool fetch ute_temp.rrd AVERAGE -s -1h > ./test.log and then cat test.log gives the result 1388608500: 2.3579639836e+00 . How do I write a script that converts the Epoch time... (4 Replies)
Discussion started by: nilekl
4 Replies

4. Shell Programming and Scripting

Converting real time to epoch time

# date +%s -d "Mon Feb 11 02:26:04" 1360567564 # perl -e 'print scalar localtime(1360567564), "\n";' Mon Feb 11 02:26:04 2013 the epoch conversion is working fine. but one of my application needs 13 digit epoch time as input 1359453135154 rather than 10 digit epoch time 1360567564... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

6. Shell Programming and Scripting

converting epoch time to ddmmyy format

I can not find a working script or way to do this on sun solaris , can someone please guide me? e.g 1327329935 epoch secs = 012312 (ddmmyy) thanks (5 Replies)
Discussion started by: aliyesami
5 Replies

7. Shell Programming and Scripting

converting epoch time

Hi, Thanks bartus11 yesterday's code worked fine for me. In meantime I've found another "issue". As you can see highlighted, the time format in my original input in case of two rows which should be duplicited ,is differentwhat I need to do is to convert to this format "20110607-08:03:22"... (4 Replies)
Discussion started by: hernand
4 Replies

8. Shell Programming and Scripting

Help converting date-time value to decimal

Hi I need help to do some calculation in script. I have a monitor program (munin) that I would like to log uptime information from a server. The script looks like this (not complete): #!/bin/sh # server_uptime ### Config Start # Reads the server parameters using the HTTP port with... (7 Replies)
Discussion started by: Jotne
7 Replies

9. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

10. UNIX for Dummies Questions & Answers

Converting regular time to CTIME

Does anyone know of an easy way to convert regular time 08/21/2002 @ 8:21:21 pm to ctime. I need this to complete a script that I am writing. Your expertise and help would be amost appreciated. Please note - I am not a programmer so c-code etc will not help. A utility that can be run from a... (9 Replies)
Discussion started by: PGPhantom
9 Replies
Login or Register to Ask a Question