Operating System - Tru64 Unix
1752832 Members
3619 Online
108789 Solutions
New Discussion юеВ

Re: How to get current date/time as seconds since Epoch in Perl

 
Victor Semaska_3
Esteemed Contributor

How to get current date/time as seconds since Epoch in Perl

Greetings,

Sorry if this is a simple question with an obvious answer but I've never done any programming in Perl.

I have a script written in Perl where I need to get the current date/time in seconds since Epoch. How would I do this?

I've used the following to get the date string given the seconds since Epoch:

_ntfytme=1133799166
_strg="print scalar localtime($_ntfytme)"
_ntfydte=`/usr/bin/perl -e "$_strg"`

So I assume that localtime might work but haven't been able to find the manpage for it.

Thanks in advance for any help.

Vic
There are 10 kinds of people, one that understands binary and one that doesn't.
9 REPLIES 9
Michael Schulte zur Sur
Honored Contributor

Re: How to get current date/time as seconds since Epoch in Perl

Hi,

is there an spcific problem you have in mind?

greetings,

Michael
Victor Semaska_3
Esteemed Contributor

Re: How to get current date/time as seconds since Epoch in Perl

Michael,

A few years ago a consultant wrote us a script in Perl that allows certain users to manage accounts within their group.

One of its functions is to change password. When it does it makes sure that the account isn't locked with:

system("/usr/sbin/usermod -x administrative_lock_applied=0 $NUSER");

It appears not to work properly when the password is just expired. In dxaccounts that's when a red circle with a white X in it appears next to the user icon (I think).

What I think is missing is the u_succhg field has to be updated with the current date/time in seconds since Epoch. That's why I asked.

Thanks,
Vic

There are 10 kinds of people, one that understands binary and one that doesn't.
Ann Majeske
Honored Contributor

Re: How to get current date/time as seconds since Epoch in Perl

Why don't you try setting the grace limit instead of updating the time field? Something on the order of:
system("/usr/sbin/usermod -x grace_limit=1 $NUSER");
This should work if the password is expired, but will not work if the account is expired.
Victor Semaska_3
Esteemed Contributor

Re: How to get current date/time as seconds since Epoch in Perl

Ann,

Thanks for the reply. One question, what's the difference between an expired password and an expired account.

Thanks,
Vic
There are 10 kinds of people, one that understands binary and one that doesn't.
Ann Majeske
Honored Contributor

Re: How to get current date/time as seconds since Epoch in Perl

See "man prpasswd" u_exp is the password expiration, u_expdate is the account expiration. The account expiration is used if you want to have an account active onlu for a specific period of time, e.g. for a contract employee.

Ann
Michael Schulte zur Sur
Honored Contributor

Re: How to get current date/time as seconds since Epoch in Perl

Victor,

what are you using to change the password?
Maybe it is not necessary to change the u_succhg?

Michael
Victor Semaska_3
Esteemed Contributor

Re: How to get current date/time as seconds since Epoch in Perl

Ann,

Thanks for the info, I wasn't aware of the distinction. We've always allowed the password expiration date to, in a sense, expire the account.

Michael,

Again, a consultant wrote this Perl script and I don't know Perl. From what I can gather it manually encrypts the password entered by the user with the 'crypt' function and then updates the u_pwd field.

Anyway, I found out for someone that to get what I wanted in Perl it is:

my $epoc=time();

Simple enough. I've made the necessary changes, crossed my fingers, and tried it. It works.

I'm sure there's better ways of doing this but right now I've got other more pressing problems to look at, like Legato backups that start before the precmd finishes.

Thanks,
Vic
There are 10 kinds of people, one that understands binary and one that doesn't.
Ann Majeske
Honored Contributor

Re: How to get current date/time as seconds since Epoch in Perl

Hi Vic,

Manually encrypting the password with crypt isn't the best way to do this. It limits you to using crypt which is one of the reasons why people enable Enhanced Security, to allow other encryption methods. It would be better to figure out a way to either use the usermod command to change the password or call out from the perl to the sia_change_password() c routine. Something to look into when you have some free time :)

To fix your current problem it looks like updating u_succhg is the best way to handle it.

Ann
Victor Semaska_3
Esteemed Contributor

Re: How to get current date/time as seconds since Epoch in Perl

Ann,

Thanks for the information but where I work, "When I have some free time" usually means "It ain't gonna happen". :)

Happy Holidays,
Vic
There are 10 kinds of people, one that understands binary and one that doesn't.