Display the number of characters in the name of each month
months = %w(January February March April May June July August September October November December) months.each { |m| print m, " (", m.length, ")\n" }
11307 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
months = %w(January February March April May June July August September October November December) months.each { |m| print m, " (", m.length, ")\n" }
#!/usr/bin/perl # $Id$ # Rewrite Apache logs that have incorrect dates. # Example usage: $0 '28/May/2006:01:17:14 +0200' '19/Jan/2007:08:49:14 +0100' \ # access_log.* error_log.* use strict; use warnings; ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) our ($VERSION) = '$Revision$' =~ m{ \$Revision: \s+ (\S+) }xms; ## use critic use HTTP::Date; my @DAYS = qw(Sun Mon Tue Wed Thu Fri Sat); my @MONTHS = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my $wrong_datetime = shift @ARGV; my $right_datetime = shift @ARGV; my ($timezone) = $right_datetime =~ m/ ([+-]\d\d\d\d)\z/xms; my $seconds_diff = str2time($right_datetime) - str2time($wrong_datetime); foreach my $file (@ARGV) { print "Rewriting $file\n"; open my $IN, '<', $file or die "Can't open $file: $!\n"; open my $OUT, '>', "$file.rewritten" or die "Can't write to $file.rewritten: $!\n"; while (<$IN>) { if (m{ \A (.+\s+) # Before date and time (if any) \[ ( \d\d/\w\w\w/\d\d\d\d # Date :\d\d:\d\d:\d\d # Time \s [\+\-]\d\d\d\d # Time zone ) \] (\s+.+) # After date and time \z }xms) { print {$OUT} $1, q{[}, rewrite_access_datetime($2, $seconds_diff, $timezone), q{]}, $3; } elsif (m{ \A \[ ( \w\w\w \s \w\w\w \s \d\d \s # Date \d\d:\d\d:\d\d \s # Time \d\d\d\d # Year ) \] (\s+.+) # After date and time \z }xms) { print {$OUT} q{[}, rewrite_error_datetime($1, $seconds_diff), q{]}, $2; } else { print {$OUT} $_; } } } sub rewrite_access_datetime { my ($datetime, $seconds_diff, $timezone) = @_; my ($sign, $hours, $minutes) = $timezone =~ m/\A([+-])(\d\d)(\d\d)\z/xms; my $seconds_offset = ($hours * 60 + $minutes) * 60; $datetime = str2time($datetime) + $seconds_diff; if ($sign eq q{+}) { $datetime = $datetime + $seconds_offset; } elsif ($sign eq q{-}) { $datetime = $datetime - $seconds_offset; } my ($sec, $min, $hour, $mday, $mon, $year) = gmtime $datetime; return sprintf '%02d/%s/%04d:%02d:%02d:%02d %s', $mday, $MONTHS[$mon], $year + 1900, $hour, $min, $sec, $timezone; } sub rewrite_error_datetime { my ($datetime, $seconds_diff) = @_; $datetime = str2time($datetime) + $seconds_diff; my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime $datetime; return sprintf '%s %s %02d %02d:%02d:%02d %04d', $DAYS[$wday], $MONTHS[$mon], $mday, $hour, $min, $sec, $year + 1900; }
class Date def distance_to(end_date) years = end_date.year - year months = end_date.month - month days = end_date.day - day if days < 0 days += 30 months -= 1 end if months < 0 months += 12 years -= 1 end {:years => years, :months => months, :days => days} end end
(6.months.ago.to_date..1.year.ago.to_date).to_s(:db) => "BETWEEN '2005-07-27' AND '2005-01-22'"
# workdays # returns the number of working days between two dates FUNCTION workdays( dt_begin, dt_end ) DEFINE dt_begin DATE, dt_end DATE, dt_first_sunday DATE, dt_last_saturday DATE, int_workdays INTEGER # get first sunday LET dt_first_sunday = dt_begin + ((7 - WEEKDAY(dt_begin)) MOD 7) # get last saturday LET dt_last_saturday = dt_end + ((-1 * (WEEKDAY(dt_end) + 1)) MOD 7) # get work weeks between first sunday and last saturday LET int_workdays = (((dt_last_saturday - dt_first_sunday) + 1) / 7) * 5 # if first sunday is not begin date IF dt_first_sunday <> dt_begin THEN # assume first sunday is after begin date # add workdays from begin date to first sunday LET int_workdays = int_workdays + (6 - WEEKDAY(dt_begin)) END IF # if last saturday is not end date IF dt_last_saturday <> dt_end THEN # assume last saturday is before end date # add workdays from last saturday to end date LET int_workdays = int_workdays + WEEKDAY(dt_end) END IF # return working days RETURN int_workdays END FUNCTION
' WorkDays ' returns the number of working days between two dates Public Function WorkDays(ByVal dtBegin As Date, ByVal dtEnd As Date) As Long Dim dtFirstSunday As Date Dim dtLastSaturday As Date Dim lngWorkDays As Long ' get first sunday in range dtFirstSunday = dtBegin + ((8 - Weekday(dtBegin)) Mod 7) ' get last saturday in range dtLastSaturday = dtEnd - (Weekday(dtEnd) Mod 7) ' get work days between first sunday and last saturday lngWorkDays = (((dtLastSaturday - dtFirstSunday) + 1) / 7) * 5 ' if first sunday is not begin date If dtFirstSunday <> dtBegin Then ' assume first sunday is after begin date ' add workdays from begin date to first sunday lngWorkDays = lngWorkDays + (7 - Weekday(dtBegin)) End If ' if last saturday is not end date If dtLastSaturday <> dtEnd Then ' assume last saturday is before end date ' add workdays from last saturday to end date lngWorkDays = lngWorkDays + (Weekday(dtEnd) - 1) End If ' return working days WorkDays = lngWorkDays End Function