Thread Datums- und Zeitformatierung (7 answers)
Opened by mzurhorst at 2012-04-16 00:52

mzurhorst
 2012-04-16 00:52
#157509 #157509
User since
2012-03-28
9 Artikel
BenutzerIn
[default_avatar]
Guten Abend zusammen!

Mein Skript wächst und gedeit, aber nun komme ich mal wieder nicht so recht weiter.

Vom prinzipiellen Aufbau geht es gerade um folgendes Teilproblem:

1) schauen ob Garmin GPS-Gerät angeschlossen ist und dort die Fund-Zeit besorgen aus der Datei geocache_visits.txt:
  • Dazu übergebe ich meiner sub "get_foundtime_from_garmin" die Variable $gcid, in welcher die ID des aktuellen Caches drin steht.
  • Die Formatierung der Zeit lautet hier: "yyyy-mm-ddThh:mmZ".
  • Falls etwas fehl schlägt (z.B. Garmin nicht angeschlossen), wird "n/a" zurück geliefert.

2) Fund-Zeit alternativ ermitteln: sub "read_date_from_notetxt"
  • Sofern "n/a" erscheint, schaue ich in eine andere Textdatei namens note.txt.
  • Dort steht das Datum in einem anderen Format drin: "dd.mm.yyyy".
  • Nun wollte ich dieses Datum "umkrempeln" sozusagen, um dann ebenfalls wieder "yyyy-mm-ddThh:mmZ" zu bekommen.
  • Da die note.txt Datei die Uhrzeit nicht enthält, wollte ich einfach "T00:00Z" an den String hängen.

3) wenn beide Formatierungen identisch sind möchte ich dann in eine Epochenzeit umwandeln, um die Caches in die richtige Reihenfolge zu bekommen. Diese Funktion gibt es aber noch nicht.

Eine typische Zeile aus der geocache_visits.txt Datei lautet:
Quote
GC39XWB,2012-03-17T10:33Z,Found it,""

Und die Zeile aus der note.txt lautet:
Quote
Found: 17.03.2012



Teil des Hauptprogramms:
Code (perl): (dl )
1
2
3
4
5
6
7
8
    my $foundstring = get_foundtime_from_garmin($gcid);    
    print "   DEBUG add_new_cache 4: return value from get_foundtime_from_garmin: $foundstring\n\n\n";
    
    if ($foundstring eq "n/a") {
        # need to read the time from the note.txt file
        my $notetxtfile = $gcdir . "/found/" . $currentdir . "/note.txt";
        $foundstring = read_date_from_notetxt($notetxtfile);
    }


Funktion read_date_from_notetxt:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# STEP 2.5   --> read_date_from_notetxt
sub read_date_from_notetxt {
    my $file = shift;
    
    read_config $file => my %notetxtrecord;
    my $datestring = $notetxtrecord{''}{'Found'};
    print "DATESTRING:  $datestring\n";

    # FIXME: this is not working right now    
    my @datearray;
    @datearray = split(/./,$datestring);
    print "Date-Array 2nd position: ";
    print $datearray[2];
    print "\n";
    my $timestring = $datearray[2]."-".$datearray[1]."-".$datearray[0]."T00:00Z";
    
    print $timestring."\n\n";
    return $timestring;    
}


Funktion get_foundtime_from_garmin (8.6kb):


Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# STEP 2.6   --> get_foundtime_from_garmin
sub get_foundtime_from_garmin {
    my $cache_id = shift;
    my $foundtime_from_garmin = '';

    if ( -f $garminfoundfile ) {
        print "   DEBUG get_foundtime_from_garmin 1:  GARMIN CONNECTED:  $garminfoundfile\n";

        # sleep for 1 second because we open and close the file again and again
        sleep 1;
        
        # read exact timestamp from files 
        open GCVISITSFILE, "<encoding(utf16_le)", $garminfoundfile 
                   or die "Error: $garminfoundfile file cannot be opened!\n$!";

        # read the geocache_visits file line by line
        while (<GCVISITSFILE>){     
            # read the current line
            my $line = $_;

            # removing the last two characters
            chop($line);
            chop($line);
        
            # split $line into comma separated strings
            my @currentline = split(/,/,$line);
        
            # check whether the $cache_id is listed in the beginning of the $currentline
            
            if ($currentline[0] eq "$cache_id") {
                # found the $cache_id
                print   "   Found GCid $currentline[0] in geocache_visits.txt file\n".
                        "   Reading time of found...\n";
                
                $foundtime_from_garmin = $currentline[1];
            }     
        }
        close(GCVISITSFILE);   # close file again
    } else {
        # setting return code to "n/a" when the geocache_visits.txt
        # file is not accessible 
        $foundtime_from_garmin = "n/a";
    }
    return $foundtime_from_garmin;
}




Soweit funktioniert fast alles. Aber beim Auseinandernehmen und neu zusammen setzen des Datums in der Funktion "read_date_from_notetxt" (Zeile 11ff) kommt es zu diesem Fehler:
Quote
Looking up Der_Rabe_GC39XWB in the SQLite database...: not found!
Hopping now into add_new_cache function...

DEBUG add_new_cache 1: starting function add_new_cache
DEBUG add_new_cache 2: Full path to the file: /home/marcus/Dokumente/Geocaching/gcdir2/found/Der_Rabe_GC39XWB/cache.txt
DEBUG add_new_cache 3: GC39XWB
DEBUG add_new_cache 4: return value from get_foundtime_from_garmin: n/a


DATESTRING: 17.03.2012
Use of uninitialized value in print at /home/marcus/bin/gcfounds2mysql.pl line 200.
Date-Array 2nd position:
Use of uninitialized value $datearray[2] in concatenation (.) or string at /home/marcus/bin/gcfounds2mysql.pl line 202.
Use of uninitialized value in concatenation (.) or string at /home/marcus/bin/gcfounds2mysql.pl line 202.
Use of uninitialized value in concatenation (.) or string at /home/marcus/bin/gcfounds2mysql.pl line 202.
--T00:00Z


Der "DATESTRING" (=das zweite Feld des Arrays) wird noch richtig dargestellt.
- Aber anscheinend wird dieses dann im folgenden split nicht weiter aufgeteilt, so dass die Felder des "@datearray" dann leer sind.



Meine Fragen:
1) natürlich: was mache ich falsch ?
2) mein Plan war bisher: über Regex den String aufteilen in $jahr, $monat, $tag etc, und dann in Epoche umwandeln.
Aber das scheint mühsam. Geht das auch geschickter ?
3) ich hadere mit diesem "T" und dem "Z". Irgendwo im Netz fand ich einen Hinweis auf "Zulu-Zeit" für das "Z", aber das war ein Java-Forum. Da war zwar ebenfalls ISO 8601 erwähnt, aber ich habe im CPAN zu dem Z nichts weiter gefunden. Habt ihr eine Idee ?


Vielen Dank vorweg und gute Nacht!

Gruß,
Marcus



PS: falls ihr antwortet nicht wundern, wenn ich mich nicht direkt melde. Ich programmiere fast nur nach 22 Uhr :-)

modedit Editiert von pq: more-tags hinzugefügt
Last edited: 2012-04-16 02:05:41 +0200 (CEST)

View full thread Datums- und Zeitformatierung