<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: time code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 12 Oct 2008 20:32:32 GMT</pubDate>
    <description>DZone Snippets: time code</description>
    <item>
      <title>Creating a timestamp in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/6229</link>
      <description>&lt;code&gt;&lt;br /&gt; # year + month + day + hour + minutes +seconds&lt;br /&gt; Time.now.strftime("%y%m%d%H%M%S") # =&gt; "081008115016"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;References:&lt;br /&gt; - &lt;a href="http://almosteffortless.com//2007/07/29/the-perfect-timestamp/"&gt;The Perfect Timestamp - almost effortless&lt;/a&gt; [almosteffortless.com]&lt;br /&gt;&lt;br /&gt;Additional code - Displaying a human friendly elapsed time&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def time_ago_or_time_stamp(from_time, to_time = Time.now, include_seconds = true, detail = false)&lt;br /&gt;  from_time = from_time.to_time if from_time.respond_to?(:to_time)&lt;br /&gt;  to_time = to_time.to_time if to_time.respond_to?(:to_time)&lt;br /&gt;  distance_in_minutes = (((to_time - from_time).abs)/60).round&lt;br /&gt;  distance_in_seconds = ((to_time - from_time).abs).round&lt;br /&gt;  case distance_in_minutes&lt;br /&gt;    when 0..1           then time = (distance_in_seconds &lt; 60) ? "#{distance_in_seconds} seconds ago" : '1 minute ago'&lt;br /&gt;    when 2..59          then time = "#{distance_in_minutes} minutes ago"&lt;br /&gt;    when 60..90         then time = "1 hour ago"&lt;br /&gt;    when 90..1440       then time = "#{(distance_in_minutes.to_f / 60.0).round} hours ago"&lt;br /&gt;    when 1440..2160     then time = '1 day ago' # 1-1.5 days&lt;br /&gt;    when 2160..2880     then time = "#{(distance_in_minutes.to_f / 1440.0).round} days ago" # 1.5-2 days&lt;br /&gt;    else time = from_time.strftime("%a, %d %b %Y")&lt;br /&gt;  end&lt;br /&gt;  return time_stamp(from_time) if (detail &amp;&amp; distance_in_minutes &gt; 2880)&lt;br /&gt;  return time&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;input:&lt;br /&gt;a = Time.now&lt;br /&gt;&lt;br /&gt;output:&lt;br /&gt;&gt;&gt; time_ago_or_time_stamp(a)&lt;br /&gt;=&gt; "45 seconds ago"&lt;br /&gt;&gt;&gt; time_ago_or_time_stamp(a)&lt;br /&gt;=&gt; "1 minute ago"&lt;br /&gt;&gt;&gt; time_ago_or_time_stamp(a)&lt;br /&gt;=&gt; "1 minute ago"&lt;br /&gt;&gt;&gt; time_ago_or_time_stamp(a)&lt;br /&gt;=&gt; "2 minutes ago"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 08 Oct 2008 10:57:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/6229</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Comparing a DateTime object with Time.now</title>
      <link>http://snippets.dzone.com/posts/show/6127</link>
      <description>&lt;code&gt;this_evening = DateTime.parse('Tue Sep 23 20:35:37  +0100 2008')&lt;br /&gt;t1 = Time.now&lt;br /&gt;just_now = DateTime.parse(t1.to_s)&lt;br /&gt;puts 'this evening has not happened yet.' if this_evening &gt; just_now&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 23 Sep 2008 14:03:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/6127</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Displaying the difference between times in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5958</link>
      <description>This function converts the number of seconds into hours, minutes, and seconds.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;require 'time'&lt;br /&gt;&lt;br /&gt;def seconds_fraction_to_time(seconds)&lt;br /&gt;  hours = mins = 0&lt;br /&gt;  if seconds &gt;=  60 then&lt;br /&gt;    mins = (seconds / 60).to_i &lt;br /&gt;    seconds = (seconds % 60 ).to_i&lt;br /&gt;&lt;br /&gt;    if mins &gt;= 60 then&lt;br /&gt;      hours = (mins / 60).to_i &lt;br /&gt;      mins = (mins % 60).to_i&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  [hours,mins,seconds]&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;departed_house = Time.parse("07:34")&lt;br /&gt;arrived_at_supermarket = Time.parse("09:10")&lt;br /&gt;travel_duration_in_seconds =  arrived_at_supermarket - departed_house&lt;br /&gt;&lt;br /&gt;hours, minutes, seconds = seconds_fraction_to_time(travel_duration_in_seconds)&lt;br /&gt;&lt;br /&gt;puts "The journey from my home to the supermarket took #{hours} hour(s), #{minutes} minutes, and #{seconds} seconds."&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Output:&lt;br /&gt;The journey from my home to the supermarket took 1 hour(s), 36 minutes, and 0 seconds.&lt;br /&gt;&lt;br /&gt;References:&lt;br /&gt;-  &lt;a href="http://www.ruby-doc.org/core/classes/Time.html"&gt;Class: Time&lt;/a&gt; [ruby-doc.org]&lt;br /&gt; - &lt;a href="http://www.techotopia.com/index.php/Working_with_Dates_and_Times_in_Ruby"&gt;Working with Dates and Times in Ruby&lt;/a&gt; [techotopia.com]&lt;br /&gt; - &lt;a href="http://rubylearning.com/satishtalim/numbers_in_ruby.html"&gt;Numbers in Ruby&lt;/a&gt; [rubylearning.com]</description>
      <pubDate>Wed, 20 Aug 2008 21:48:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5958</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Making time for Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5956</link>
      <description>&lt;code&gt;b = Time.parse("17:30")&lt;/code&gt;&lt;br /&gt;=&gt; Wed Aug 20 17:30:00 +0100 2008&lt;br /&gt;&lt;br /&gt;Reference:&lt;br /&gt;  &lt;a href="http://www.ruby-doc.org/core/classes/Time.html"&gt;Class: Time&lt;/a&gt; [ruby-doc.org]</description>
      <pubDate>Wed, 20 Aug 2008 17:49:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5956</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>A user friendly elapsed time with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5715</link>
      <description>This Ruby code outputs a user friendly elapsed time or a historical date if the event was more than 24 hours ago. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;def didwhen(old_time)&lt;br /&gt;&lt;br /&gt;  val = Time.now - old_time&lt;br /&gt;  #puts val&lt;br /&gt;  if val &lt; 10 then&lt;br /&gt;    result = 'just a moment ago'&lt;br /&gt;  elsif val &lt; 40  then&lt;br /&gt;    result = 'less than ' + (val * 1.5).to_i.to_s.slice(0,1) + '0 seconds ago'&lt;br /&gt;  elsif val &lt; 60 then&lt;br /&gt;    result = 'less than a minute ago'&lt;br /&gt;  elsif val &lt; 60 * 1.3  then&lt;br /&gt;    result = "1 minute ago"&lt;br /&gt;  elsif val &lt; 60 * 50  then&lt;br /&gt;    result = "#{(val / 60).to_i} minutes ago"&lt;br /&gt;  elsif val &lt; 60  * 60  * 1.4 then&lt;br /&gt;    result = 'about 1 hour ago'&lt;br /&gt;  elsif val &lt; 60  * 60 * (24 / 1.02) then&lt;br /&gt;    result = "about #{(val / 60 / 60 * 1.02).to_i} hours ago"&lt;br /&gt;  else&lt;br /&gt;    result = old_time.strftime("%H:%M %p %B %d, %Y")&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;  result&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Inspired by Twitter.</description>
      <pubDate>Mon, 30 Jun 2008 09:37:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5715</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>TimeLine //JavaScript Class</title>
      <link>http://snippets.dzone.com/posts/show/5293</link>
      <description>&lt;br /&gt;&lt;br /&gt;&lt;a href="http://jsfromhell.com/classes/timeline"&gt;&lt;br /&gt;Simulates the Adobe Flash timeline. You define the amount of frames, the speed in fps (frames per second) and, at each frame passage an event is called, useful for animations.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/classes/timeline [v1.0]&lt;br /&gt;&lt;br /&gt;TimeLine = function(fps, f){&lt;br /&gt;	this.fps = fps, this.frames = f;&lt;br /&gt;};&lt;br /&gt;with({o: TimeLine, $: TimeLine.prototype}){&lt;br /&gt;	o.timers = [];&lt;br /&gt;	$.running = !!($.current = +(o.timer = $.time = null));&lt;br /&gt;	o.run = function(){&lt;br /&gt;		var o = this;&lt;br /&gt;		o.timer || (o.timer = setInterval(function(){&lt;br /&gt;			for(var h, d = +(new Date), t = o.timers, i = t.length; i--;){&lt;br /&gt;				(!t[i].running || ((d - t[i].time) / (1e3 / t[i].fps) &gt; t[i].current + 1 &amp;&amp;&lt;br /&gt;				t[i].onframe(++t[i].current), t[i].current &gt;= t[i].frames)) &amp;&amp;&lt;br /&gt;				(h = t.splice(i, 1)[0], h.stop(1));&lt;br /&gt;			}&lt;br /&gt;		}, 1));&lt;br /&gt;	};&lt;br /&gt;	$.start = function(c){&lt;br /&gt;		var o = this, t = TimeLine;&lt;br /&gt;		if(o.running) return;&lt;br /&gt;		o.running = true, o.current = c || 0;&lt;br /&gt;		o.time = new Date, o.onstart &amp;&amp; o.onstart();&lt;br /&gt;		if(!o.onframe || o.frames &lt;= 0 || o.fps &lt;= 0)&lt;br /&gt;			return o.stop(1);&lt;br /&gt;		t.timers.push(this), t.run();&lt;br /&gt;	};&lt;br /&gt;	$.stop = function(){&lt;br /&gt;		var o = this;&lt;br /&gt;		o.running = false;&lt;br /&gt;		if(!TimeLine.timers.length)&lt;br /&gt;			TimeLine.timer = clearInterval(TimeLine.timer), null;&lt;br /&gt;		arguments.length &amp;&amp; o.onstop &amp;&amp; o.onstop();&lt;br /&gt;	};&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;&lt;div id="box" style="position: absolute; top: 100px; background: #efe; width: 100px; height: 100px"&gt;25 fps&lt;/div&gt;&lt;br /&gt;&lt;div id="box2" style="position: absolute; top: 300px; background: #ff9; width: 100px; height: 100px"&gt;12 fps&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;TimeLine working together with the ease in quad function.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;Math.ease = function (t, b, c, d) {&lt;br /&gt;	if ((t /= d / 2) &lt; 1)&lt;br /&gt;		return c / 2 * t * t + b;&lt;br /&gt;	return -c / 2 * (--t * (t - 2) - 1) + b;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;var o = new TimeLine(25, 50), d = document, b = d.getElementById("box");&lt;br /&gt;o.onframe = function(){&lt;br /&gt;	b.style.left = Math.ease(this.current, 0, 400, 30) + "px";&lt;br /&gt;};&lt;br /&gt;o.onstart = function(){&lt;br /&gt;	d.body.appendChild(d.createTextNode("Started"));&lt;br /&gt;};&lt;br /&gt;o.onstop = function(){&lt;br /&gt;	d.body.appendChild(d.createTextNode(" - Finished (" + (((new Date) - this.time)) + " msec)"))&lt;br /&gt;	d.body.appendChild(d.createElement("br"));&lt;br /&gt;	this.start();&lt;br /&gt;};&lt;br /&gt;o.start();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;var o2 = new TimeLine(12, 50), b2 = d.getElementById("box2");&lt;br /&gt;o2.onframe = function(){&lt;br /&gt;	b2.style.left = Math.ease(this.current, 0, 400, 30) + "px";&lt;br /&gt;};&lt;br /&gt;o2.onstop = function(){&lt;br /&gt;	this.start();&lt;br /&gt;};&lt;br /&gt;o2.start();&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 30 Mar 2008 16:49:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5293</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>Measuring the elapsed time</title>
      <link>http://snippets.dzone.com/posts/show/5229</link>
      <description>This Ruby code measures how long it takes to display the message "hello world".&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def test_method(statement)&lt;br /&gt;  start_time = Time.now&lt;br /&gt;  eval(statement)&lt;br /&gt;  end_time = Time.now&lt;br /&gt;  end_time - start_time&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;statement = "sleep 1.5; puts 'hello world'"&lt;br /&gt;elapsed_time = test_method(statement)&lt;br /&gt;puts "elapsed time : #{elapsed_time}"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output&lt;br /&gt;&lt;code&gt;&lt;br /&gt;hello world&lt;br /&gt;elapsed time : 1.499266&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 14 Mar 2008 08:47:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5229</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Ruby Servlets</title>
      <link>http://snippets.dzone.com/posts/show/5208</link>
      <description>This WEBrick example demonstrates a basic servlet which displays the current time. Source code copied from &lt;a href="http://www.linuxjournal.com/article/8356"&gt;At the Forge - Getting Started with Ruby&lt;/a&gt; [linuxjournal.com].&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/sw/bin/ruby&lt;br /&gt;require 'webrick'&lt;br /&gt;include WEBrick&lt;br /&gt;&lt;br /&gt;# ---------------------------------------------&lt;br /&gt;# Define a new class&lt;br /&gt;class CurrentTimeServlet&lt;br /&gt;  &lt; WEBrick::HTTPServlet::AbstractServlet&lt;br /&gt;&lt;br /&gt;  def do_GET(request, response)&lt;br /&gt;    response['Content-Type'] = 'text/plain'&lt;br /&gt;    response.status = 200&lt;br /&gt;    response.body = Time.now.to_s + "\n"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# ----------------------------------------------&lt;br /&gt;# Create an HTTP server&lt;br /&gt;s = HTTPServer.new(&lt;br /&gt;  :Port            =&gt; 8000,&lt;br /&gt;  :DocumentRoot    =&gt; "/usr/local/apache/htdocs/"&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;s.mount("/time", CurrentTimeServlet)&lt;br /&gt;&lt;br /&gt;# When the server gets a control-C, kill it&lt;br /&gt;trap("INT"){ s.shutdown }&lt;br /&gt;&lt;br /&gt;# Start the server&lt;br /&gt;s.start&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;e.g. http://localhost:8001/time&lt;br /&gt;output&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Mon Mar 10 23:06:58 +0000 2008&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Reference: http://www.webrick.org/</description>
      <pubDate>Mon, 10 Mar 2008 23:17:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5208</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Creating a DateTime object with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5190</link>
      <description>This example creates a date and time variable which represents 22nd March 2008 4:30pm and 12 seconds.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;d2 = DateTime.new(y=200,m=3,d=22, h=16,min=30,s=12)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;or convert a date string into a DataTime object:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;"17/03/2009 17:48:00"[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]&lt;br /&gt;d2 = DateTime.new(y=$3.to_i,m=$2.to_i,d=$1.to_i, h=$4.to_i,min=$5.to_i,s=$6.to_i)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;or convert a date string into a Time object:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;"22/03/2008 17:48:00"[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]&lt;br /&gt;iyear = $3.to_i; imonth = $2.to_i; iday = $1.to_i; ihour = $4.to_i; imin = $5.to_i; isec = $6.to_i&lt;br /&gt;twork = Time.local(iyear,imonth,iday,ihour,imin,isec) &lt;br /&gt;puts 'we still have time to party' if Time.now &lt; twork &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Reference:&lt;br /&gt;  &lt;a href="http://www.ruby-doc.org/core/classes/DateTime.html"&gt;Class: DateTime&lt;/a&gt; [ruby-doc.org]&lt;br /&gt;  &lt;a href="http://www.ruby-doc.org/core/classes/Time.html"&gt;Class: Time&lt;/a&gt; [ruby-doc.org]</description>
      <pubDate>Sat, 01 Mar 2008 21:53:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5190</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>before? and after? - Ruby Time class mixin</title>
      <link>http://snippets.dzone.com/posts/show/5022</link>
      <description>The mixin below allows comparison between two Time objects using the more intuitive and natural-sounding before? and after? methods.&lt;br /&gt;&lt;br /&gt;Some examples (using Rails' ActiveSupport Time extensions or (preferred) the 'units' gem):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;2.minutes.ago.after? Time.now # =&gt; false&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;Time.now.before? 2.hours.from_now # =&gt; true&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;module BeforeAndAfter&lt;br /&gt;&lt;br /&gt;  LEFT_SIDE_LATER  = 1&lt;br /&gt;  RIGHT_SIDE_LATER = -1&lt;br /&gt;  &lt;br /&gt;  def before?(input_time)&lt;br /&gt;    (self &lt;=&gt; input_time) == RIGHT_SIDE_LATER&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def after?(input_time)&lt;br /&gt;    (self &lt;=&gt; input_time) == LEFT_SIDE_LATER&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;Time.send :include , BeforeAndAfter&lt;/code&gt;</description>
      <pubDate>Mon, 21 Jan 2008 21:57:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5022</guid>
      <author>JimCropcho (Jim Cropcho)</author>
    </item>
  </channel>
</rss>
