in reply to Re^2: Converting local time to utc time
in thread Converting local time to utc time

I've managed to install the DateTime module but I'm still getting incorrect values returned as it moves through the Daylight Saving Time change.

I've added the following code:
my $dt = DateTime->new ( year => $obs_year, month => $obs_month, day => $obs_day, hour => $obs_hour, minute => $obs_minute, time_zone => 'Europe/London', ); $dt->set_time_zone( 'UTC' );
The following is output:

LOCAL | UTC
2014/10/26 00:59 | 2014/10/25 23:59 <- Correct
2014/10/26 01:00 | 2014/10/26 01:00 <- Incorrect, should be 00:00

UTC is also actually repeating 01:00 - 01:59 so after the Daylight Saving Change it is correct again it's just the transition it appears to go wrong.

The UTC datetime is still being moved on by 1hr - this should stay the same enabling the Local time to catch-up.

Have I coded this correctly?

Really appreciate your help on this - Thanks.

Replies are listed 'Best First'.
Re^4: Converting local time to utc time
by poj (Abbot) on Nov 11, 2014 at 21:24 UTC
    From the DateTime docs
    Ambiguous Local Times

    Because of Daylight Saving Time, it is possible to specify a local time that is ambiguous. For example, in the US in 2003, the transition from to saving to standard time occurred on October 26, at 02:00:00 local time. The local clock changed from 01:59:59 (saving time) to 01:00:00 (standard time). This means that the hour from 01:00:00 through 01:59:59 actually occurs twice, though the UTC time continues to move forward. If you specify an ambiguous time, then the latest UTC time is always used, in effect always choosing standard time.
    poj
      Didn't fully understand that paragraph when I first read it! Given that I cannot change the source local datetime - is there anyway I can convert these ambiguous date times into UTC?

      Kind of feel I'm back to my original question... given a local date time for the entire year incrementing by 1 minute each time how can I convert this local time into UTC time and get correct results, even during the ambiguous date time period when the local date time changes from Daylight Saving Time and back again?

        The problem is: if you have a timestamp of "2003-10-26 01:17:47 Local Time", you actually have incomplete information. Only if you can distinguish(*) between "Local Time (Summer)" and "Local Time (Winter)", you have a chance to getting the right results here.

        Does your CSV source contain that info?

        Otherwise it will become guesswork, e.g. you could assume that the transition between summer and "normal" time (as I like to call it) is marked by a record with a time stamp earlier than its predecessor.

        (*) ... and can give this bit to the converting (sub)routine
