UTC time in Android device. With NTP server sync.

I had a requirement to persist the current UTC time of a request in Android device for future reference.

Getting the time from the Android device and converting it to UTC will not be efficient since, user might have set wrong time in the device and it might mislead the data.
So we decided to sync the device with NTP server before converting the time to UTC.

Step 1 : Copy this SntpClient.java into your source.
Step 2 : The SntpService.java to compute the current UTC is here below.

public String getUTCTime(){
        long nowAsPerDeviceTimeZone = 0;
        SntpClient sntpClient = new SntpClient();

        if (sntpClient.requestTime("0.africa.pool.ntp.org", 30000)) {
            nowAsPerDeviceTimeZone = sntpClient.getNtpTime();
            Calendar cal = Calendar.getInstance();
            TimeZone timeZoneInDevice = cal.getTimeZone();
            int differentialOfTimeZones = timeZoneInDevice.getOffset(System.currentTimeMillis());
            nowAsPerDeviceTimeZone -= differentialOfTimeZones;
        return DateUtils.getFormattedDateTime(new Date(nowAsPerDeviceTimeZone));

Some more details on SntpService code:

Connect to any of the prominent ntp servers. There were lots of recommendation to place this in config file, however i thought it doesn’t make sense for Android since i have to repackage this anyways.


gives you the current NTP time as per the device time zone.

Then identify the device’s time zone,


and calculate the offset difference between UTC and the current device time.


is our custom method to format date into String.

What’s new in Apple Passbook iOS7

I’m currently working in a project that integrated with iOS Passbook application to deliver digital passes to their users. Spent some time in investigating the changes to passbook application in iOS7 (beta 4). Here is a quick summary of it.

Apple listened to its user’s feedback and have come with some features which increases the usability of the application.

Add multiple passes to Passbook.

Yes, you can able to add multiple passes to the Passbook application in one go. So, if you are issuing multiple passes to your users, probably have a page with all the passes and you can have download all link, which downloads all the passes in one shot.

Delivery through Barcode.

Apple have added one more delivery mechanism to the passbook. Currently you can deliver a pass through Safari browser (with vnd.apple.pkpass as header) or Through email attachment or to stream from your native iOS application. Now in the new passbook app they have added a barcode scanner. So the content of the pass can be crisped into a barcode and can be delivered to the Passbook application.

Anchor tags at the back of the pass.

Currently you can have links, but you cannot have link text. For example if you want to link http://www.google.com to ‘Click here’ it is not possible. But it will be possible from iOS7

Expiration date for Passes.

Now passes can have its own expiration date. It’s a meta data that you can set and after that the passes will be destroyed from the Apple passbook automatically.

Usage restriction by Geo location.

You can restrict the usage of passes, within a specific geo location. For example if you are issuing a coupon, then you can make sure that your users could able to access the coupons within a specific geo co-ordinates.

All these are nice features that are provided in iOS7. So far i was happy with all these news, until i read that, (Dev forum link)

The rendering algorithms are significantly different from the previous versions. 

This seems to be a major issue to me, since i have to now test the appearance of my pass with new version. And have to think about optimising the design across all the versions.

Setting up Cucumber-jvm

Cucumber JVM is Java implementation of Cucumber BDD.

Integrating into the Project

The installation using maven is super simple, just add the dependency and you are ready to go. Make sure you add both command line interface (cucumber-core) and the IDE interface (cucumber-junit)

I was using Intellij and add Intellij Cucumber plugin, to make the navigations easier.

One thing i liked very much is the ability to add custom annotations to the feature. You can add a custom annotation and can create Before and After hook for them.

In .feature file


In the step definitions file.


Integrating with Spring

For Spring integration you need to add one more component of the cucumber-jvm (cucumber-spring)

It is advisable to have a test runner class which can run all the feature files in one go especially when you are runnning in the CI.

The structure of the test runner class will be :

public class CucumberAdapterTest {

Make sure to place all the feature files in the same package as of this Runner class. Or you can specify the path using the cucumber options, like this.

@Cucumber.Options(features = "classpath:**/*.feature")
public class CucumberAdapterTest {

If you are placing all the step definition in other package you can add that to the annotation using glue attribute.

@Cucumber.Options(features = "classpath:**/*", glue = {"path of the step definitions"})
public class CucumberAdapterTest {

This will look up for cucumber.xml file in the classpath. This xml file can hold all the bean definitions. My cucumber.xml was super simple.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <import resource="classpath*:/application-context.xml"/>

    <context:component-scan base-package="path of the step definition"/>

The step defnitions can lie in a different package and make sure you use glue attribute to wire them in the Runner class.

public class StepDefinitions {

EntityRepository entityRepository;

@Given("^Register a user$")
public void registerUser() throws Throwable {


Integrating with Spring Transactions

One last thing that i wanted to do is to hook up Spring transactions. So all the data created by the features have to be removed after the test completes. So you can write independent tests without bothering about the data.

You can use ‘txn‘ annotation that comes with Cucumber-JVM. All you need to do is to wire up that package along with your adapter class.

@Cucumber.Options(glue = {"cucumber.api.spring"})
public class CucumberAdapterTest {


Scenario: Some scenario to test


Link your Sublime Text 2 instances with Dropbox

After started using Sublime Text 2 (ST2), was completely head over heels for it. After using it for some time, figured out that ST2 uses a settings file to remember the plugins, open tabs etc. I was using my laptop (Mac) and sometimes uses my PC (Windows) do type blog post or some other stuff. So thought of giving a try in syncing these two using Dropbox folder.

The idea is to have the settings file in the Dropbox folder and to have a symlink in the OS to point to. This means whatever changes i did to a ST2 instance will reflect in my other instance too.


  • Install ST2 in both the machines.
  • Have a DropBox account and install the software in both the machines.

In Mac:

Move the entire Sublime Text folder into the Dropbox folder.

mv '~/Library/Application\ Support/Sublime\ Text\ 2/' '~/Dropbox/Sublime\ Text\ 2'

Next step is to create a symlink in the original location so that it the folder in the Dropbox will be used to store the settings.

ln -s '~/Library/Application\ Support/Sublime\ Text\ 2' '~/Dropbox/Sublime\ Text\ 2'

In Windows:

Windows don’t have symlink concept. So we have to settle with NTFS symbolic link

mklink /J 'C:/Users/user_name/Dropbox/Sublime Text 2' 'C:/User/user_name/Applications/Sublime Text 2'

That’s it, now you don’t need to worry about the sync between these two instances.

Scribblings on Socket.io

I was trying my hands on socket.io. On my first glance it looked extremely simple to get going. The app i was working was on node.js, so i had no trouble in including socket.io into my project.

My app had client and server component. For the server component i could able to do the npm install and got the socket.io working. Whereas for the client component i couldn’t able to find the stand alone js available for download. Basically the js comes in along with the npm which means you got to take it out separately if you want to use it. Then i used the js file from their Github repo.

By default Socket.io doesn’t perform broadcast

This is my first learning. Though it seems to be obvious after taking a good look onto the API, it wasn’t very clear for me in the beginning.

For example:


var app = require('express')(),
	server = require('http').createServer(app),
	io = require('socket.io').listen(server);


io.sockets.on('connection', function(socket) {
	socket.on('first_msg', function(data) {
		socket.emit('reply', {
			hello: 'world'

Client 1:

< script src = "/socket.io/socket.io.js" > < /script>
	var socket = io.connect('http:/ / localhost ');
	socket.emit('first_msg ', { my: 'data1 ' });

Client 2:

< script src = "/socket.io/socket.io.js" > < /script>
	var socket = io.connect('http:/ / localhost ');
	socket.on('reply ', function (data){
		console.log("Client1 had pinged server.");

In this case i was expecting my Client2 console.log to execute but that never happened. Reason being whenever Client1 emits ‘first_msg‘ it was Client1 who was receiving the reply too (obvious i know !!).

So in these cases socket.io provides an API to broadcast messages.Hence instead of

socket.emit('reply', { hello: 'world' });

it should have been

socket.broadcast.emit('reply', { hello: 'world' });

Exposed events in socketIO are just defined for socket.on methods

I was trying to emit a custom message from my client. I need to perform some actions on its success and failure. Now i need to attach success and error callbacks. For this i found this Exposed events doc. The funda is that all these exposed events are defined only for socket.on which means while emitting a message i cannot bind any callbacks to it.

For error callback it is straight forward. We have

socket.on('error', () -> console.log("Error Occured"))

which can be bound on the socket so whenever an error is been thrown on the socket the defined behaviour gets executed.

Client emits the custom message and sends JSON data to the socket via socket.emit, also he gets an update function that handles the success callback

socket.emit ('message', {hello: 'world'});
socket.on ('messageSuccess', function (data) {
   //do something

Server-side Gets a call from the message emit from the client and emits the messageSuccess back to the client

socket.on ('message', function (data) {
	io.sockets.emit ('messageSuccess', data);