Small JavaScript’s trick about Date object.

June 18, 2009 – 4:08 pm

Today I’ve found one more trick about JavaScript and I think it will be interesting for someone else.

So, how do you usually get timestamp value with JavaScript?
I used the following code:
<script type=”text/javascript”>
var timestamp = Math.round(new Date().getTime()/1000);
</script>

As you might know Firefox has its own function to get timestamp named now, so you can do static call like this:
<script type=”text/javascript”>
var timestamp = Math.round(Date.now()/1000);
</script>

In this case you aren’t creating a new Date object. That is a good point.

But, what can we do for the rest browsers? Of course we can use known construction (see above). However there is one more possible constraction:
<script type=”text/javascript”>
var timestamp = Math.round(+new Date/1000);
</script>

As we see this construction is shorter. How does it work?
When script calls +new Date the script creates new Date object and makes a call to valueOf method of Date object.
In turn the valueOf method returns the number of milliseconds since midnight 01 January, 1970 UTC, so it is an equivalent to the getTime method.

I’ve check it with performance and they work identical.

Here is updated variant:
<script type=”text/javascript”>
function timestamp()
{
return Math.round(
(Date.now && Date.now() || +new Date)/1000
);
}
</script>

Enjoy it!

PHP TestFest 2009

June 11, 2009 – 11:14 pm

You might know that PHP TestFest 2009 is still continuing.

I want to participate in this event as it is one of my favorite programming languages. I tried to find user group from Ukraine but I didn’t. It looks like nobody from Ukraine wants to improve it and its a pity.

elePHPants

Actually you don’t need to know C++ and it is enough to have your PHP knowledges. Of course you have to be an expert in PHP.

If you are interested you can start reading this page which explains how to write tests.

As a result if your test cases will be approved by mentor your tests will be committed to PHP CVS and you might get official PHP.net accounts with direct commit access to cvs.php.net and an @php.net email address. Yep! That’s not all - you will get elePHPant as a small present :)

Please contact me if you are interested and we can make a UA user group!

Where do we go?

June 8, 2009 – 11:05 am

Today I’ve seen a new movie called HOME.

I am so affected by this movie and all people have to watch it. It explains in which situation the people live now and how we’ve been killing ourself for over 50 years.

Yes, for the last 50 years we’ve changed the Earth and we are about to destroy the nature, animals, everything that has been built for thousands years.

Here is a home page of that movie and here is a russian version.

This movie is going to be online till 14 June, so don’t postpone and watch it right now.

How to build patch for Pidgin by yourself

July 3, 2008 – 8:52 pm

As you may know the company AOL has raised the protocol version from ICQ5 to ICQ6 and this caused connection problems for some ICQ clients. In this article I am explaining how to patch your pidgin by yourself.

  • First of all we need to install the following packages: build-essential, fakeroot and devscripts
 sudo apt-get install devscripts build-essential fakeroot
  • Download the sources of libpurple0 because we need to patch this library
 apt-get source libpurple0
  • Download the patch and get patched the library
 wget http://launchpadlibrarian.net/15741199/pidgin-2.4.2-icq.patch
 tar xzf pidgin_2.4.1.orig.tar.gz
 cd pidgin-2.4.1/
 patch -p0 < ../pidgin-2.4.2-icq.patch
  • Set the dependences
 sudo apt-get build-dep libpurple0
  • Rebuild the package
 debuild -us -uc
  • Install patched package
 cd ..
sudo dpkg -i libpurple0_2.4.1-1ubuntu2_i386.deb

You can get an amd version instead of i386 depending of your platform

  • Reload your pidgin and be happy

php/simplexml and namespace in rdf

April 4, 2007 – 12:07 pm

Using simplexml ext to parse rdf file.

The problem:
For example we have next rdf file:

<?xml version=“1.0″ encoding=“utf-8″?>
<item rdf:about=“http://someurl”>
<title>my title </title>
<rdf:RDF>
<description>my desc </description>
<dc:date>March, 10 </dc:date>
</item>
</rdf:RDF>


some.php :

<?php
$rdf = simplexml_load_file($path2Rdf);
foreach ($rdf->item as $item)
echo $item->date;
?>


But this code doesn’t work, because element “date” has a namespace prefix of “dc”, so the script doesn’t have access to this namespace.

Solution:

Firstly, we should define the namespace in rdf file:

<rdf:RDF xmlns:rdf=”http://www.w3.org/1999/02/22-rdf-syntax-ns#”

xmlns=”http://purl.org/rss/1.0/” xmlns:dc=”http://purl.org/dc/elements/1.1/”>


and secondly, we should use “children” method to get a collection of elements which belong to “dc” namespace:
<?php
$dc = $item->children(‘http://purl.org/dc/elements/1.1/’);
echo $dc->date;
?>

Professional programmer

March 29, 2007 – 10:43 am

I found a good article about programmer levels. This article shows the points what a professional programmer should have.

Project management

March 22, 2007 – 2:49 am

The other day i have read a fascinating novel about project management. I suppose any manager and developer should read this book independently of specialization and experience.
This book tells about people and their relations, phases of development and ordering, rules of staff recruitment, problems during developing and methods to solve them and a lot of other helpful advices.

php interface

March 21, 2007 – 11:26 pm

Today i was writing next code:

interface iTest 
{
    function testMe($x); 
}

class Test implements iTest  
{
    public function testMe($x, $y=”World”)
    {
        print $x . $y;
    }
}

$test = new Test();
$test->testMe(”Hello “);

The declaration of method in class differs from interface.
I was surprised at this situation because php doesn’t generate any errors.