Re^4: Converting local time to utc time
by ureco (Acolyte) on Nov 13, 2014 at 23:44 UTC
    Managed to write code to get around this problem - at least for my specific requirements. Might not be the most elegant code but it works.

    For each local time I could obtain from the DateTime module whether it was DST or not ('0' / '1') indicator. By storing the previous value I could determine whether the dates being processed where changing from DST to GMT/UTC - value of "10" when combining the previous and current values.

    When this occurred I set up a counter to subtract 1 hour from the UTC time for the next hour. This correctly ensured that the UTC time continue without jumping forward and missing the 00:00 - 00:59 period.

    When I ran March data (GMT/UTC to DST) I discovered I did not have a problem during this calculation because the local time move forward an hour it never contains data that will cause the Date Time module to return an error due to an invalid date time - i.e. a time in the missing hour.

    Code below, (part off as a bit long after I added all my weather parameters) as I said probably not the most efficient or well written Perl code (still new to Perl) but it does what I need it to do... Thanks for all your guidance and pointers, much appreciated.
    my $prev_dst = ""; my $curr_dst = ""; my $dst_code = ""; my $dst_flag = "N"; my $dst_counter = 1; foreach (@extended_records) { my @ext_lines = split /\n/, $_; foreach my $line_ext (@ext_lines) { my @ext_fields = split ",", $line_ext; # Get the fields from the Extended File. my $obs_day = $ext_fields[0]; my $obs_month = $ext_fields[1]; my $obs_year = $ext_fields[2]; my $obs_hour = $ext_fields[3]; my $obs_minute = $ext_fields[4]; my $obs_seconds = 0; my $temp_c = $ext_fields[5]; my $humidity_rh = $ext_fields[6]; my $dewpoint_c = $ext_fields[7]; my $wind_dir_degrees = $ext_fields[12]; my $wind_speed_mph = $ext_fields[10]; my $wind_gust_mph = $ext_fields[11]; my $pressure_hpa = $ext_fields[9]; my $rain_last_minute = 0; my $rain_today = $ext_fields[13]; my $heat_index = $ext_fields[8]; my $rain_rate = $ext_fields[14]; my $soil_temp = $ext_fields[15]; my $weather_conditions = $ext_fields[16]; my $heat_word = $ext_fields[17]; my $ground_temp = $ext_fields[18]; my $solar = $ext_fields[19]; my $uv_index = $ext_fields[20]; my $solar_percentage = $ext_fields[21]; my $solar_description = $ext_fields[22]; my $et = $ext_fields[23]; my $burntime = $ext_fields[26]; my $sunshine = $ext_fields[27]; my $soil_moisture = $ext_fields[28]; my $leaf_wetness = $ext_fields[29]; my $cloud_height_metres = $ext_fields[32]; my $indoor_temp_c = $ext_fields[35]; my $indoor_humidity_rh = $ext_fields[36]; my $apparent_temp = $ext_fields[37]; my $apparent_solar_temp = $ext_fields[38]; my $humidex = $ext_fields[40]; my $air_density = $ext_fields[44]; # Format the collected data $temp_c =~ s/°C//; $dewpoint_c =~ s/°C//; $wind_dir_degrees =~ s/ °//; $wind_speed_mph =~ s/ mph//; $wind_gust_mph =~ s/ mph//; $pressure_hpa =~ s/ mb//; $rain_today =~ s/ mm//; $heat_index =~ s/°C//; $soil_temp =~ s/°C//; $solar_percentage =~ s/ %//; $humidex =~ s/°C//; # Get the Observation Date Time and keep in Local Format. my $obs_datetime_local = DateTime->new ( year => $obs_year, month => $obs_month, day => $obs_day, hour => $obs_hour, minute => $obs_minute, time_zone => 'Europe/London', ); # Get the Observation Date Time and Convert to UTC. my $obs_datetime_utc = DateTime->new ( year => $obs_year, month => $obs_month, day => $obs_day, hour => $obs_hour, minute => $obs_minute, time_zone => 'Europe/London', ); $obs_datetime_utc->set_time_zone( 'UTC' ); my $dst = $obs_datetime_local->is_dst(); # Combine the Current and Previous IsDST values to determine i +f the DST to GMT/UTC change has occured. $curr_dst = $dst; $dst_code = $prev_dst . $curr_dst; # If the Previous DST is set (1) and the Current DST is not se +t (0) then set the DST Flag. if ($dst_code eq "10") { $dst_flag = "Y"; } # If the DST Flag has been set then a change from DST to GMT/U +TC has occurred. if ($dst_flag eq "Y") { # Subtract 1 from the Observation Hour to get the correct +UTC Date/Time - construct the formated UTC Date & Time. my $dst_hour = $obs_hour - 1; my $dst_datetime_utc = DateTime->new ( year => $obs_year, month => $obs_month, day => $obs_day, hour => $dst_hour, minute => $obs_minute, ); $obs_datetime_utc = $dst_datetime_utc; # Reset the DST Flag and Counter after the hour requiring +the change to UTC has been processed. if ($dst_counter == 59) { $dst_flag = "N"; $dst_counter = 1; } $dst_counter = $dst_counter + 1; } # Use the UTC Date/Time for the unformatted Observation Date & + Time. my $rep_year = substr $obs_datetime_utc,0,4; my $rep_month = substr $obs_datetime_utc,5,2; my $rep_day = substr $obs_datetime_utc,8,2; my $rep_hour = substr $obs_datetime_utc,11,2; my $rep_minute = substr $obs_datetime_utc,14,2; my $rep_second = substr $obs_datetime_utc,17,2; # Output Record Line to a CSV File my $record_line = $rep_year . $rep_month . $rep_day . $rep_hou +r . $rep_minute . $rep_second . "," . $obs_datetime_utc . "," . $obs_ +datetime_local . "," . $temp_c . "," . $dewpoint_c . ",". $humidity_r +h . "," . $wind_dir_degrees . "," . $wind_speed_mph . "," . $wind_gus +t_mph . "," . $pressure_hpa . "," . $rain_last_minute . "," . $rain_t +oday . "," . $heat_index . "," . $rain_rate . "," . $soil_temp . "," +. $weather_conditions . "," . $heat_word . "," . $ground_temp . "," . + $solar . "," . $uv_index . "," . $solar_percentage . "," . $solar_de +scription . "," . $et . "," . $burntime . "," . $sunshine . "," . $so +il_moisture . "," . $leaf_wetness . "," . $cloud_height_metres . "," +. $indoor_temp_c . "," . $indoor_humidity_rh . "," . $apparent_temp . + "," . $apparent_solar_temp . "," . $humidex . "," . $air_density . " +\n"; print $record_line; my $file = "C:\\WEATHER_DATA_COLLECTOR\\BATHGATE\\" . $rep_yea +r . $rep_month . "_BATHGATE.CSV"; open (TXT, ">>", $file); print TXT $record_line; close (TXT); $prev_dst = $curr_dst; } }