costas has asked for the wisdom of the Perl Monks concerning the following question:

I need to generate a unique identifier to a large number of users accessing my code. The unique identifier has to be the number of seconds passed since 00:00::00 2000 GMT.

I am able to get the date from the time::local module but how would i mess around with this date without having to go through loads and loads of convert to seconds modules. I am using my $time = gmtime(time)

THis also must be dynamic so that i dont have to upgrade the code every time the calendar year changes.

thanks, cos

Replies are listed 'Best First'.
Re: time in seconds
by Hofmator (Curate) on Aug 08, 2001 at 16:59 UTC

    not sure if I'm getting what you mean ... but what about simply my $seconds = time - 946684800; time gives the seconds since 00:00:00 UTC, January 1, 1970 and subtract from this the seconds till 00:00::00 2000 UTC.

    -- Hofmator

Re: time in seconds
by mattr (Curate) on Aug 08, 2001 at 17:16 UTC
    The sequence of "number of seconds passed since x" will not provide enough unique ids for a "large number of users" i.e. >1 user per second.

    How about $t = time-$deltasecs . "|" . int(rand(10e5));

    overkill: Apache::Session::Generate::MD5

Re: time in seconds
by scain (Curate) on Aug 08, 2001 at 17:18 UTC
    While Hofmator is right, you may want to look at Date::Calc which allows many time and date transformations done easily.

    Scott

Re: time in seconds
by thpfft (Chaplain) on Aug 08, 2001 at 18:04 UTC

    Small confusion to clear up: time returns seconds-since-epoch (ie 1970). gmtime() converts seconds-since-epoch into either a readable string or an array of hours and minutes and so on depending on whether it's called in scalar or list context. Performing backflips to parse a gmtime(time) string into seconds would not be effort well spent.

    So the seconds counter is easy, as Hofmator shows. But it probably isn't good enough: two people could very easily use your script during the same second if, as you say, there are a large number of them.

    Here are a few other options to consider:

    seconds . ip address; # if on web. has anonymity implications seconds . username; # if command-line. also not anonymous $ENV{UNIQUE_ID}; # if in apache & compiled with mod_unique_id use Time::HiRes; # microseconds.

    or then there's Apache::Session, which would be overkill to start with but might save you some days later.

Re: time in seconds
by costas (Scribe) on Aug 08, 2001 at 18:24 UTC
    I have decided to use the epoch time in second and then use it as the seed value in the rand function.

    i am therefore doing

    my $id = int(rand($epochdate));

    Does anybody know what kind of calculation is being done using this rand function and if there is a possibility of two end results being the same (having been generated from the same epoch time or from different time)

    costas
      This approach does not work because it is unpredictable when a previously assigned number shows up.
      What about using a simple (persistent) counter without using time or rand at all?

      MP