RSSAll Entries Tagged With: "time"

Example of Simple Retry Technique to Deal with Timeouts in Android

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class TestHttpGet {
public String executeHttpGetWithRetry() throws Exception {
int retry = 3;
int count = 0;
while (count < retry) {
[...]

Create a countdown timer that counts back in Javascript

Div to show the countdown timer.

<div id="showRemain"></div>

Javascript code:

<script type="text/javascript">
function countDown(){
var timeA = new Date();
var timeB = new Date("10/31/2000 03:04:05");
var timeDifference = timeB-timeA;
if(timeDifference>=0){
timeDifference=timeDifference/1000;
timeDifference=Math.floor(timeDifference);
var wan=Math.floor(timeDifference/86400);
var l_wan=timeDifference%86400;
var hour=Math.floor(l_wan/3600);
var l_hour=l_wan%3600;
var minute=Math.floor(l_hour/60);
var second=l_hour%60;
var showPart=document.getElementById(’showRemain’);
showPart.innerHTML=wan+" hour "+hour+" minute " +minute+" Second "+second+"";
if(wan==0 && hour==0 && minute==0 && second==0){
clearInterval(iCountDown);
}
}
}
 
var iCountDown=setInterval("countDown()",1000);
</script>

Removing event listeners in Actionscript

var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
function onTimer(evt:TimerEvent):void {
watch.hand.rotation +=5;
if (watch.hand.rotation >= 25) {
timer.removeEventListener(TimerEvent.
TIMER, onTimer);
}
}

Actionscript timer event

var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
function onTimer(evt:TimerEvent):void {
watch.hand.rotation +=5;
}

Using timers in c#

A simple program illustrating the usage of timers in c#

using System;
using System.Timers;
 
class TestTimer
{
public static void Main ()
{
Timer timer = new Timer();
timer.Elapsed + = new ElapsedEventHandler(DisplayTimeEvent);
[...]

Find difference in dates in mysql

We can use the TIMEDIFF and TIME_TO_SEC functions to find the time difference in seconds.

SELECT TIME_TO_SEC(TIMEDIFF(’2010-01-09 10:24:46′,’2010-01-09 10:23:46′));

Timing program execution in PHP using PEAR

We can use the PEAR Benchmark module to profile a program, to see how long each statement takes to execute.

<?php
require_once ‘Benchmark/Timer.php’;
$timer =& new Benchmark_Timer(true);
$timer->start();
 
// some program code here
$timer->setMarker(’one’);
 
// some more program executed here
$timer->setMarker(’two’);
 
// even yet still more program code here
$timer->setmarker(’three’);
// and a last bit of code here
 
$timer->stop();
$timer->display();
?>

Determine how much time a function takes to execute in PHP

To determine how much time a single function takes to execute, we can compare time in milliseconds before running the function against the time in milliseconds after running the function to see the elapsed time spent in the function.

<?php
$long_str = "this is a test to see how much time md5 function takes to execute over [...]

PHP Find time ago between two times

/**
* Formats a timestamp nicely with an adaptive "x units of time ago" message.
* Only handles past dates.
* @return string Nicely-formatted message for the timestamp.
* @param $time Output of strtotime() on your choice of timestamp.
*/
function ago($time) {
$time = strtotime($time);
$delta = time() – $time;
if ($delta < 60) {
[...]

Get file size and create time of file in PHP

All file have certain properties that can be displayed using PHP. These properties include the last access time, the last modified time, the last changed time, and the size of the file.
Example:

<HTML>
<HEAD>
<TITLE>Returning information about a file</TITLE>
</HEAD>
<BODY>
<?
print "The size of the file is ";
print filesize( "samplefile.doc" );
print "<br>";
$atime = fileatime( "samplefile.doc" );
print "This file accessed on [...]