Never been to DZone Snippets 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

« Newer Snippets
Older Snippets »
Showing 31-40 of 74 total

Easter date - C

Easter_c computes the easter date for year Y. It uses the standard (as used by most western churches) algorithm developed by the Napolitan astronomer Aloysius Lilius and the Jesuit mathematician Christopher Clavius. It works for any year after 1582.

typedef struct {
  unsigned char d;
  unsigned char m;
} Date;

void easter_c(unsigned short Y, Date *d) {
  int G = (Y % 19) + 1;
  int C = (int)(Y / 100) + 1;
  int X = 3 * C / 4 - 12;
  int Z = (8 * C + 5) / 25 - 5;
  int D = 5 * Y / 4 - X - 10;
  int E = (11 * G + 20 + Z - X) % 30;
  if (((E == 25) && (G > 11)) || (E == 24))
    ++E;
  int N = 44 - E;
  if (N < 21)
    N += 30;
  N = N + 7 - ((D + N) % 7);

  if (N > 31) {
    d->d = N - 31;
    d->m = 4;
  } else {
    d->d = N;
    d->m = 3;
  }
}

Try'n'Go: The Last Date

Extends Date class with a method that return the last Date of a month.
Date#last_of_month takes either a Fixnum or a Time as argument.

require 'date'

class Date
  def self.last_of_month( arg = Time.now )
    year = ( arg.is_a? Fixnum ) ? Time.now.year : arg.year
    mon  = ( arg.is_a? Fixnum ) ? arg : ( arg.mon rescue Time.now.mon )
    
    raise ArgumentError unless mon.between?( 1, 12 )

    begin; Date.new year, mon, mday ||= 31
    rescue ArgumentError; mday -= 1; retry
    end
  end
end

Get the Unix Epoch time in one line of C#

int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;

Install DateBocks

Install engines plugin

./script/plugin source http://svn.rails-engines.org/plugins/
./script/plugin install engines


Install datebocks

./script/plugin source http://svn.toolbocks.com/plugins
./script/plugin install datebocks_engine

Formatting a date with or without the time

Ruby on Rails

This helper returns the date (and possibly the time) formatted the way you want

def format_date(date, use_time = false)
    if use_time == true
        ampm = date.strftime("%p").downcase
        new_date = date.strftime("%B %d, %Y at %I:%M" + ampm)
    else
        new_date = date.strftime("%B %d, %Y")
    end
end

days-up-to-month - returns the number of days in a year, preceding the given month.

days-in-months: [31 28 31 30 31 30 31 31 30 31 30 31]
days-in-months-leap: head change at copy days-in-months 2 29

days-up-to-month: func [month /in year] [
    sum copy/part either leap-year?/with any [year now] [days-in-months-leap] [days-in-months] month - 1
]

PHP echo date

<?php echo date('d/m/Y',strtotime($row_name['column'])); ?>

time since

// returns the time since $original (unix date)

function time_since($original) {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
    );
    
    $today = time(); /* Current unix time  */
    $since = $today - $original;
	
	if($since > 604800) {
		$print = date("M jS", $original);
	
		if($since > 31536000) {
				$print .= ", " . date("Y", $original);
			}

		return $print;

	}
    
    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        
        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0) {
            // DEBUG print "<!-- It's $name -->\n";
            break;
        }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";

    return $print . " ago";

}

fix safari's Date#setMonth

fix safari's Date#setMonth
(function(){
	var set_month = Date.prototype.setMonth;
	Date.prototype.setMonth = function(num){
		if(num <= -1){
			var n = Math.ceil(-num);
			var back_year = Math.ceil(n/12);
			var month = (n % 12) ? 12 - n % 12 : 0 ;
			this.setFullYear(this.getFullYear() - back_year);
			return set_month.call(this, month);
		} else {
			return set_month.apply(this, arguments);
		}
	}
})();

Gmail Date Format Helper

I needed a short and intuitive way of showing dates, so rather than just making something up I decided to steal Google's short date format from Gmail. I'm sure they did usability studies and whatnot.

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
  :gmail => lambda { |date|
    Time.now.beginning_of_day <= date ?
    "#{date.strftime('%I').to_i}:#{date.strftime('%M')} #{date.strftime('%p').downcase}" :
    Time.now.beginning_of_year <= date ?
    "#{date.strftime('%b')} #{date.day}" :
    "#{date.month}/#{date.day}/#{date.strftime('%y')}"
  }
)

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  :gmail => lambda { |date|
    Time.now.beginning_of_day <= date ?
    "#{date.strftime('%I').to_i}:#{date.strftime('%M')} #{date.strftime('%p').downcase}" :
    Time.now.beginning_of_year <= date ?
    "#{date.strftime('%b')} #{date.day}" :
    "#{date.month}/#{date.day}/#{date.strftime('%y')}"
  }
)


Put this code in your "environmen.rb" file in your "RAILS_ROOT/config" directory or make a new Ruby script file containing it in your "RAILS_ROOT/config/initializers" directory.
« Newer Snippets
Older Snippets »
Showing 31-40 of 74 total