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!)

Limit Google searches by date

See: Easy Way to Find Recent Web Pages

date='d15'
date='m3'
date='y'
words='unix linux shell bash'
site='codesnippets.joyent.com'

open -a Safari "http://www.google.com/search?q=${words// /+}&as_qdr=${date}"

open -a Safari "http://www.google.com/search?q=site%3A${site}%20${words// /+}&as_qdr=${date}"


# sbd - search by date
# cf. also http://codesnippets.joyent.com/posts/show/1700

function sbd() {
   declare date site words
   date="${1}"
   words="${2}"
   site="${3}"
   if [[ $# -eq 2 ]]; then
      /usr/bin/open -a Safari "http://www.google.com/search?q=${words// /+}&as_qdr=${date}"
   elif [[ $# -eq 3 ]]; then
      /usr/bin/open -a Safari "http://www.google.com/search?q=site%3A${site}%20${words// /+}&as_qdr=${date}"
   else
      return 1
   fi
   return 0
}


sbd d 'unix linux shell bash'
sbd m5 'unix linux shell bash'
sbd y3 'unix linux shell bash' codesnippets.joyent.com

fsinfo

fsinfo - get file status information with Apple's FSMegaInfo sample code
See: FSMegaInfo by Apple and FSMegaInfoGUI by Ross Tulloch

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export IFS=$' \t\n'

/usr/bin/sudo /bin/mkdir -p /usr/local/bin
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local /usr/local/bin
/usr/bin/sudo /bin/chmod 0755 /usr/local /usr/local/bin

cd ~/Desktop
curl -LO http://developer.apple.com/samplecode/FSMegaInfo/FSMegaInfo.zip
unzip -qq FSMegaInfo.zip

open -e ~/Desktop/FSMegaInfo/Read\ Me\ About\ FSMegaInfo.txt


file -ik ~/Desktop/FSMegaInfo/build/Debug/FSMegaInfo
otool -Lmv $_
otool -hmVv $_
otool64 -hmVv $_
lipo -detailed_info $_


/usr/bin/sudo /bin/cp -i ~/Desktop/FSMegaInfo/build/Debug/FSMegaInfo /usr/local/bin
ls -l /usr/local/bin/FSMegaInfo

/usr/bin/sudo /bin/ln -is /usr/local/bin/FSMegaInfo /usr/local/bin/fsinfo
ls -l /usr/local/bin/fsinfo


# create a test file
testfile="${HOME}/Desktop/testfile.txt"
jot -b 'sample text' 10 | cat -n > $testfile


FSMegaInfo help
fsinfo help
fsinfo help 2>&1 | grep -i info

fsinfo -v help stat
fsinfo stat $testfile

fsinfo -v help FSGetCatalogInfo
fsinfo -v help FSGetCatalogInfo 2>&1 | grep -i finder
fsinfo -v FSGetCatalogInfo $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoGettableInfo $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoGettableInfo $testfile 2>&1 | grep -i encod


# createDate

fsinfo -v FSGetCatalogInfo -kFSCatInfoCreateDate $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoAccessDate,kFSCatInfoContentMod,kFSCatInfoAttrMod,kFSCatInfoCreateDate $testfile

stat -x $testfile | tail -n 3
stat -f $'%N:\nlast accessed:\t\t%Sa\nlast modified:\t\t%Sm\nlast inode change:\t%Sc' $testfile

touch -f $testfile

stat -x $testfile | tail -n 3
stat -f $'%N:\nlast accessed:\t\t%Sa\nlast modified:\t\t%Sm\nlast inode change:\t%Sc' $testfile

fsinfo -v FSGetCatalogInfo -kFSCatInfoCreateDate $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoAccessDate,kFSCatInfoContentMod,kFSCatInfoAttrMod,kFSCatInfoCreateDate $testfile

cwd - copy with date

unset -f cwd
function cwd() {
   declare dirname filename newfile
   if [[ ! -f "$1" ]]; then echo "No such file: ${1}"; return 1; fi
   if [[ $# -eq 1 ]]; then 
      dirname="$(/usr/bin/dirname "${1}")"
   elif [[ $# -eq 2 ]]; then
      if [[ ! -d "$2" ]]; then echo "No such directory: ${2}"; return 1; fi
      dirname="${2%/}"
   else
      echo "argument error"
      return 1
   fi
   #/bin/sleep 1
   filename="$(/usr/bin/basename "${1}")"
   newfile="${dirname}/${filename}.$(/bin/date +%Y-%m-%d-%H.%M.%S)"
   #newfile="${dirname}/${filename}.$(/bin/date +%Z-%Y-%m-%d-%H.%M.%S)"
   /bin/cp -ip "${1}" "${newfile}"
   return 0
}


cwd file
cwd file dir

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'