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

Hi! Im trying to create a script that would give me a
particular date by giving a week number exp.

First I need to enter fix reference date:
$reftime = 102049345; #(Nov. 5 2001)
#this would be my reference
then the user would enter a week number:
example would give a result like this:


week      result
1      Nov. 5 2001 Monday - Nov. 11 2001 Sunday
2      Nov. 12 2001 Monday - Nov. 18 2001 Sunday
~      ~
9      Dec. 24 2001 Monday - Dec. 30 2001 Sunday
10      ~
Now I know that the "time" function returns a long number I think 10 digits,
I need to understand what is being returned by "time" so I
could understand, if it Is possible to just add an amount to the "time" to get the needed result.
Exp. to get week 1 is : localtime($reftime + (1 * 7))
So basically I just add the number of days to $reftime which is in raw ?time?, but by how much.

And also if it is possible to revert the result of a
localtime(time)
Nov. 11 2001 Sunday = localtime(time) ;
Time = Nov. 11 2001 Sunday #this is for the $reftime,
if I could enter the normal result of A localtime(time) and get the raw "time".

And if there is a script like this that I could study?

Summary:
The meaning of what is returned by ?time?? 1006450348
And if it is possible to revert the result of localtime(time);
or am i a approaching this the wrong way?

Replies are listed 'Best First'.
Re: time & localtime() function Q?
by mitd (Curate) on Nov 22, 2001 at 23:06 UTC
    time() returns the number of SECS since Epoch. Epoch can vary from OS to OS but localtime(time) if used in a scalar context will return a human readable date.
    my $datetime = localtime(time); # returns Thu Nov 22 13:05:26 2001 # (well at least it did at the time() )
    For your continuing date manipulation needs Date::Calc, Date::Manip and Date::Format will quickly become your friends.

    mitd-Made in the Dark
    'Interactive! Paper tape is interactive!
    If you don't believe me I can show you my paper cut scars!'

      Date::manip is the way to go.
      It handles the addition of periods of time to a date, and while the documentation doesn't mention it, using
      $date=&DateCalc("today","+ 1week",\$err);
      Would almost certainly work to set date to one week from today.
      what would time() return in unix and windows? what dose it mean?

      im creating in windows but will need to upload on a unix server.
        If you mean you are writing/testing code on windows and uploading to Unix then don't worry things will work as expected ie localtime() will do the right thing. time and localtime() in Perl are really just 'wrappers' around lower level system library calls of the same name. Most (gulp) modern OSes support theses calls,

        mitd-Made in the Dark
        'Interactive! Paper tape is interactive!
        If you don't believe me I can show you my paper cut scars!'

Re: time & localtime() function Q?
by jlongino (Parson) on Nov 23, 2001 at 01:51 UTC
    Gerryjun, as the others have stated, you need to use localtime. The function localtime is usually called like so:
    ($secs,$mins,$hrs,$dom,$moy,$yrs,$dow,$juldays,$dst) = localtime $reft +ime;
    $secs: seconds
    $mins: minutes
    $hrs: hours (military 24hr)
    $dom: day of month
    $moy: month of year (values 0-11, 0=jan)
    $yrs: years since 1900
    $dow: day of week (values 0-6, 0=sun)
    $juldays: number of days since jan 1, current year
    $dst: using daylight savings time (0=no, 1=yes)

    The function time() returns the number of seconds since Jan 1, 1970. BTW, the $refdate you gave (102049345) is for Monday, March 26, 1973.
    $secs: 25
    $mins: 2
    $hrs: 21
    $dom: 26
    $moy: 2
    $yrs: 73
    $dow: 1
    $juldays: 84
    $dst: 0

    If you start out with a reference date like the value returned by time (your example: 102049345) and you say:

    $wklater = $reftime + 60*60*24*6
    $wklater can then be passed to localtime and you can build the text string you want:
    ($secs,$mins,$hrs,$dom,$moy,$yrs,$dow,$juldays,$dst) = localtime $wkla +ter;
    Hint: Build a few array constants to return the string parts ($dow[1]="Monday", $moy[11]="December").

    --Jim

Re: time & localtime() function Q?
by davorg (Chancellor) on Nov 23, 2001 at 14:42 UTC

    time returns the number of seconds since some arbitrary "epoch" time. For most operating systems, that time is 00:00 Jan 1st 1970 (GMT). As someone else pointed pointed out, that number just passed 1E9.

    To convert from a given date to an epoch number use the timelocal function from the (standard) Time::Local module. To convert from an epoch number to a human readable format use the localtime function. You can control the format of this date easier using the POSIX::strftime function.

    If you have an epoch date and you want to add or substract a certain period, then you simple convert to the appropriate number of seconds. e.g. to add two weeks to an epoch number, use code like:

    $epoch += 2 * 7 * 86_400; # 86,400 seconds in a day

    And finally, people have been plugging modules like Date::Manip, Date::Format and Date::Calc. In my opinion, these have all recently been obsoleted by the release of Matt Sergeant's very wonderful Time::Piece module.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: time & localtime() function Q?
by Matts (Deacon) on Nov 23, 2001 at 21:33 UTC
    Try this.
    use Time::Piece; use Time::Seconds; my $refsecs = 102049345; #(Nov. 5 2001) (prefer strptime!) my $reftime = localtime($refsecs); for my $week (1..10) { print "$week ", $reftime->strftime("%b. %e %Y %A"); print " - ", $reftime += ONE_DAY * 6; print $reftime->strftime("%b. %e %Y %A"), "\n"; $reftime += ONE_DAY; }
Re: time & localtime() function Q?
by schweini (Friar) on Nov 24, 2001 at 00:33 UTC
    heyho!

    i devel'd a system that needed all those features once. here are some things that might help:

    $weeknum = $yday / 52 ($yday as returned from localtime() )
    i always stored the seconds since epoch in the DB, cause manipulating integers is way much funner than dealing with dates (only humans could have thought of such an incredibly computing-unfriendly concept).

    watch out for DST-hour shifts! i can't really recall where that was, but i checked for e.g. "messages received in a particular week" using offsets (coded in S.S.E.s), and that is if a week/month/day has an extra hour somewhere, that really messes things up. try to convert the offset to SSE using functions of the TIME package on cpan.

    when using SQL, try to avoid storing the SSEs in the DB - rather use a time-to-sql-timeformat-conversion function...that was you'll avoid a couple of problems later on.

    hope i could help, and i'm always willing to help,

    M.