Google+

A Modest Proposal For Time

Time, when you really think about it, is CRAZY. We expect it to be this constant in life, slowly, we hope, marching forward, but invariably going quicker than we want. However, the concept of time, as in “What time is it?” has changed over the centuries, with the pace escalating during the information age. If there is one thing Einstein taught us with the Theory of Relativity, it is that time is not constant.

Melting Time

There are really two ideas of time… The first, relates to the time on a clock that we all reference. The second, is the larger concept of time that is part of the theories that Einstein laid out in his Special and General Theories of Relativity. We will focus on the clock for most of this.

Einstein (gratuitous Einstein picture)

When I interview people for jobs, or talk with young programmers, I will sometimes ask a variation of this question, "you are running a website and are taking orders for customers all over the world, how do you handle the date and time of an order?" This question is amazingly complex.

First, you get an idea if the person has a grasp of time zones and local geography beyond the one they are in, which is always a good first step. This leads to other discussions. Do they mention Daylight Saving Time? Do they mention what a pain Indiana and/or Arizona are, neither of which recognize Daylight Saving Time?

They make it this far, you are happy. Next however, you hope they mention there really isn’t a decent way to know where a user is when they place an order so that you can display a time when they think they bought their product. Unless they explicitly provide an address or a time zone, you are not guaranteed to know where they are. You can guess based on things like IP address, however, this costs money. You can ask the user if your application can use their browser geolocation feature to determine where they are, but if they are like me, this is a sign to go to another site.

So, you say to yourself, the best thing I can do when a user places an order is to ask them to provide a username that is tied to a physical address in the real world. This will surely solve the problem! No, it really won’t….. Do you save the time zone where they placed the order? If I am vacationing in sunny California and order the latest Marvel Blu-Ray to be shipped to my house, what is the proper order time from my perspective? The time back home or the time where I am?

None of this touches on craziness of time zone names. I live in the United States Midwest, home of Central Standard Time (CST). However, there is also Cuba Standard Time, China Standard Time, and Central Standard Time in Australia, all claiming the acronym CST. Which one wins?

So it is a GREAT interview question because you get an idea of the breadth of experience of a candidate in dealing with large scale applications. However, I only tend to ask this question if I am trying to be a deliberate jerk. It is a difficult question.

If you work with large companies or companies with interests all over the world, you realize how much time you spend talking about time. Most people I talk to have no idea that when it is 8:00 AM in the United States Midwest, in India it is 6:30 PM. That is right, not only is the time many hours different than ours, it is staggered 30 minutes. Throw in the fact that countries all over the world have different ways of dealing with Daylight Savings Time and sometimes their governments will change it at the last minute and you have many, many opportunities for error. Time is a constant source of discussion when you work with people across the globe. That discussion of time costs the world money and effectiveness.

So, I propose a few things. First, let me start in the United States. Let us abolish Daylight Savings Time. There really is no need for this anymore. We are not at war, and the cost savings for going through this process twice a year have not been proven out. Every person in Illinois who has missed a meeting with a colleague in Indiana when we “spring forward” or “fall back” can verify this.

Second, and I assume much more controversially, let us abandon time zones in the United States. We live in a time and place where we ALL need to be on the same time as a country. In our agrarian past, it may have made sense for 12:00 PM to represent when the sun was at its apex. However, for the most part, we are NOT an agrarian society currently. Our economy is an information and finance based economy. I can guarantee you, stock brokers living in California, don’t wake up at 9:30 AM PST for the opening bell on Wall Street, they wake and are ready to serve their customers every day by 6:30 AM PST to match the 9:30 AM EST opening bell. Our economy already is driven by the time when business happens, not when we want it to happen.

Many folks may think the idea of a single time zone in the states is crazy talk, and it probably is. As a country, we tend to be obstinate to change and prefer to cling to the way things have been done (I am looking at you Metric System conversion). However, time marches on, and more importantly, our economy marches on. It wasn’t until commerce became widespread, that the concept of time (or more exactly, time keeping) became prevalent in order to facilitate commerce. Our economy though has moved past local time, it has moved global. The reality is that countries like China are already ahead of us here, with a single time that spans five time zones. One needs to only look at the image of the differing morass of time zones from WikiPedia to realize there is a huge opportunity for improvement.

WikiPedia time zones

