# -*- cperl -*-
# -----------------------------------------------------------------------------
# $Id: SQLTime.pm,v 1.1.1.1 2004/09/27 14:01:00 admin Exp $
# -----------------------------------------------------------------------------
package SQLTime;
use strict;
use warnings;
use Time::Local;
use Carp;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(sqlmaketime sqlparsetime);

sub sqlmaketime ($) {
    my $epoch = shift;
    my ($sec, $min, $hour, $mday, $mon, $year) = localtime($epoch);
    $year += 1900;
    $mon++;
    sprintf(
	'%04d-%02d-%02d %02d:%02d:%02d',
	$year, $mon, $mday, $hour, $min, $sec);
}

sub sqlparsetime ($) {
    my $str = shift;
    if ($str =~ m/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/) {
	timelocal($6, $5, $4, $3, $2 - 1, $1 - 1900);
    }
    elsif ($str =~ m/^(\d{4})(\W)(\d{2})\2(\d{2}) (\d{2})(\W)(\d{2})\6(\d{2})$/) {
	timelocal($8, $7, $5, $4, $3 - 1, $1 - 1900);
    }
    else {
	croak "Unknown time format: $str";
    }
}

1;
