ureco has asked for the wisdom of the Perl Monks concerning the following question:
There is a bit of redundant code at the moment but the area of interest is within the foreach (@standard_records) loop and the date conversions within this section of the code. Initially I thought this was working but it does not handle the time when the clocks change correctly.#! c:\xampp\perl\bin\perl use strict; use warnings; use LWP::Simple; use Time::Local; my $standard_filename = $ARGV[0]; my $extended_filename = $ARGV[1]; # Get yesterdays date time my $epoc = time(); $epoc = $epoc - 86400; my $yesterday = localtime($epoc); my ($sec, $min, $hour, $day, $month, $year) = (localtime($epoc))[0,1,2 +,3,4,5]; $month++; $year += 1900; my $mm = $month; if ($month < 10) { $month = "0" . $month; } if ($day < 10) { $day = "0". $day; } # Build the Standard and Extended filenames if not passed as parameter +s if (not defined $standard_filename) { $standard_filename = "C:\\wdisplay\\logfiles\\" . $mm . $year . "l +gcsv.csv"; } if (not defined $extended_filename) { $extended_filename = "C:\\WebHost\\customtextout.txt"; } # Load Standard File in to an Array my @standard_records; open (standard_file, "<", $standard_filename) or die "Failed to open file: $!\n"; while (<standard_file>) { chomp; push @standard_records, $_; } close standard_file; # Load Extended File into an Array my @extended_records; open (extended_file, "<", $extended_filename) or die "Failed to open file: $!\n"; while (<extended_file>) { chomp; push @extended_records, $_; } close extended_file; # Process each Standard Record foreach (@standard_records) { my @std_lines = split /\n/, $_; foreach my $line_std (@std_lines) { my @std_fields = split ",", $line_std; my $obs_day = $std_fields[0]; my $obs_month = $std_fields[1]; my $obs_year = $std_fields[2]; my $obs_hour = $std_fields[3]; my $obs_minute = $std_fields[4]; my $obs_seconds = 0; if ($obs_day eq "day") { next; } my $utc_time = timelocal($obs_seconds, $obs_minute, $obs_hour, + $obs_day, $obs_month - 1, $obs_year - 1900); my ($utc_sec, $utc_min, $utc_hour, $utc_day, $utc_month, $utc_ +year) = (gmtime($utc_time))[0,1,2,3,4,5]; $utc_month++; $utc_year += 1900; if ($obs_day < 10) { $obs_day = "0" . $obs_day; } if ($obs_month < 10) { $obs_month = "0" . $obs_month; } if ($obs_hour < 10) { $obs_hour = "0" . $obs_hour; } if ($obs_minute < 10) { $obs_minute = "0" . $obs_minute; } my $record_line = $obs_year . "," . $obs_month . "," . $obs_da +y . "," . $obs_hour . "," . $obs_minute . "," . "00" . "," . "," . $u +tc_year . "," . $utc_month . "," . $utc_day . "," . $utc_hour . "," . + $utc_min . "," . $utc_sec . "\n"; print $record_line; my $file = "C:\\WEATHER_DATA_COLLECTOR\\temp.csv"; open (TXT, ">>", $file); print TXT $record_line; close (TXT); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Converting local time to utc time
by boftx (Deacon) on Nov 10, 2014 at 22:03 UTC | |
by ureco (Acolyte) on Nov 10, 2014 at 22:46 UTC | |
by wjw (Priest) on Nov 10, 2014 at 22:58 UTC | |
by ureco (Acolyte) on Nov 11, 2014 at 20:43 UTC | |
by poj (Abbot) on Nov 11, 2014 at 21:24 UTC | |
by ureco (Acolyte) on Nov 11, 2014 at 21:47 UTC | |
| |
by ureco (Acolyte) on Nov 13, 2014 at 23:44 UTC |