So then, let us take it to the logical conclusion. We should wipe out time zones everywhere in the world. They are no longer needed. ALL business should act on the same time. The first benefit of this is that the mental energy the world spends each day to just set up a meeting with colleagues disappears. Instead of everyone doing a little math determining their GMT offset, they just say what time. Everyone has the same reference. It all gets done much quicker. We loose the concept of 12:00 PM being when the sun is brightest, but the benefits are huge.

The second benefit is that computer systems are MUCH more efficient. While we will never get past the scourge of developers checking if an item is NULL, the second thing they spend their time on is dealing with dates and times in their programming language of choice. By consolidating on a single global time, that problem disappears. That is pretty cool. You may think this is superfluous, but it is analogous to the metric and British system of units. One need only look at the loss of the Mars Climate Orbiter because of unit conversions to realize there are real reasons for simplicity and very high costs for ignoring it!

So then, what time is it? Well, the answer is easy. We already have it and it is Coordinated Universal Time (UTC). The interesting thing is, from a programming standpoint, we are all moving there. If you do multi time zone or country work, you are most likely already saving time as UTC. If you aren’t, you soon will be as you move to a cloud infrastructure. Amazon Web Services (AWS) and Azure already default to UTC since their servers are geographically dispersed and they need to standardize on tme. This is so prevalent, that for me personally, on my development PCs I have two times displayed, one for local time and one for UTC time. When I code, I spend more time in UTC than my local time zone.

It is time for the geeks of the world to lead the charge on this. We need to encourage businesses to focus on driving costs down for doing business globally. We need to challenge politicians to take up the banner and make the world more connected and consolidated by having a single global time.

In the news lately has been the disappearance of Malaysian Flight 370. Take for instance this article at the New York Times that discusses the timeline of the plane radar reading. It says the last contact happened at 2:15 am local time. First, I assume they mean local time in Malaysia and not New York. If it is Malaysia, I have no idea without a little Internet searching what time that is for me. I think a good way to begin the journey is to whenever we display time to also display the UTC time. So 2:15 am local time in Malaysia becomes 2:15 am/Saturday 18:15 U, where the U designation indicates Universal time.

How hard can it be?!?!? Probably hard, the reality is it is coming sooner or later, with sooner being better. However, amazing things happen every day. Let us start. I am posting this article at 6 AM in the morning, but 11:00 U wherever you are.


 

Twitter Bootstrap Themes

With a name like Ptacek, you would of course, have to know I am half-Irish. So, to celebrate, we are going to wrap up a St Patrick's Day theme for the blog today (3/17/14). You may be saying to yourself, how could I spend so much time and effort totally retheming the blog to support a relatively minor holiday on the calendar? Easy! Twitter Bootstrap themes!

Twitter Bootstrap, is obviously, a very popular web CSS framework. It enables web developers, without a lot of CSS expereince to quickly create responsive sites that look decent out of the box. One of the interesting things you can do with version 3 of Twitter Bootstrap is quickly update your site easily using themes.

With Twitter Bootstrap, there are two ways you can do things. Option 1 is to download the minified files. This will result in three separate files, bootstrap.min.css , bootstrap-theme.min.css and bootstrap.min.js, which is used in conjunction with jQuery. The theme elements are then contained in the bootstrap-theme.min.css file.

The second approach you can use is to download the source files and compile the CSS yourself. This is done by downloading LESS files and using your favorite LESS compiler to generate CSS. There are also SASS files for the Ruby fans of the world. With the LESS approach, there are separate files for theming, a variable.less file and a bootstrap theme file. This is the approach I use for this site and most projects I start. Within the main site.css LESS file I import the theme files

@import "bootstrapTheme/bootswatch.less";
@import "bootstrapTheme/variables-bootswatch.less";

The reason I like using the LESS approach rather than just downloading the minified CSS files is the theme part is a bit more isolated. I can keep all the theme content, which I really think of as the "skin" for a Bootstrap site, in two separate files. This enables new versions of Bootstrap to be downloaded without loosing other CSS changes and to quickly swap them out, like we are doing here for St. Patrick's Day.

