chenga script to show one houer more

Discussion in 'Perl Misc' started by kozmos241, Sep 19, 2005.

  1. kozmos241

    kozmos241 Guest

    Hi,

    i am new to perl and i am wondering how to change this script to show one
    houer more, i will change it back for daylight saving.



    #!/usr/bin/perl

    use strict;

    (my $secT, my $minT, my $hourT, my $mdayT, my $monT, my $yearT, my $wdayT,
    my $ydayT, my $isdstT) = localtime();

    $yearT += 1900;
    $monT += 1;
    $monT = (length($monT)==1?"0$monT":$monT);
    $mdayT = (length($mdayT)==1?"0$mdayT":$mdayT);

    my $fechaT = $yearT.$monT.$mdayT;

    my $diferencia;

    if (
    ($fechaT >= "20020407" && $fechaT <= "20021027" ) ||
    ($fechaT >= "20030406" && $fechaT <= "20031026" ) ||
    ($fechaT >= "20040404" && $fechaT <= "20041031" ) ||
    ($fechaT >= "20050403" && $fechaT <= "20051030" ) ||
    ($fechaT >= "20060402" && $fechaT <= "20061029" )
    ){
    $diferencia = 3600;
    }else{
    $diferencia = 7200;
    }

    (my $sec, my $min, my $hour, my $mday, my $mon, my $year, my $wday, my
    $yday, my $isdst) = localtime(time + $diferencia);
    $year += 1900;
    $mon += 1;
    if ($mon == 1){
    $mon = "Januar";
    }elsif ($mon == 2){
    $mon = "Februar";
    }elsif ($mon == 3){
    $mon = "März";
    }elsif ($mon == 4){
    $mon = "April";
    }elsif ($mon == 5){
    $mon = "Mai";
    }elsif ($mon == 6){
    $mon = "Juni";
    }elsif ($mon == 7){
    $mon = "Juli";
    }elsif ($mon == 8){
    $mon = "August";
    }elsif ($mon == 9){
    $mon = "September";
    }elsif ($mon == 10){
    $mon = "Oktober";
    }elsif ($mon == 11){
    $mon = "November";
    }elsif ($mon == 12){
    $mon = "Dezember";
    }

    $mday = (length($mday)==1?"0$mday":$mday);
    $hour = (length($hour)==1?"0$hour":$hour);
    $min = (length($min)==1?"0$min":$min);

    my $fecha = $mday.". ".$mon." ".$year.", ".$hour.":".$min."h";

    print "content-type: text/html\n\n";
    print << "FIN";

    <tr>
    <td width=185 valign=top height=15>
    <p>
    <B>City, State</B><br>
    $fecha
    </p>
    </td>
    </tr>
    FIN
    kozmos241, Sep 19, 2005
    #1
    1. Advertising

  2. kozmos241

    Mothra Guest

    Hello,

    kozmos241 wrote:
    > Hi,
    >
    > i am new to perl and i am wondering how to change this script to show
    > one houer more, i will change it back for daylight saving.
    >
    >
    >

    [code snipped]

    You might want to take a look at the DateTime project
    http://datetime.perl.org/

    Hope this helps

    Mothra
    Mothra, Sep 19, 2005
    #2
    1. Advertising

  3. kozmos241

    kozmos241 Guest

    On Mon, 19 Sep 2005 10:53:18 -0700, Mothra wrote:

    > Hello,
    >
    > kozmos241 wrote:
    >> Hi,
    >>
    >> i am new to perl and i am wondering how to change this script to show
    >> one houer more, i will change it back for daylight saving.
    >>
    >>
    >>

    > [code snipped]
    >
    > You might want to take a look at the DateTime project
    > http://datetime.perl.org/
    >
    > Hope this helps
    >
    > Mothra


    well, it will have to do, i hopet that someon will change in script needed
    and post it cos i am not perl programer but its never to late lo learn
    something.

    thank you
    kozmos241, Sep 20, 2005
    #3
  4. kozmos241

    Anno Siegel Guest

    kozmos241 <> wrote in comp.lang.perl.misc:
    > On Mon, 19 Sep 2005 10:53:18 -0700, Mothra wrote:
    >
    > > Hello,
    > >
    > > kozmos241 wrote:
    > >> Hi,
    > >>
    > >> i am new to perl and i am wondering how to change this script to show
    > >> one houer more, i will change it back for daylight saving.
    > >>
    > >>
    > >>

    > > [code snipped]
    > >
    > > You might want to take a look at the DateTime project
    > > http://datetime.perl.org/
    > >
    > > Hope this helps
    > >
    > > Mothra

    >
    > well, it will have to do, i hopet that someon will change in script needed
    > and post it cos i am not perl programer but its never to late lo learn
    > something.


    That's hard to do, since it is by no means clear what change you want.
    Show the time one hour later than the code you have? Since you mention
    DST in the context, you can simply add 1 to the number of hours you
    have now. If it's DST it won't overflow into the next day, so the
    operation is trivial. If it's something else you want, please explain.

    Otherwise, your code does a lot of time formatting that is already
    done in available modules. For a start, look up strftime in POSIX.
    Together with a suitable format string $format, this may be all
    you need:

    strftime $format, localtime;
    strftime $format, 60*60 + localtime; # one hour later

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 20, 2005
    #4
  5. kozmos241

    Mothra Guest

    Hello,

    kozmos241 wrote:
    > On Mon, 19 Sep 2005 10:53:18 -0700, Mothra wrote:
    >
    >> Hello,
    >>


    (my stuff snipped)

    > well, it will have to do, i hopet that someon will change in script
    > needed and post it cos i am not perl programer but its never to late
    > lo learn something.


    Sorry, I did not realize that. I should have provided a sample script.

    use strict;
    use warnings;
    use DateTime;
    use DateTime::Format::Strptime;
    use CGI;
    my $Strp = new DateTime::Format::Strptime(
    pattern => '%d %B %Y %H:%M',
    locale => 'en_de',
    time_zone => 'Europe/Berlin',
    );

    my $dt = DateTime->now( locale => 'de', time_zone => 'Europe/Berlin' );

    my $q = new CGI;

    my $table = $q->table(
    $q->Tr(
    $q->td(
    { width => 185, valign => 'top', height => 15 },
    $q->p( $q->b( 'City', 'State' ), $Strp->format_datetime($dt) )
    )
    )
    );

    This should provide you what you need. With using the DateTime suite of
    modules
    it will "know" weather or not the date is DST or not. From the docs:

    is_dst
    Returns a boolean indicating whether or not the datetime object is
    currently in Daylight Saving Time or not.

    So you could use this Method to see if the time is DST or not.

    >>>
    >>>}elsif ($mon == 3){
    >>> $mon = "März";

    ^^^^^^
    one question, what language is that? My guess was german (hence my example)

    I hope this helps

    Mothra
    Mothra, Sep 20, 2005
    #5
  6. kozmos241

    Mothra Guest

    Hi Anno,

    Anno Siegel wrote:
    (snipped)
    > That's hard to do, since it is by no means clear what change you want.
    > Show the time one hour later than the code you have?


    I think the OP was a poor attempt at DST conversion. Hence
    my post about the DateTime project.

    > Since you
    > mention DST in the context, you can simply add 1 to the number of
    > hours you have now. If it's DST it won't overflow into the next day,
    > so the operation is trivial.


    not quite, switching to and from DST is not an easy task. Example:

    All countries in Europe, except Iceland , observe daylight-saving time and
    change on the same date: moving clocks forward one hour on the last Sunday
    in March and back one hour on the last Sunday in October.
    In the West European (UTC), Central European (UTC+1), and
    East European (UTC+2) time zones the change is simultaneous: on both dates
    the clocks are changed everywhere at 01:00 UTC, i.e. from local times
    of 01:00/02:00/03:00 to 02:00/03:00/04:00 in March, and vice versa in
    October.

    If the script in question was to run at 12:30 UTC and you add one hour to
    that is would
    be 1:30UTC which would be an invalid time for that zone (Western European)

    This and other issues is what the DateTime project is attempting to solve.
    If you have a chance, please take a look at the DateTime project
    http://DateTime.perl.org I think you will find it to be a very complete
    date/time suite of modules.

    > If it's something else you want, please explain.


    Yes, I think the OP could have explained a little better but I suppect
    that english is not his native tongue.

    (more snippage)
    >
    >
    > Anno


    Mothra
    Mothra, Sep 20, 2005
    #6
  7. kozmos241

    Anno Siegel Guest

    Mothra <> wrote in comp.lang.perl.misc:
    > Hi Anno,
    >
    > Anno Siegel wrote:
    > (snipped)
    > > That's hard to do, since it is by no means clear what change you want.
    > > Show the time one hour later than the code you have?

    >
    > I think the OP was a poor attempt at DST conversion. Hence
    > my post about the DateTime project.
    >
    > > Since you
    > > mention DST in the context, you can simply add 1 to the number of
    > > hours you have now. If it's DST it won't overflow into the next day,
    > > so the operation is trivial.

    >
    > not quite, switching to and from DST is not an easy task. Example:
    >
    > All countries in Europe, except Iceland , observe daylight-saving time and
    > change on the same date: moving clocks forward one hour on the last Sunday
    > in March and back one hour on the last Sunday in October.
    > In the West European (UTC), Central European (UTC+1), and
    > East European (UTC+2) time zones the change is simultaneous: on both dates
    > the clocks are changed everywhere at 01:00 UTC, i.e. from local times
    > of 01:00/02:00/03:00 to 02:00/03:00/04:00 in March, and vice versa in
    > October.
    >
    > If the script in question was to run at 12:30 UTC and you add one hour to
    > that is would
    > be 1:30UTC which would be an invalid time for that zone (Western European)


    I'm not sure I understand your argument. Is that 12:30 Sunday or Monday
    (before or after the switchover) and is it spring or autumn? And how can
    a UTC time be valid or invalid in a certain time zone? It's either valid
    UTC or it isn't.

    In any case, it is concerned with *when* to begin (or stop) adding an hour.
    My only point was that the switchover, when it occurs, doesn't involve a
    date change. That is taken care of in all civilized time zones.

    > This and other issues is what the DateTime project is attempting to solve.
    > If you have a chance, please take a look at the DateTime project
    > http://DateTime.perl.org I think you will find it to be a very complete
    > date/time suite of modules.


    Ah, I'm sure it is. Time and calendar(s) are fascinating subjects, and
    they lend themselves to be cast into Perl modules. That probably explains
    the proliferation of Date/Time modules on CPAN, not by itself a bad thing.

    On the other hand, I find that my day-to-day needs are met by Perl's
    localtime and gmtime, POSIX::strftime and the occasional use of Time::Local.
    So, I'm currently just not interested enough in the subject to review a
    large Date/Time project. I do understand the fascination with it, I've
    been there.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 20, 2005
    #7
  8. kozmos241

    Mothra Guest

    Hi Anno,

    Anno Siegel wrote:
    > Mothra <> wrote in comp.lang.perl.misc:
    >> not quite, switching to and from DST is not an easy task. Example:
    >>
    >> All countries in Europe, except Iceland , observe daylight-saving
    >> time and change on the same date: moving clocks forward one hour on
    >> the last Sunday in March and back one hour on the last Sunday in
    >> October.
    >> In the West European (UTC), Central European (UTC+1), and
    >> East European (UTC+2) time zones the change is simultaneous: on both
    >> dates the clocks are changed everywhere at 01:00 UTC, i.e. from
    >> local times
    >> of 01:00/02:00/03:00 to 02:00/03:00/04:00 in March, and vice versa in
    >> October.
    >>
    >> If the script in question was to run at 12:30 UTC and you add one
    >> hour to that is would
    >> be 1:30UTC which would be an invalid time for that zone (Western
    >> European)

    >
    > I'm not sure I understand your argument. Is that 12:30 Sunday or
    > Monday (before or after the switchover) and is it spring or autumn?


    I messed that up didn't I. In Western Europe During non DST times
    (November through the end of March) UTC is the same as local time.
    Now come the last Sunday in March and it is 12:30 AM (1/2 hr
    before the time change ie. adding one hour ) If the script in question
    was to be run it would add one hour which would put the time at
    1:30 AM (local time) on that same Sunday but the switch to DST occures
    at 1:00AM. It goes from UTC time to UTC+1 (in our example)
    What I was trying to illustrate was that there is no 1:30AM local time

    > And how can
    > a UTC time be valid or invalid in a certain time zone? It's either
    > valid UTC or it isn't.


    because I messed up in the explanation
    >
    > In any case, it is concerned with *when* to begin (or stop) adding an
    > hour. My only point was that the switchover, when it occurs, doesn't
    > involve a date change. That is taken care of in all civilized time
    > zones.


    That is correct

    >
    >> This and other issues is what the DateTime project is attempting to
    >> solve. If you have a chance, please take a look at the DateTime
    >> project http://DateTime.perl.org I think you will find it to be a
    >> very complete date/time suite of modules.

    >
    > Ah, I'm sure it is. Time and calendar(s) are fascinating subjects,
    > and they lend themselves to be cast into Perl modules. That probably
    > explains the proliferation of Date/Time modules on CPAN, not by
    > itself a bad thing.


    I think it is both good and bad. The good thing about it is there
    is so much variety. You are bound to find something you can use.
    But the bad thing is that they don't talk to each other.
    One of the reasons why DateTime was started was to provide
    a consistence interface for date and time functions. Along with
    Timezone/DST functions/conversions.

    >
    > On the other hand, I find that my day-to-day needs are met by Perl's
    > localtime and gmtime, POSIX::strftime and the occasional use of
    > Time::Local. So, I'm currently just not interested enough in the
    > subject to review a large Date/Time project. I do understand the
    > fascination with it, I've been there.


    I understand that. DateTime is a hefty perl project. And
    for most things it is overkill big time. I for one use it all the time
    because I help support it on the DateTime mailing list.All in all
    I have to say if you need DST/Timezone support the only
    option is the DateTime suite of modules

    Mothra
    Mothra, Sep 20, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    4
    Views:
    395
    Matt Hammond
    Jun 26, 2006
  2. Merciadri Luca
    Replies:
    4
    Views:
    796
  3. Replies:
    3
    Views:
    177
    Josef Moellers
    Nov 1, 2007
  4. Steven D'Aprano
    Replies:
    0
    Views:
    75
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    68
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page