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

# /.../_/sex2hms == Format seconds into "xx week(s) xx day(s) hh:mm:ss" #

// # sex2hms == Bourne Shell Script == Format seconds into "xx week(s) xx day(s) hh:mm:ss"


#!/bin/sh
##############################################################################
# /.../_/sex2hms == Format seconds into "xx week(s) xx day(s) hh:mm:ss"      #
#----------------------------------------------------------------------------#
#.........william.o.yates...spamware.at.tru2life.net...tru2life.info.........#
#----------------------------------------------------------------------------#
# and YES, sex2hms coulda, maybe shoulda be called sex2wdhms, but oh well... #
#----------------------------------------------------------------------------#
# original (for bash): http://codesnippets.joyent.com/tag/bash/3#post1936    #
#                                                                            #
# #!/bin/sh                                                                  #
# #Convert seconds to h:m:s format                                           #
#                                                                            #
# [ -z ${1} ] && echo "Usage: $(basename $0) <seconds>" && exit||secs=${1}   #
# _hms()                                                                     #
# {                                                                          #
#  local S=${1}                                                              #
#  ((h=S/3600))                                                              #
#  ((m=S%3600/60))                                                           #
#  ((s=S%60))                                                                #
#  printf "%dh:%dm:%ds\n" $h $m $s                                           #
# }                                                                          #
#                                                                            #
# _hms ${secs}                                                               #
#                                                                            #
#----------------------------------------------------------------------------#
#                                                                            #
#          1 second  = 1 second  = "S=$(expr \( ${SEX} % 60             \))" #
#         60 seconds = 1 minute  = "M=$(expr \( ${SEX} % 3600 / 60      \))" #
#      3,600 seconds = 1 hour    = "H=$(expr \( ${SEX} % 86400 / 3600   \))" #
#     86,400 seconds = 1 day     = "D=$(expr \( ${SEX} % 604800 / 86400 \))" #
#    604,800 seconds = 1 week    = "W=$(expr \( ${SEX} / 604800         \))" #
#                                                                            #
# Following are beyond scope of this script, but jus4giggles...              #
#                                                                            #
#  2,419,200 seconds = 1 month28 = "[ ${SEX} -gt 2419200 ] && calc_month"... #
#  2,505,600 seconds = 1 month29 = "[ ${SEX} -gt 2505600 ] && calc_month"... #
#  2,592,000 seconds = 1 month30 = "[ ${SEX} -gt 2592000 ] && calc_month"... #
#  2,678,400 seconds = 1 month31 = "[ ${SEX} -gt 2678400 ] && calc_month"... #
# 31,536,000 seconds = 1 year365 = "[ ${SEX} -gt 31536000 ] && calc_year"... #
# 31,622,400 seconds = 1 year366 = "[ ${SEX} -gt 31622400 ] && calc_year"... #
#                                                                            #
#----------------------------------------------------------------------------#
#                                                                            #
# a heavy duty hand calculation for what correct answers should be:          #
#                                                                            #
# 999,999 / 604,800 =       1.6534375 == ( W=1 )                             #
#                                                                            #
#       1 * 604,800 = 604,800         == ( 1 week in seconds )(doh... :-)    #
#                                                                            #
# 999,999 - 604,800 = 395,199         == ( SEX - 1W )                        #
#                                                                            #
# 395,199 / 86,400  =       4.5740625 == ( D=4 )                             #
#                                                                            #
#       4 *  86,400 = 345,600         == ( 4 days in seconds )               #
#                                                                            #
# 395,199 - 345,600 =  49,599         == ( SEX - 1W - 4D )                   #
#                                                                            #
#  49,599 /   3,600 =      13.7775    == ( H=13 )                            #
#                                                                            #
#      13 *   3,600 =  46,800         == ( 13 hours in seconds )             #
#                                                                            #
#  49,599 -  46,800 =   2,799         == ( SEX - 1W - 4D - 13H )             #
#                                                                            #
#   2,799 /      60 =      46.65      == ( M=46 )                            #
#                                                                            #
#      46 *      60 =   2,760         == ( SEX - 1W - 4D - 13H - 46M )       #
#                                                                            #
#   2,799 -   2,760 =      39         == ( S=39 )                            #
#                                                                            #
# So, (finally!) with the following sex2hms runs, the answers are:           #
# (with your directory prefixed from where you run it from)                  #
#                                                                            #
# /.../_/sex2hms: 9 seconds = 00 week 00 day 00:00:09                        #
# /.../_/sex2hms: 99 seconds = 00 week 00 day 00:01:39                       #
# /.../_/sex2hms: 999 seconds = 00 week 00 day 00:16:39                      #
# /.../_/sex2hms: 9999 seconds = 00 week 00 day 02:46:39                     #
# /.../_/sex2hms: 99999 seconds = 00 week 01 day 03:46:39                    #
# /.../_/sex2hms: 999999 seconds = 01 week 04 days 13:46:39                  #
# /.../_/sex2hms: 9999999 seconds = 16 weeks 03 days 17:46:39                #
# /.../_/sex2hms: 99999999 seconds = 165 weeks 02 days 09:46:39              #
# /.../_/sex2hms: 999999999 seconds = 1653 weeks 03 days 01:46:39            #
#----------------------------------------------------------------------------#
#.........william.o.yates...spamware.at.tru2life.net...tru2life.info.........#
#============================================================================#