It should be noted, that out of the box, Twitter Bootstrap ships with a default theme, a theme that makes the site look like Twitter Bootstrap 2.x. There is quite a growing community for creating themes for bootstrap (it's not WordPress type size, that would just be crazy talk). Example sites include WrapBootstrap.com, Bootstraptor and the one I tend to gravitate to, Bootswatch.

To update your site UI you just simply download either minified CSS files and swap them out, or two LESS files and then reference them in your site. Since I use LESS, I download the LESS files, rename them to a theme name, which allows me to keep my original, and then update my site.css.less. I called the St Patrick's Day theme shamrock and my references are now

@import "bootstrapTheme/shamrock-bootswatch.less";
@import "bootstrapTheme/shamrock-variables.less";

Viola, a save and a publish later we have a total site look! It is like CSSZenGarden for mortals.

Rules

You can find the St Patrick's Day theme I downloaded from Bootswatch here. Obviously, since this is just for a short time, there are some UI things that would need to be redone on the site, but we will be back to our regularly scheduled programming soon!

Totally useless Ptacek news.... Obviously, the half Irish comes from my Mother's side. My Uncle Tom Durkin, a somewhat famous horse race announcer who used to call the Kentucky Derby and other Triple Crown races on television, would often get flown over to call the Irish Derby. Here is one of my favorite race calls he has ever done, but probably more appropriate for Talk Like a Pirate Day

Happy St. Patrick's Day!


 

National Pi Day - Calculating PI With AngularJS and a Monte Carlo Simulation

Every year on 3/14, Geeks everywhere can let their geek flag fly by celebrating National Pi Day. On this day, every year, the date equals the first three digits of π, 3.14. Last year we celebrated with a blog post, so let’s do it again!

In last year’s post, we calculated Pi using Leibiniz formula to determine π, which is a relatively slow algorithm.

This year we are going to look at a totally different approach, a Monte Carlo simulation. A Monte Carlo simulation is a statistical approach where you use random numbers to run a simulation over and over and record those results. It is very popular in the fields of finance, fluid mechanics and the study of Brownian Motion. For our scenario today, you can also use a Monte Carlo method to calculate π!

To do this, we are going to use two geometric formulas, one to calculate the area of a square and the second the area of a circle. If we put a circle of radius R in a square of length 2R, we have a scenario as shown in Figure 1.

Circle in Square

The area of the rectangle will then be 4R and the area of the circle will be πR. Dividing the area of the circle by the area of the rectangle give us π /4. Stated another way, the ratio of area of the circle to that of the square is Pi/4.

The Monte Carlo method comes into play because we will randomly put a point on the square. If the point falls within the circle, we record it. The total ratio of points in the circle (aka, the area of the circle) to total points (aka, the area of the square) will tell us π /4.

So, we now know a way to calculate π, the next step is to actually write some code. There are, obviously, many ways we could go and do this, but lately I have been kicking the tires with AngularJS and thought this would work as well as anything.

We won’t spend a lot of time jumping into AngularJS. You can check out some previous posts at http://www.jptacek.com/tags/angularjs/ for an introduction.

We will have two things, our chemistryApp.js file to create our angular application, piApp, and our controller, where the interesting stuff will happen.

Our HTML file is pretty basic,

<div id="piApp" ng-app="piApp">
    <div id="piCtrl" ng-controller="piAppController">
        <input id="iteration" data-ng-model="iterations"/><br/>
        <div id="piCalc" >{{pi}}</div><br/>
        <div id="piCalcDiff" >{{delta}}</div><br/>
        <button data-ng-click="calculatePi()" >Update</button>
    </div>
</div>

We will enter in the number of random dots (iterations) we want our simulation to run. The more dots, the closer we get. We will then display the calculated value of π and how close we are to π actual. Last, we have a button that we will click to execute our Monte Carlo simulation.

Our AngularJS controller is where the interesting stuff is happening, and that is not even particularly interesting.

We have created a function, determinePi, which will perform our calculation. We assume a circle with a radius of 1. We then generate two random numbers, one for the x position and one for the y position. We know that (x + y)=1 (the radius of our circle), so for any value that is less than this, we know we are inside the circle and increment a counter for hits inside. Once we are done with the iterations, we determine our calculated version of π and the difference from our known value of π for display on the page. In our calculation, we are only really doing a quadrant, just to make the code a bit easier to follow. It all works though since we are just talking about ratios.

This is not a super accurate, or super-fast method of calculating π, but it was an interesting chance to take a quick look at Monte Carlo methods, which are common place with a subset of our clients.


{{pi}}

{{delta}}

Happy Pi day! Go have a nice piece of Apple Pie and enjoy the day!!

This post originally appeared at Skyline Technologies


 

John Ptacek I'm John Ptacek, a software developer for Skyline Technologies. This blog is my contains my content and opinionss, which are not those of my employer.

Currently, I am reading Norse Mythology by Neil Gaiman

@jptacekGitHubLinkedInStack OverflowGoogle+