Database-BI

 view release on metacpan or  search on metacpan

lib/Database/BI/Calc.pm  view on Meta::CPAN

	day => sub {
		return undef unless defined $_[0] && $_[0] =~ /\A\d{4}-\d\d-(0[1-9]|[12]\d|3[01])/;
		$1 + 0;
	},
	quarter => sub {
		return undef unless defined $_[0] && $_[0] =~ /\A\d{4}-(0[1-9]|1[0-2])-/;
		int(($1 - 1) / 3) + 1;
	},
	week => sub {
		# Returns ISO 8601 week number (1-53); week containing the first
		# Thursday of the year is week 1.
		return undef unless defined $_[0] && $_[0] =~ /\A(\d{4})-(\d\d)-(\d\d)/;
		my $t = _epoch_of($1 + 0, $2 + 0, $3 + 0, 12, 0, 0);
		return undef unless defined $t;
		my @lt = Database::BI::Util::local_wall($t);
		# Not strftime('%V'): the Windows C runtime does not have it, and
		# gives ''.
		_iso_week($lt[5] + 1900, $lt[6], $lt[7]);
	},
	month_name => sub {
		# Returns full English month name, e.g. 'January'.
		my @M = qw(January February March April May June
		           July August September October November December);
		return undef unless defined $_[0] && $_[0] =~ /\A\d{4}-(0[1-9]|1[0-2])-/;
		$M[$1 - 1];
	},
	weekday => sub {
		# Returns ISO 8601 weekday: 1 = Monday, 7 = Sunday.
		return undef unless defined $_[0] && $_[0] =~ /\A(\d{4})-(\d\d)-(\d\d)/;
		my $t = _epoch_of($1 + 0, $2 + 0, $3 + 0, 12, 0, 0);
		return undef unless defined $t;
		my $wd = (Database::BI::Util::local_wall($t))[6];	# 0 = Sunday
		$wd == 0 ? 7 : $wd;
	},
	now => sub {
		# Returns today's date as 'YYYY-MM-DD'.  Takes no arguments.
		my @t = (localtime(Database::BI::Util::frame_epoch(time)))[3, 4, 5];	# mday, mon (0-based), year (since 1900)
		sprintf '%04d-%02d-%02d', $t[2] + 1900, $t[1] + 1, $t[0];
	},

	# --- time of day (see "Time values and time buckets" below) --
	hour => sub {
		my $t = _parse_time($_[0]);
		return undef unless $t && $t->{has_time};
		(Database::BI::Util::local_wall($t->{epoch}))[2] + 0;
	},
	minute => sub {
		my $t = _parse_time($_[0]);
		return undef unless $t && $t->{has_time};
		(Database::BI::Util::local_wall($t->{epoch}))[1] + 0;
	},
	second => sub {
		my $t = _parse_time($_[0]);
		return undef unless $t && $t->{has_time};
		(Database::BI::Util::local_wall($t->{epoch}))[0] + 0;
	},

	# --- grouping / binning ---
	bin => sub {
		# bin(value, width) -> '[lo,hi)' label for the interval containing value.
		# Width must be a positive number.  Both endpoints use %d when width is
		# an integer, %g otherwise, to avoid spurious ".0" suffixes.
		my ($v, $w) = @_;
		return undef unless defined $v && defined $w;
		return undef unless looks_like_number($v) && looks_like_number($w) && $w > 0;
		# Normalise the quotient to 10 significant digits before flooring: binary
		# representation error (0.3 / 0.1 == 2.9999999999999996) must not move a
		# value one bin to the left.  %.10g is magnitude-independent.
		my $n   = POSIX::floor(sprintf('%.10g', $v / $w) + 0);
		my $lo  = $n * $w;
		my $hi  = $lo + $w;
		my $fmt = ($w == int($w)) ? '%d' : '%g';
		sprintf("[${fmt},${fmt})", $lo, $hi);
	},
);

# ---------------------------------------------------------------------------
# Time values and time buckets
#
# One parser and one interval vocabulary, with the time-of-day functions and
# span() on top of them.  Every value is read as *local* time, by
# POSIX::mktime, exactly as the date functions and the relative time ranges
# already are: no zone is parsed, none is stored, and nothing here reads the
# clock -- a bucket for a value is the same bucket tomorrow.
# ---------------------------------------------------------------------------

# _fmt_body($fmt) -> ($regex_body, \@tokens)
#
# Purpose: Turn a strptime-style pattern into a regex body with one capture per
#          token, plus the token each capture belongs to.  A token outside
#          %_FMT_RE is a compile error rather than a silent skip: a pattern
#          that quietly ignored half of itself is how a value ends up in the
#          wrong bucket without anybody being told.
sub _fmt_body {
	my ($fmt) = @_;
	my @tokens;
	my $body = '';
	my $i    = 0;

	while ($i < length $fmt) {
		my $ch = substr($fmt, $i, 1);
		if ($ch ne '%') {
			$body .= quotemeta($ch);
			$i++;
			next;
		}
		my $tok = substr($fmt, $i + 1, 1);
		croak _fmt('error_time_format', length($tok // '') ? "%$tok" : $fmt)
			unless length($tok // '') && exists $_FMT_RE{$tok};
		push @tokens, $tok;
		$body .= '(' . $_FMT_RE{$tok} . ')';
		$i += 2;
	}

	return ($body, \@tokens);
}

# _fmt_compile($fmt) -> { re => qr, tokens => \@tokens }
#
# A pattern on its own has to name a date; a time with no day has no place on a
# calendar to be resolved against.



( run in 1.051 second using v1.01-cache-2.11-cpan-eb134014566 )