in reply to Date Timezone

The general code ... works if used in the main routine ... BUT if I place it in a function

Show us that then!

Replies are listed 'Best First'.
Re^2: Date Timezone
by kepler (Scribe) on Jul 15, 2017 at 23:29 UTC
    Hi. Sure.

    Here's what I'm trying:

    #!/usr/bin/perl use LWP::Simple; use DateTime; use DateTime::TimeZone; etc etc... sub TimeZone () { (my $year_tz, my $month_init, my $hour_init, my $zone) = @_; $offset_days = 0; $offset = 0; my $minutes_init = 0; my $seconds_init = 0; my $tz = DateTime::TimeZone->new( name => $zone ); my $dt = DateTime->new( year => $year_tz, month => $month_init, day => $day_init, hour => $hour_init, minute => $minutes_init, second => $seconds_init, nanosecond => 0, ); $offset = $tz->offset_for_datetime($dt); $offset_days = $offset / 86400; return $offset_days; }
    As I've said, no error. It just...stops. If I don't call it the entire scripts works.

      being that $offset_days and $offset seem to have a global scope i would look to see if there are side effects in the other code from resetting them, such as loops terminating early or exit getting called

      Hi, stevieb already mentioned this, but just to be clear: If your code is really how you've written it here, your sub is requiring that no arguments be passed to it. The code shown should throw an error if you try to pass it the arguments that you then try to load into @_ ... so are you getting that error? Or have you contrived some way to suppress the error? Or, is this not really the code you are using?

      $ perl -Mstrict -wE 'sub foo{ my $bar = shift; say $bar } foo("baz");' baz $ perl -Mstrict -wE 'sub foo(){ my $bar = shift; say $bar } foo("baz") +;' Too many arguments for main::foo at -e line 1, near ""baz")" Execution of -e aborted due to compilation errors.
      (For more detail, see Prototypes in perlsub.)


      The way forward always starts with a minimal test.

      Please show us a full Short, Self-Contained Correct Example. A full blown script as small as possible that fully shows the problem. What you've shown will just lead to endless guessing. For example, you don't even show us in the etc... where you're actually calling the sub... are you? Also, you don't need (and unless you know when you should) shouldn't be using prototypes in your sub definition (ie. remove the ()).

      Do you have use strict; and use warnings;? huck may be onto something. You are not scoping the return variable within the sub (if it's not declared globally, strict will catch that).

        Hi

        Thanks for the help, but I only found one solution: to put the following inside the function:
        my $dt = DateTime->new( year => $my_year, month => $my_mon, day => $my_day, hour => $my_hours, minute => $my_minutes, time_zone => 'GMT', ); $dt->set_time_zone( $zone ); $rh = $dt->hour; #returns the adjusted hour $rm = $dt->minute; #returns the adjusted minutes
        $zone must be declared 'local' outside the procedure. We can play actually with 2 timezones (not putting GMT but $zone2 for example). Regards. Kepler