Gossamer Forum
Home : Products : DBMan SQL : Discussion :

Date converter

Quote Reply
Date converter
I needed a little script, that converts my date-field of the flat version of dbman to the MySQL date-notation.
Perhaps someone else can use this.
Code:

#!/usr/local/bin/perl

# This little script converts the date notation of the
# flat dbman to the MySQL needed date notation
# Upload this file to the directory where you database
# stays and give it the right permissions (755)
# Run this script with Telnet and the date field is
# converted from 01-Jan-2000 to 2000-01-01.

my $date_field = 3; # The number of the field that hold your date

&do;

sub do {

# This is the path to your database-file
$db_rec_date = "/path/to/your/server/cgi-bin/dbman/db_name.db";

open (CHE,"<$db_rec_date") or die;
@lines = <CHE>;

foreach $line (@lines) {
chomp ($line);
@data = split (/\|/, $line);

@repair = split (/-/, $data[$date_field]);

if ($repair[0] ne "2000") {

if ($repair[1] eq "Jan") {$repair[1] = "01";}
if ($repair[1] eq "Feb") {$repair[1] = "02";}
if ($repair[1] eq "Mrt") {$repair[1] = "03";}
if ($repair[1] eq "Apr") {$repair[1] = "04";}
if ($repair[1] eq "May") {$repair[1] = "05";}
if ($repair[1] eq "Jun") {$repair[1] = "06";}
if ($repair[1] eq "Jul") {$repair[1] = "07";}
if ($repair[1] eq "Aug") {$repair[1] = "08";}
if ($repair[1] eq "Sep") {$repair[1] = "09";}
if ($repair[1] eq "Okt") {$repair[1] = "10";}
if ($repair[1] eq "Nov") {$repair[1] = "11";}
if ($repair[1] eq "Dec") {$repair[1] = "12";}

$data[$date_field] = "$repair[2]-$repair[1]-$repair[0]";

}

$output .= join ("|", @data) ."\n";

}

open (CHE1,">$db_rec_date") or die;

print CHE1 "$output";
close CHE1;

}
----------
Mart.