Time is an illusion but TimeZones are not.

Recently I have been working on handling timezones on a client project. Front end is AngularJs 1.3 and backend api is Rails api.

It is a very common scenario. User chooses what timezone the data should be displayed in. Api saves and returns all the dates in UTC format.

There are also a lot many client side validations comparing saved time with local time (in all permutation and combinations possible).

Tools used:

  1. Javascript Date object:
    new Date();

    While working with timezones, it is always good to know that Javascript Date object is always in local time zone. There is no direct option to set the timezone. You can change time by adding/subtracting timezone offset.

  2. moment.js: moment is a javascript library which provides pretty options to format the moment date object which is not possible with javascript Date object.
  3. moment-timezone.js: This library allows you to convert moment time object in different timezones.
    moment.tz("2015-12-23 20:30:00", "US/Central")

 

My approach:

  • Keep all the times in local timezone, whatever that may be .. the browser figures that out and by default all the times are in local timezone.
  • All the comparison of different time objects should be done when all the objects are in same time zone and preferably in local time zone.
  • When submitting the form, change the time zone of the time objects from local timezone to desired timezone. Will explain in more detail later.
  • When doing a get request of the resource(i.e the edit page of lets say business profile), change all the time objects to time in local timezone.

Reasons for the approach:

There are 2 main things that I am doing. First I am changing all time objects to time objects in desired timezone. And second, I am converting all the time objects in local timezone.

These are 2 different things.

In first case, I am doing this:

make_double_digit = function(num) {
  if(num < 10) {
    return ("0" + String(num));
  }else{
    return String(num);
  }
}

var start_time = new Date();
var year = String(start_time.getFullYear());
var month = make_double_digit(start_time.getMonth() + 1);
var day = make_double_digit(start_time.getDate());
var time_hour = make_double_digit(start_time.getHours());
var time_min = make_double_digit(start_time.getMinutes());
var time_sec = make_double_digit(start_time.getSeconds());

var new_date_format = [year, month, day].join('-') + " " + [time_hour, time_min, time_seconds].join(':');

return moment.tz(new_date_format, zone_name);

For ex: let start_time be 2015-dec-21 8:30:00 IST. So I am extracting year, month etc  from this date object and asking moment to return moment object for this string “2015-12-21 08:30:00” in US/Central timezone. It is not converting start_time to US/Central timezone but rather building time object in US/Central timezone.

In second case, I am doing this:

var timezoned_start_date = moment(vm.info.start_date).tz('US/Central');

var start_date = new Date();
start_date.setYear(timezoned_start_date.format('YYYY'));
start_date.setMonth(timezoned_start_date.format('MM') - 1);
start_date.setDate(timezoned_start_date.format('DD'));
start_date.setHours(timezoned_start_date.format('hh'));
start_date.setMinutes(timezoned_start_date.format('mm'));
start_date.setSeconds(timezoned_start_date.format('ss'));

I am converting the time object obtained in response which is in UTC format to US/Central time zone. and then building a javascript Date object from it.

Now start_date object is in local time zone and we can use this to compare it with Date objects as needed.

Conclusion:

Dealing with timezones is not very straight forward. And this is not the only way to do it. A lot of people(read StackOverflow) suggested that I convert timezones by adding/subtracting timezone offsets. I found this to be more easy to manage in future and for other team mates to understand.

Review of 2015

Now that we have entered into December, I think I can review 2015 which passed by at a lightning speed.

Let me start with some resolutions I made with myself for 2015. Here you can read about it.

My last years resolutions.

I said that I would:

1. Understand computer networks

Result: Failure.

Thought process: The plan was to read a book or two until I have a basic understanding of the topic.

Reason for failure/success: I think this went really low on my list of priorities.

2. Stick to one side project

Result: Mixed feelings.

Thought process: As history have suggested, I keep wandering from ideas to ideas and never actually end up doing one thing completely. So I had decided last year that I will be working on just one thing. And believe me I had said no to friends for side projects they wanted me to be involved with. I ended up starting ‘At a happy place‘  in January and it has been 11 months since I have been working on this. Although for the last few months I have not been able to give it time as much as I would have wished to. But I think the project still needs a lot more work and I have some really cool ideas for the project which I have no idea how I can implement them. 🙂

 

Reason for failure/success: I would not go ahead and call this a failure. This was definitely not a failure but I would also say I am not fully satisfied with the progress so far on it.

3. Do more open source contributions

Result: Mixed feelings.

Thought process: I started doing open source contribution somewhere around mid 2014 and I wrote this straight from the heart post about contributing to open source.

You can see my github open-source contribution chart for this year. Its not the prettiest thing you will see but it has improved a lot this year. But that being said I could have done a lot more.

Screen Shot 2015-11-30 at 10.04.33 pm

Reason for failure/success: I can`t call this an outright success or a failure either. I would rather say that there is a lot more I could have done.

4. Read more non-tech books(at least 4)

Result: Success.

Thought process: The plan was to read more books to get a broader sense of things and pick up style of writing of the people who are professionals as I believe I need to improve upon my style of writing.

I ended up reading 4 John Grasham`s novel. I really liked his style of writing. I also read a couple of other non-fiction books. In all I think I read 6 books.

Reason for failure/success: I would definitely call this a success as I have achieved the target I had set for myself. But now looking back at it, I think I will set a more difficult target for myself for the future as reading 4 books in an year is no big deal.

Now moving on to see how things went in my professional and personal life.

Professional Review:

  • Spoke at 2 international conferences in Belgium and Israel. So this makes it 5 conferences so far.
  • Became a collaborator on medium_editor open source project.
  • Started working on angular js.

Personal Life:

  • As I had mentioned in my last years post, we were looking forward to welcome a special new member to our family. God blessed my sister with a healthy baby and we call him “Manny”.IMG_4174
  • I did travel this year. I visited New York, Chicago, Ghent, Barcelona and Jerusalem this year. And also did 3 or 4 local trips with friends. So it was all round a fun filled year.
  • I did skydiving from 5000 meters in Barcelona which was pretty scary and fun at the same time. Here is the video.
  • I ran my first ever 10kms marathon.
  • A friend of mine and I had a horrific accident on a road trip. But all is good now.

 

What about 2016 ?

I have not actually thought of resolutions for this year. But I would like to improve upon a few things namely contributing to open source projects as it is a very satisfying experience. I would like to speak at more conferences as it gives me opportunity to make new friends and travel to different places. I would also like to improve ‘At a happy place’ as I know it could be a really good project. I would definitely like to learn a lot more about computer science in general and not just things related to my work. Also I would like to taste the ‘startup bug’ about which I have been thinking (thats all I have done) for past 2 years now.

Some moments from 2015:

DSC_0551

IMG_1279

 

I hope the world have a great 2016 which is full of peace and harmony and less of terrorism… and as Ellen says it “Be kind to one another everyone” …. !!