Never been to CodeSnippets before?

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 (or not, you can keep them private!)

month & day

# cf. http://www.macgeekery.com/tips/cli/a_neat_bash_one-liner

function month() {
#/usr/bin/cal | sed -E -e 's/^/  /' -e 's/$/  /' -e "s/ $(/bin/date +%e) /$(printf '\e[1m&\e[m')/" 
#/usr/bin/cal | sed -E -e 's/^/  /' -e 's/$/  /' -e "s/ $(/bin/date +%e) /$(printf '\e[1;31m&\e[m')/" 
#/usr/bin/cal | sed -E -e 's/^/  /' -e 's/$/  /' -e "s/ $(/bin/date +%e) /$(printf '\e[1;32m&\e[m')/" 
#/usr/bin/cal | sed -E -e 's/^/  /' -e 's/$/  /' -e "s/ $(/bin/date +%e) /$(printf '\e[1;33m&\e[m')/" 
#/usr/bin/cal | sed -E -e 's/^/  /' -e 's/$/  /' -e "s/ $(/bin/date +%e) /$(printf '\e[1;34m&\e[m')/" 
#/usr/bin/cal | sed -E -e 's/^/  /' -e 's/$/  /' -e "s/ $(/bin/date +%e) /$(printf '\e[1;35m&\e[m')/" 
/usr/bin/cal | sed -E -e 's/^/  /' -e 's/$/  /' -e "s/ $(/bin/date +%e) /$(printf '\e[1;36m&\e[m')/" 
return 0
}

month

alias day=month
day

Get a date without time in SQL

--Extracts the date from the date time
CAST(FLOOR(CAST(GETDATE() AS float)) AS datetime)

backup files

// create backup copy

#!/bin/bash

filename=$1
date=`date +%Y%m%d`

usage () {
        echo "Usage: `basename $0` filename"
}

if [ -z "$filename" -a ! -f "$filename" ]; then
        usage
        exit 1
fi

rev=0
backup="$filename.$date.$rev"

while [ -f $backup ]; do
        let rev+=1
        backup="$filename.$date.$rev"
done

cp $filename $backup
exit $?

Selecting different parts of a DATETIME with MSSQL

Thanks to this website for the information:

http://www.databasejournal.com/features/mssql/article.php/1442021

-- The syntax for pulling a certain part of a DATETIME is:
--
-- CONVERT(date_type[(length)],expression[,style])
--
-- The available styles are as follows:
--
-- NULL  Jun 24 2001 9:48PM
-- 1     06/24/01
-- 101   06/24/2001
-- 2     01.06.24
-- 104   24.06.2001
-- 108   21:48:00
-- 112   20010624
-- 121   2001-06-24 21:48:00.000

-- Some example usage:
SELECT CONVERT(DATETIME,GETDATE(),112) as date
-- Will output in YYYYMMDD format
SELECT CONVERT(DATETIME,client.birthday,101) as birthday
-- Will output in MM/DD/YYYY format

Calculate the number of working days between two dates

Function
   # wdays is an array with the days of the week
   # to exclude days (eg: wdays = [0,6] for sunday and saturday )

   def calculate_working_days(d1,d2,wdays)
        diff = d2 - d1
        holidays = 0
        ret = (d2-d1).divmod(7)
        holidays =  ret[0].truncate * wdays.length
        d1 = d2 - ret[1]
        while(d1 <= d2)
                if wdays.include?(d1.wday)
                        holidays += 1
                end
                d1 += 1
        end
        diff - holidays
   end


Iterates over date range.
d1 = Date.new( 2006, 12, 1 ) 

d2 = Date.new( 2007, 1, 15 )

weekdays = (d1..d2).reject { |d| [0,6].include? d.wday } 

weekdays.length

Nice Date

 sub nice_date {   
		(my $date) = @_;   
		($sec, $min, $hr, $day, $month, $year, $day_Of_Week, $julianDate, $dst) = localtime($date); 
		$month+=1; $year+=1900;   
		($sec2, $min2, $hr2, $day2, $month2, $year2, $day_Of_Week2, $julianDate2, $dst2) = localtime();
		$month2+=1; $year2+=1900;
		if($julianDate2==$julianDate and $year==$year2) {   
			if($hr==$hr2) {print "a few moments ago";}   
			elsif(($hr2-$hr)==1) {print "an hour ago";}   
			elsif(($hr2-$hr)==2) {print "two hours ago}";}   
			elsif(($hr2-$hr)==3) {print "three hours ago";}   
			else {print "today";}   
		}
		elsif($julianDate2==($julianDate+1) and $year==$year2) {print "yesterday";} 
		elsif(($julianDate2-$julianDate)<=2 and $year==$year2) {print "two days ago";}
		elsif(($julianDate2-$julianDate)<=5 and $year==$year2) {print "this week";}   
		else {print "$month/$day/$year";}  
	}

TimeDate format in Oracle - zmiana

 ALTER SESSION SET NLS_DATE_FORMAT='MM/DD/YYYY-HH24:MI'

Calculate Thanksgiving Date

This is the most concise way I've seen to calculate Thanksgiving date (in PHP).

strtotime("3 weeks thursday",mktime(0,0,0,11,1,$year)

date time in ruby

------------------------------------------------------------------- Time#strftime
time.strftime( string ) => string
---------------------------------------------------------------------------------
Formats time according to the directives in the given format string. Any text not listed as a directive will be passed through to the output string.

"
Format meaning:

%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character

t = Time.now
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56AM"
"

Measure the daily number of E-mail messages in a mailbox

This snippet written in bash with calls to perl from the command line measures the number of E-mail messages sent to a mailbox per calendrical day.

#!/bin/bash
grep -h '^Date:' * |
    perl -pe 's!^Date: !!' |
    perl -pe 's!^\w\w\w, !!' |
    perl -pe 's{\d{2}:\d{2}:\d{2}.*$}{}' |
    perl -pe 's!^\s+!!' |
    perl -pae '$_=sprintf("%.2d-%s-%s\n", @F)' |
    sort | uniq -c | sort -n


I used perl for some places where sed would have been more suitable because I find the sed regexp syntax confusing. :-)