#-----------------------------------------------------------------------------
# hms() == convert seconds into hours:minutes:seconds (hh:mm:ss) format
#-----------------------------------------------------------------------------
hms()
{
  local SEX=${1};

  local HOUR=3600; local DAY=86400; local WEEK=604800;

  local DAZE="day"; local WEKZ="week";

  W=$(expr \( ${SEX} / ${WEEK} \))

  D=$(expr \( ${SEX} % ${WEEK} / ${DAY} \))

  H=$(expr \( ${SEX} % ${DAY} / ${HOUR} \))

  M=$(expr \( ${SEX} % ${HOUR} / 60 \))

  S=$(expr \( ${SEX} % 60 \))

  [ ${D} -gt 1 ] && DAZE="days"

  [ ${W} -gt 1 ] && WEKZ="weeks"

  #===========================================================================
  # printf format [arguments ...]
  # '%s'=string, '%d'=decimal_number, '%02d'=0_padded_2_digit_decimal_number
  # character_escape_sequences: '\v'=vertical_tab, '\t'=tab, '\n'=newline
  #---------------------------------------------------------------------------
  #-------------------SEX-----------W--WEKZ--D--DAZE--H----M----S-------------
  printf "\v\t${0}: %d seconds = %02d %s %02d %s %02d:%02d:%02d\v\n" \
    ${SEX} ${W} ${WEKZ} ${D} ${DAZE} ${H} ${M} ${S}

  echo -e "\v\tSEX=${SEX}, W=${W}, WEKZ=${WEKZ}, D=${D}, DAZE=${DAZE}, \
H=${H}, M=${M}, S=${S} \v"
}

#=============================================================================
# [ -z STRING ] == True if the length of STRING is zero.
#-----------------------------------------------------------------------------
[ -z ${1} ]&&echo -e "\n\tUsage: $(basename ${0}) <seconds>\n"&&exit||hms ${1}

##############################################################################

CNN Live via mplayer

# cf. MPlayer on Mac OS X, http://codesnippets.joyent.com/posts/show/1896

