Type talk:Time

From semanticweb.org

Jump to:navigation, search

[edit] Code Example

The following is an code-excerpt how the unit conversion might look like:

function SMWConvertTime($value, $unit) {
	$result=Array();
	switch ( $unit ) {
		case '': case 's': case 'second': case 'seconds': // this is also the default unit
			$mainval=$value; 
			$result['UNIT']='s'; // this is the preffered unit during output
			break;
		case 'min': case 'minute': case 'minutes': 
			$mainval=$value*60; // convert to the default unit second
			$result['UNIT']='min'; 
			break;
		case 'h': case 'hour': case 'hours':
			$mainval=$value*3600; 
			$result['UNIT']='h'; 
			break;
		case 'd': case 'day': case 'days':
			$mainval=$value*86400; 
			$result['UNIT']='d'; 
			break;
		case 'week': case 'weeks':
			$mainval=$value*86400; 
			$result['UNIT']='d'; 
			break;
		case 'fortnight': case 'fortnights':
		    $mainval=$value*1209600;
		    $result['UNIT']='d'; 
			break;
		case 'month': case 'months':
		    $mainval=$value*2629746;
		    $result['UNIT']='d'; 
			break;
		case 'quarter': case 'quarters'
		    $mainval=$value*7889238;
		    $result['UNIT']='d'; 
			break;
		case 'a': case 'year': case 'years':
		    $mainval=$value*31556952;
		    $result['UNIT']='a'; 
			break;
		case 'decade': case 'decades':
		    $mainval=$value*315569520;
		    $result['UNIT']='a'; 
			break;
		case 'century': case 'centuries':
		    $mainval=$value*3155695200;
		    $result['UNIT']='a'; 
			break;
		case 'millennium': case 'millenniums':
		    $mainval=$value*31556952000;
		    $result['UNIT']='a'; 
			break;
			
		default: //unsupported unit
			$result['ERROR']='Warning: Unit "'.$unit.'" is not supported for this attribute.';
			return $result;
	}
	
	// Output: print custom unit conversions
	$result['VALUES']=Array();
	$result['VALUES']['s']=$mainval;
	$result['VALUES']['min']=$mainval/60;
	$result['VALUES']['h']=$mainval/3600;
	$result['VALUES']['d']=$mainval/86400;
	$result['VALUES']['a']=$mainval/31556952;

	return $result;
}

Note, that I still can't PHP. This code is just a variation of the lenght-code. I'm also not fully happy about the way it works, because there can't be the number after the unit, and the unit can't calculated from a set of other units like from "m/s*s2"
MovGP0 19:24, 21 November 2005 (CET)

That's about right, thanks for the conversion factors. I'll implement in SMW_DT_Float.php. I think 'UNIT' just normalizes all the ways of representing a particular inbound unit, so I'm not sure what you mean by 'a' for decades, centuries, and millenia. -- Skierpage 08:37, 25 April 2006 (CEST)


[edit] Dimension reference

FYI, the most complete implementation of unit conversions that I've found is in the Perl Data-Dimensions module, but that doesn't have fortnights! -- Skierpage 09:09, 25 April 2006 (CEST)

Navigation
services
Toolbox