<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users13</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/13" rel="self"/>
  <title>furtive</title>
  <updated>Fri Feb 08 19:49:35 -0800 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor2196</id>
    <published>2008-02-08T19:49:35-08:00</published>
    <title>[Bash] On Slow bash for loop.</title>
    <content type="html">&lt;p&gt;It didn't make a difference performance wise but I really like the clarity of the cut command, I didn't know it existed.  Thanks!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>furtive</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/229-slow-bash-for-loop/refactors/2196" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code229</id>
    <published>2008-02-07T19:57:46-08:00</published>
    <updated>2008-02-19T14:44:06-08:00</updated>
    <title>[Bash] Slow bash for loop.</title>
    <content type="html">&lt;p&gt;Here's a quick and dirty bash script that takes a file and looks for a particular line, then loops through those lines and splits the contents to get the 8th column and create an sql insert statement with it.  I'm not looking to make it more generic, but the for loop is unbearably slow.  How to improve it?&lt;/p&gt;

&lt;pre&gt;SCRIPT=&amp;quot;gen_${1}.sql&amp;quot;
echo &amp;quot;--gen_${1}.sql&amp;quot; &amp;gt; $SCRIPT
typeset -i counter
counter=0

echo &amp;quot;Get all lines that start with D|4| (this may take a while)&amp;quot;
LINES=$(grep &amp;quot;D|4|&amp;quot; $1)

echo &amp;quot;Generate insert statements to $SCRIPT&amp;quot;
for LINE in $LINES; do
  REFNO=$(echo $LINE | awk '{split($0,a,&amp;quot;|&amp;quot;);print a[8]}')
  echo &amp;quot;insert into CHEQUE (datecomptable, noreference) values (to_date('2007/11/30','YYYY/MM/DD'),'${REFNO}');&amp;quot; &amp;gt;&amp;gt; $SCRIPT
        counter=counter+1
done

echo &amp;quot;commit;&amp;quot; &amp;gt;&amp;gt; $SCRIPT
echo &amp;quot;${counter} records created in $SCRIPT&amp;quot;&lt;/pre&gt;</content>
    <author>
      <name>furtive</name>
      <email>no-email@refactormycode.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/229-slow-bash-for-loop" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor179</id>
    <published>2007-10-01T18:28:28-07:00</published>
    <title>[JavaScript] On Beautify JS Date to how recently the event occured.</title>
    <content type="html">&lt;p&gt;Wow guys, I'm impressed!  Typefreak's does a great job in being efficient operationally while Andre's appears to do the best job at exploiting the Date object itself.  I'm impressed!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>furtive</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/37-beautify-js-date-to-how-recently-the-event-occured/refactors/179" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code37</id>
    <published>2007-10-01T09:55:17-07:00</published>
    <updated>2009-05-02T02:12:53-07:00</updated>
    <title>[JavaScript] Beautify JS Date to how recently the event occured.</title>
    <content type="html">&lt;p&gt;This adds a .when() method to Date() object allowing you do things such as &amp;quot;Posted &amp;quot; + someDate.when() + &amp;quot; ago.&amp;quot; and get output such as &amp;quot;Posted 10 minutes ago.&amp;quot;  I've refactored it twice but would like to see it even smaller.&lt;/p&gt;

&lt;pre&gt;/***
* Beautify date to how recent the event occurred compared to now.
* Some examples:  1 second, 4 hours, 10 days, 1 year.
*	Example:
*   var oneFiftyMin = new Date(new Date().getTime() - 60000 * 150);
*   alert(oneFiftyMin.when()); // will display &amp;quot;2 hours&amp;quot;
*
*  Handy for things like &amp;quot;Item posted &amp;quot; + someDate.when() + &amp;quot; ago.&amp;quot;
*/
Date.prototype.when = function() {

	var diff = new Date().getTime() - this.getTime();
	var when; // our return value

	//TODO:  what if the time is in the future? 
	//if (diff &amp;lt; 0) throw new Error (&amp;quot;Date is in future, check timezone?&amp;quot;;

	//one or more of these will be non-zero, but we only care about the biggest one (in scale of time)
  var secondsOld = Math.floor(diff / 1000);
  var minutesOld = Math.floor(diff / 60000);
  var hoursOld   = Math.floor(diff / 3600000);
  var daysOld    = Math.floor(diff / 86400000);
	var monthsOld  = Math.floor(diff / 2592000000);
	var yearsOld   = Math.floor(diff / (2592000000 * 12));  //your content is old!

	//determine which value is non-zero and assign appropriate text
  if (yearsOld &amp;gt; 0) {
		when = yearsOld + &amp;quot; year&amp;quot;;
  }
  else if (monthsOld &amp;gt; 0) {
		when = monthsOld + &amp;quot; month&amp;quot;;
  }
  else if (daysOld &amp;gt; 0) {
		when = daysOld + &amp;quot; day&amp;quot;;
  }
  else if (hoursOld &amp;gt; 0) {
		when = hoursOld + &amp;quot; hour&amp;quot;;
  }
  else if (minutesOld &amp;gt; 0) {
		when = minutesOld + &amp;quot; minute&amp;quot;;
  }
  else {
		when = Math.round(diff /1000) + &amp;quot; second&amp;quot;;
	}

	//add plural if necessary
	return (0 == when.indexOf(&amp;quot;1 &amp;quot;)) ? when : when + &amp;quot;s&amp;quot;;
} 
&lt;/pre&gt;</content>
    <author>
      <name>furtive</name>
      <email>no-email@refactormycode.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/37-beautify-js-date-to-how-recently-the-event-occured" rel="alternate"/>
  </entry>
</feed>