function cnnlive() { /usr/local/bin/mplayer -cache 500 -cache-min 90 -playlist http://www.cnn.com/video/live/cnnlive_1.asx &>/dev/null; return 0; }
function cnnlive2() { /usr/local/bin/mplayer -cache 500 -cache-min 90 -playlist http://www.cnn.com/video/live/cnnlive_2.asx &>/dev/null; return 0; }
function cnnlive3() { /usr/local/bin/mplayer -cache 500 -cache-min 90 -playlist http://www.cnn.com/video/live/cnnlive_3.asx &>/dev/null; return 0; }
function cnnlive4() { /usr/local/bin/mplayer -cache 500 -cache-min 90 -playlist http://www.cnn.com/video/live/cnnlive_4.asx &>/dev/null; return 0; }


cnnlive
cnnlive2
cnnlive3
cnnlive4



#---------------------------------------------------------------------------



# CNN Daily
# cf. http://edition.cnn.com/services/podcasting/

function cnndaily() { 

   declare CACHE WEBFILE

   CACHE=700

   #WEBFILE="$(/usr/bin/curl -L -s --max-time 5 http://edition.cnn.com/services/podcasting/popups/cnn.daily.html | /usr/bin/egrep -o -m 1 \
   #         'http://ht.cdn.turner.com/cnn/big/podcasts/cnnnewsroom/video/[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}/the\.daily\..+\.m4v')"

   WEBFILE="$(/usr/bin/curl -L -s --max-time 5 http://edition.cnn.com/services/podcasting/popups/cnn.daily.html | /usr/bin/egrep -o -m 1 'http://.+\.m4v' | /usr/bin/head -n 1)"

   if [[ -n "${WEBFILE}" ]]; then
      printf "%s" "${WEBFILE}" | /usr/bin/sed -E -e "s|^.+([[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}).+$|$(printf '\e[1m\\1\e[m')|"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   else
      echo 'No web file from: http://edition.cnn.com/services/podcasting/popups/cnn.daily.html'
      return 1
   fi


: <<-'COMMENT'

   declare CACHE DATE1 DATE2 HEADER WEBFILE

   CACHE=700

   DATE1="$(/bin/date "+%Y/%m/%d")"
   DATE2="$(/bin/date "+%m.%d")"
   WEBFILE="http://ht.cdn.turner.com/cnn/big/podcasts/cnnnewsroom/video/${DATE1}/the.daily.${DATE2}.cnn.m4v"
   HEADER="$(/usr/bin/curl -I -s --max-time 5 "${WEBFILE}" 2>/dev/null | /usr/bin/head -n 1)"

   if [[ "${HEADER}" == *OK* ]]; then
      printf "\e[1m%s\e[m\n" "${DATE1}"
      #echo "${WEBFILE}"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   fi


   DATE1="$(/usr/bin/env TZ=posixrules+24 /bin/date "+%Y/%m/%d")"
   DATE2="$(/usr/bin/env TZ=posixrules+24 /bin/date "+%m.%d")"
   WEBFILE="http://ht.cdn.turner.com/cnn/big/podcasts/cnnnewsroom/video/${DATE1}/the.daily.${DATE2}.cnn.m4v"
   HEADER="$(/usr/bin/curl -I -s --max-time 5 "${WEBFILE}" 2>/dev/null | /usr/bin/head -n 1)"

   if [[ "${HEADER}" == *OK* ]]; then
      printf "\e[1m%s\e[m\n" "${DATE1}"
      #echo "${WEBFILE}"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   else
      echo "No such web file: ${WEBFILE}"
      return 1
   fi

   return 0

COMMENT

}

cnndaily
cnndaily 200



#---------------------------------------------------------------------------



# Now In The News
# http://edition.cnn.com/services/podcasting/popups/nitn.html

function nitn() { 

   declare CACHE WEBFILE

   CACHE=700

   #WEBFILE="$(/usr/bin/curl -L -s --max-time 5 http://edition.cnn.com/services/podcasting/popups/nitn.html | /usr/bin/egrep -o -m 1 \
   #         'http://ht.cdn.turner.com/cnn/big/podcasts/nitn/video/[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}/nitn\..+\.m4v')"

   WEBFILE="$(/usr/bin/curl -L -s --max-time 5 http://edition.cnn.com/services/podcasting/popups/nitn.html | /usr/bin/egrep -o -m 1 'http://.+\.m4v' | /usr/bin/head -n 1)"

   if [[ -n "${WEBFILE}" ]]; then
      printf "%s" "${WEBFILE}" | /usr/bin/sed -E -e "s|^.+([[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}).+$|$(printf '\e[1m\\1\e[m')|"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   else
      echo 'No web file from: http://edition.cnn.com/services/podcasting/popups/nitn.html'
      return 1
   fi


: <<-'COMMENT'

   declare CACHE DATE HEADER WEBFILE

   CACHE=700

   DATE="$(/bin/date "+%Y/%m/%d")"
   WEBFILE="http://ht.cdn.turner.com/cnn/big/podcasts/nitn/video/${DATE}/nitn.edition.06.cnn.m4v"
   HEADER="$(/usr/bin/curl -I -s --max-time 5 "${WEBFILE}" 2>/dev/null | /usr/bin/head -n 1)"

   if [[ "${HEADER}" == *OK* ]]; then
      printf "\e[1m%s\e[m\n" "${DATE}"
      #echo "${WEBFILE}"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   fi


   DATE="$(/usr/bin/env TZ=posixrules+24 /bin/date "+%Y/%m/%d")"
   WEBFILE="http://ht.cdn.turner.com/cnn/big/podcasts/nitn/video/${DATE}/nitn.edition.06.cnn.m4v"
   HEADER="$(/usr/bin/curl -I -s --max-time 5 "${WEBFILE}" 2>/dev/null | /usr/bin/head -n 1)"

   if [[ "${HEADER}" == *OK* ]]; then
      printf "\e[1m%s\e[m\n" "${DATE}"
      #echo "${WEBFILE}"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   else
      echo "No such web file: ${WEBFILE}"
      return 1
   fi

   return 0

COMMENT

}


nitn
nitn 200



#---------------------------------------------------------------------------



# In Case You Missed It
# cf. http://edition.cnn.com/services/podcasting/

function icymi() { 


   declare CACHE WEBFILE

   CACHE=700

   #WEBFILE="$(/usr/bin/curl -L -s --max-time 5 http://edition.cnn.com/services/podcasting/popups/in.case.you.html | /usr/bin/egrep -o -m 1 \
   #        'http://ht.cdn.turner.com/cnn/big/podcasts/incaseyoumissed/video/[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}/in\.case\.you\..+\.m4v')"

   WEBFILE="$(/usr/bin/curl -L -s --max-time 5 http://edition.cnn.com/services/podcasting/popups/in.case.you.html | /usr/bin/egrep -o -m 1 'http://.+\.m4v' | /usr/bin/head -n 1)"

   if [[ -n "${WEBFILE}" ]]; then
      printf "%s" "${WEBFILE}" | /usr/bin/sed -E -e "s|^.+([[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}).+$|$(printf '\e[1m\\1\e[m')|"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   else
      echo 'No web file from: http://edition.cnn.com/services/podcasting/popups/in.case.you.html'
      return 1
   fi


: <<-'COMMENT'


   declare CACHE DATE1 DATE2 HEADER WEBFILE

   CACHE=700

   DATE1="$(/bin/date "+%Y/%m/%d")"
   DATE2="$(/bin/date "+%m.%d")"
   WEBFILE="http://ht.cdn.turner.com/cnn/big/podcasts/incaseyoumissed/video/${DATE1}/in.case.you.missed.it.${DATE2}.cnn.m4v"
   HEADER="$(/usr/bin/curl -I -s --max-time 5 "${WEBFILE}" 2>/dev/null | /usr/bin/head -n 1)"

   if [[ "${HEADER}" == *OK* ]]; then
      printf "\e[1m%s\e[m\n" "${DATE1}"
      #echo "${WEBFILE}"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   fi


   DATE1="$(/usr/bin/env TZ=posixrules+24 /bin/date "+%Y/%m/%d")"
   DATE2="$(/usr/bin/env TZ=posixrules+24 /bin/date "+%m.%d")"
   WEBFILE="http://ht.cdn.turner.com/cnn/big/podcasts/incaseyoumissed/video/${DATE1}/in.case.you.missed.it.${DATE2}.cnn.m4v"
   HEADER="$(/usr/bin/curl -I -s --max-time 5 "${WEBFILE}" 2>/dev/null | /usr/bin/head -n 1)"

   if [[ "${HEADER}" == *OK* ]]; then
      printf "\e[1m%s\e[m\n" "${DATE1}"
      #echo "${WEBFILE}"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   else
      echo "No such web file: ${WEBFILE}"
      return 1
   fi

   return 0

COMMENT

}

icymi
icymi 500



#---------------------------------------------------------------------------



# Anderson Cooper 360° Daily
# http://edition.cnn.com/services/podcasting/popups/ac360.html

function ac360() { 

   declare CACHE WEBFILE

   CACHE=700

   #WEBFILE="$(/usr/bin/curl -L -s --max-time 5 http://edition.cnn.com/services/podcasting/popups/ac360.html | /usr/bin/egrep -o -m 1 \
   #        'http://ht.cdn.turner.com/cnn/big/podcasts/ac360/video/[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}/cooper\.podcast\..+\.cnn\.m4v')"

   WEBFILE="$(/usr/bin/curl -L -s --max-time 5 http://edition.cnn.com/services/podcasting/popups/ac360.html | /usr/bin/egrep -o -m 1 'http://.+\.m4v' | /usr/bin/head -n 1)"

   if [[ -n "${WEBFILE}" ]]; then
      printf "%s" "${WEBFILE}" | /usr/bin/sed -E -e "s|^.+([[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}).+$|$(printf '\e[1m\\1\e[m')|"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   else
      echo 'No web file from: http://edition.cnn.com/services/podcasting/popups/ac360.html'
      return 1
   fi


: <<-'COMMENT'

   declare CACHE DATE HEADER WEBFILE full_weekday_name

   CACHE=700

   DATE="$(/bin/date "+%Y/%m/%d")"
   full_weekday_name="$(/bin/date "+%A" | /usr/bin/tr '[[:upper:]]' '[[:lower:]]')"
   WEBFILE="http://ht.cdn.turner.com/cnn/big/podcasts/ac360/video/${DATE}/cooper.podcast.${full_weekday_name}.cnn.m4v"
   HEADER="$(/usr/bin/curl -I -s --max-time 5 "${WEBFILE}" 2>/dev/null | /usr/bin/head -n 1)"

   if [[ "${HEADER}" == *OK* ]]; then
      printf "\e[1m%s\e[m\n" "${DATE}"
      #echo "${WEBFILE}"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   fi


   #DATE="$(/usr/bin/env TZ=posixrules+24 /bin/date "+%Y/%m/%d")"
   full_weekday_name="$(/usr/bin/env TZ=posixrules+24 /bin/date "+%A" | /usr/bin/tr '[[:upper:]]' '[[:lower:]]')"
   WEBFILE="http://ht.cdn.turner.com/cnn/big/podcasts/ac360/video/${DATE}/cooper.podcast.${full_weekday_name}.cnn.m4v"
   HEADER="$(/usr/bin/curl -I -s --max-time 5 "${WEBFILE}" 2>/dev/null | /usr/bin/head -n 1)"

   if [[ "${HEADER}" == *OK* ]]; then
      printf "\e[1m%s\e[m\n" "${DATE}"
      #echo "${WEBFILE}"
      /usr/local/bin/mplayer -cache ${1:-${CACHE}} -cache-min 95 -really-quiet "${WEBFILE}" &>/dev/null
      return 0
   else
      echo "No such web file: ${WEBFILE}"
      return 1
   fi

   return 0

COMMENT

}

ac360
ac360 400



#---------------------------------------------------------------------------



man date | less -p 'TZ'
man 7 environ | less -p 'TZ'
man 3 tzset | less -p 'offset'
man 3 tzset | less -p 'posixrules'
man 3 strftime | less -p 'conversion specifications'

open -a Finder /usr/share/zoneinfo
open -a Finder /usr/share/zoneinfo/US
open -a Finder /usr/share/zoneinfo/America

man env
help type
type -a env
type -p env
type -P env
type -t env

env TZ=posixrules+24 date "+%Y/%m/%d"
env TZ=posixrules+48 date "+%Y/%m/%d"
env TZ=posixrules+72 date "+%Y/%m/%d"
env TZ=posixrules+96 date "+%Y/%m/%d"
env TZ=posixrules+120 date "+%Y/%m/%d"

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