# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Get the current date and time.  We leave the integer  timestamp  in #
# $now,  and  the OSI date/time in $cymdhms.  We also leave a shorter #
# date/time string without the century and year in $mdhms. Our return #
# value is $now, the Unix integer timestamp.                          #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

sub DT {
	local($ss,$mm,$hh);		# Should these be global? Not needed yet ...
	($ss,$mm,$hh,$DD,$MM,$CY) = gmtime($now = time);
	$CY += 1900;
	$MM += 1;
	$md = sprintf('%02d%02d',$MM,$DD); 
	$hm = sprintf('%02d%02d',$hh,$mm);
	$hh_mm = sprintf('%02d:%02d',$hh,$mm);
	$hms = sprintf('%s%02d',$hm,$ss);
	$cymd = sprintf('%04d%02d%02d',$CY,$MM,$DD); 
	$mdhms = sprintf('%02d%02d%02d%02d%02d',$MM,$DD,$hh,$mm,$ss); 
	$cymdhm = "$cymd$hm"; 
	$cymdhms = sprintf('%04d%02d%02d%02d%02d%02d',$CY,$MM,$DD,$hh,$mm,$ss); 
	$cymdhms = "$cymdhm$ss"; 
	$cymd_hms = "$cymd $hms";
	return $now;
}

sub localDT { my $F='localDT'; local($dtz) = @_;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# This accepts a time string in ISO format, and returns it broken apart  into #
# YYYY, MM, DD, hh, mm, ss, and time zone. The time zone is translated to our #
# local time, however that's set, so the return  value  is  local  time,  not #
# universal.  Most of the times in our database are UTC, in ISO format.       #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($Y,$M,$D,$h,$m,$s,$Z) = (0,0,0,0,0,0,'UT');
	local($time_t,$dst);
	&sendLog("$F: Input: '$dtz'") if $V>0;
	if ($dtz =~ s"^(\d\d\d\d)[-/]*"") {
		$Y = int($1);
		if ($dtz =~ s"^(\d\d)[-/]*"") {
			$M = $1 - 1;	# Convert month to 0-11
			if ($dtz =~ s"^(\d\d)\s*"") {
				$D = int($1);
				if ($dtz =~ s"^(\d\d)[:\.]*"") {
					$h = int($1);
					if ($dtz =~ s"^(\d\d)[:\.]*"") {
						$m = int($1);
						if ($dtz =~ s"^(\d\d)\s*"") {
							$s = int($1);
						}
					}
				}
			}
		}
	}
	($Z = $dtz) =~ /^\s*(.*)[\r\s]*$/;
	&sendLog("$F: Input: Y='$Y' M='$M' D='$D' h='$h' m='$m' s='$s' Z='$Z'") if $V>0;
	if ($Z eq 'Z' || $Z eq 'UT' || $Z eq 'UTC' || $Z eq 'GMT') {
		$time_t = timegm($s,$m,$h,$D,$M,$Y);
		&sendLog("$F: unix time_t: $time_t.") if $V>0;
#		$ENV{TZ} = ':/usr/share/zoneinfo/US/Eastern';
		($s,$m,$h,$D,$M,$Y,undef,undef,$dst) = localtime($time_t);
		&sendLog("$F: localtime Y='$Y' M='$M' D='$D' h='$h' m='$m' s='$s' Z='$Z' dst='$dst'") if $V>0;
		$M += 1;
		$Y += 1900;
		$Z = '';
		&sendLog("$F: Local: Y='$Y' M='$M' D='$D' h='$h' m='$m' s='$s' Z='$Z' dst='$dst'") if $V>0;
	}
				if ($showAMPM) {
					&sendLog("$F: AMPM wanted.") if $V>0;
					if ($h > 12) {
						&sendLog("$F: h=$h > 12") if $V>0;
						$h -= 12;
						$Z = 'pm ' . $Z;
						&sendLog("$F: h=$h Z=$Z") if $V>0;
					} elsif ($h == 12) {
						&sendLog("$F: h=$h == 12") if $V>0;
						$Z = (($m > 0 || $s > 0) ? 'pm ' : 'noon ') . $Z;
						&sendLog("$F: h=$h Z=$Z") if $V>0;
					} else {
						$Z = 'am ' . $Z;
					}
				}
	&sendLog("$F: Return Y='$Y' M='$M' D='$D' h='$h' m='$m' s='$s' Z='$Z'") if $V>0;
	return($Y,$M,$D,$h,$m,$s,$Z);
}

sub when2ISO {my $F='when2ISO';
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = #
# This is a kludge that takes the "yyyy/mm/dd hh:mm[:ss] AM/PM" time  format, #
# and  converts it to ISO format without punctuation.  We won't know the time #
# zone, but we can use it for human-readable date+time fields. Its use should #
# be  restricted  to situations where people will assume local time, whatever #
# that might be.                                                              #
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = #
	local($when) = @_;
	local($Y,$M,$D,$h,$m,$s,$ampm);
	&sendLog("$F: Called for '$when'") if $V>0;
	if ($when =~ s"^(\d\d\d\d)[-/]*(\d\d)[-/]*(\d\d)\s+(\d\d):*(\d\d)"") {
		$Y = $1;
		$M = $2;
		$D = $3;
		$h = $4;
		$m = $5;
		&sendLog("$F: Y='$Y' M='$M' D='$D' h='$h' m='$m'") if $V>0;
		if ($when =~ s"^:(\d\d)\s*"") {	# Seconds included?
			$s = $6;
			&sendLog("Y='$Y' M='$M' D='$D' h='$h' m='$m' s='$s'") if $V>0;
		} else {
			$s = '00';
		}
		if ($when =~ s"^\s*pm\s*""i) {	# Afternoon?
			$h += 12;
		} else {
		#	$h = int($h);			# Morning: drop initian zero in hour
		}
		if ($when =~ s"^(am|noon)""i) {	# Morning/noon
		}
		if ($when !~ /^\s*$/) {
			&sendLog("$F: \"$when\" left over.") if $V>0;
		}
		return "$Y$M$D$h$m$s", $Y, $M, $D, $h, $m, $s, $ampm;
	}
	&send_LW("$F: Can't parse \"$when\"") if $V>0;
	return undef;
}

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = #

&DT();		# Call DT to initialize globals
1;
