Daylight saving headache

How to (properly) handle daylight saving in our application and services?

Not so long ago, I’ve ported jalaali calendar for Elixir. In that process i’ve figured out Elixir’s standard Date structure is missing something to accomplish calendar conversions. So i’ve started a conversation in Elixir forum which you can read here

My idea was to force every calendar to provide callbacks to convert specific Date to ISO date or Unix Epoch (Which I liked more at that time!)

Then… I was intruduced to Date and DateTime complexities!

Calendars were not as easy as I thought it was. I even didn’t know that we have Leap seconds in some years!!!

 A leap second is a one-second adjustment that is occasionally applied to Coordinated Universal Time (UTC) in order to keep its time of day close to the mean solar time, or UT1. Without such a correction, time reckoned by Earth's rotation drifts away from atomic time because of irregularities in the Earth's rate of rotation.

The brilliant people in elixir community, @josevalim, @kip, @Qqwy came up with a nice solution using Ratta die. It is awesome an I think Elixir has one of the best native calendar implementation.

Anyways, I didn’t learn my lesson yet at that time. :(

So I begun to use epoch millisecond as time in one of enterprise projects. We needed an offline TimeTable to handle timings of events in our application. So we designed ZigorTimeTable

Each time table entry uses a startDate::DateTime as beginning of time, A duration::Long in milliseconds as time of availability and a cycle::Long in millisecond as how often is this entry repeated.

Everything was awesome and fine. It worked like a charm! You understand? Worked!

And suddenly, Daylight saving time occurred! :(( Every time table was messed up. they were all behind by one hour.

Reason?

because of DST+1 (Day light saving) there was one hour missing from DateTime but it wasn’t missing in milliseconds!

We had to change real times from server to wrong ones so the app would work correctly.

Conclusion

Please do not mess with calendar and calendar systems in your enterprise level projects. these methods should be tested and verified precisely over long periods of time and not by normal tests. Instead, try to use a known implementation like JodaTime via updated TZDB data.

Read More

Jalaali calendar for Elixir

jalaali package version 0.2 is released for Elixir.

It offers a new module Jalaali.Calendar which is an implementation of elixir’s Calendar module.

With the help of this module, it is possible now to convert DateTimes to each other easily by using DateTime.convert or Date.convert functions.

Long story short

The previous version of jalaali was a mess! really.

It just provided functions to users for converting from/to ISO calendar.

The most annoying thing was it actually did not change the calendar property in a Date/DateTime struct.

Even if it did it wouldn’t be a useful feature cause conversion was not possible throw Elixir < 1.5

So after a very long talk in the community about this which you can read here use of day number + day fraction was chosen to implement. This decision has made elixir one of rare languages with a very very very strong native calendar system.

It’s been a while since elixir 1.5.x was released and I’ve been really excited about adapting my jalaali library with the new Calendar system in elixir.

At first, I thought it may take me several days to make that happen, BUT surprisingly it was so easy that it only took me less than 5 hours (including tests and documentations) and jalaali 0.2 is now published.

I really liked it and may try adapting other calendar systems for elixir as well just for fun.

BTW, I will be happy if you take a look at jalaali github.com/jalaali/elixir-jalaali and maybe star it?

Thank you for reading.

Read More

Elixir1 5 Is Released

1502048221661 Elixir 1.5 Recently Elixir 1.5 has been released. Release Notes About 1.5

After a quick look on release notes you will notice that these updates, improvements and bugfixes are not major, Meaning Elixir is getting mature.

The most major change in my opinion is the Calendar module. Most of the callbacks to calendar module is changed. The reason to this change is because the Calendar conversion was impossible in Elixir <1.5

So to tackle this issue community decided (This is really pleasing for me to say that “Elixir community decided”) to use Ratta die algorithm for Calendar conversions. And that is a nice move because conversions of different chronologies in Elixir are now super accurate.

If you are interested in reading more about these changes you can visit Elixir docs. Link

Other than this the majority of changes in this release are “IEx Improvements”, “@impl for callbacks” and ability to set breakpoints on functions and modules (which is in favor of OTP/20)

I’m really excited about the future of Elixir and Erlang in our concurrent world.

Thank you for reading this.

T{:okT, :lets_party_یوتی_اف8}T

Wait! Did i forgot to mention UTF-8 atoms? :O

Read More

Prex, an Elixir code generator.

Prex is a code generator for the API Blueprint documents. It generates Elixir module to interact with your API.

What you up to?

Wow, It’s my second elixir project in a about a week after numero. (Yayy!)

I don’t know why i become creative when my exams start :

So what is it?

Prex is client code generator in elixir for whom is going to use a well documented API.

It can help developers save time and focus on their own issues instead of coding the API client.

Prex know ApiBlueprint language and can convert .apib files to elixir code.

Prex: github.com/alisinabh/prex

ApiBlueprint: apiblueprint.org

How you got idea?

Anyways, In last month i was dealing with REST Api services mostly from client perspective and i realized how much people don’t care about documentation of their APIs. That’s Crazy! There was even one company who hadn’t any docs at all! They just sent me their real-time generated docs (seriously?) as we were talking on messenger and almost all of it were WRONG!

Especially most .NET developers in my country care much less about documenting. (I was a .net developer for 7 years. I know what i’m talking about)

So what is the reason?

I think the first reason for that is education!

In colleges in IRAN there is absolutely no courses for documentation and if there is it’s fairly outdated.

The second reason i think is the ecosystem. For example .NET ecosystem won’t encourage a programmer to write good documents. There is no proper ways defined for documentations in that community. Even in C# learning videos no one talk about documentation in new features.

But take Elixir for example. It has a nice way of documenting your code. Its super friendly and in most Elixir tutorials they tech newbies about documentation and how important they are.

How is Prex?

I’ve only started working on Prex for two days. But its fairly usable now since Prex itself is not going directly into your compiled code. It just generates code for you that you can customize and edit.

I’m planing on completely supporting ApiBlueprint data and adding tests based on examples in blueprint files. More importantly Prex is going to support SOAP and WSDL. :D

Can you show us a sample?

Consider this example blueprint: real-world-api.apib

After running mix prex.gen.from_blueprint real-world-api.apib example Prex is going to generate following for you:

lib/
└── example
   └── posts.ex

example/post.ex :

# Created by Prex
defmodule Example.Posts do
  @moduledoc """
  This section groups App.net post resources.
  """

  @base_url "https://alpha-api.app.net"

  ###
  # API Calls
  ###

  # Post

  @doc """
  Returns a specific Post.

  ## Parameters
    - postid: The id of the Post.
  """
  def retrieve_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}"
    HTTPoison.request(:get, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def retrieve_a_post!(postid \\ "") do
    {:ok, result} = retrieve_a_post(postid)
    result
  end

  @doc """
  Delete a Post. The current user must be the same user who created the Post. It
returns the deleted Post on success.

  ## Parameters
    - postid: The id of the Post.
  """
  def delete_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}"
    HTTPoison.request(:delete, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def delete_a_post!(postid \\ "") do
    {:ok, result} = delete_a_post(postid)
    result
  end

  # Posts Collection

  @doc """
  Create a new Post object. Mentions and hashtags will be parsed out of the post
text, as will bare URLs...
  """
  def create_a_post do
    req_url = Path.join @base_url, "/stream/0/posts"
    HTTPoison.request(:post, req_url)
  end

  def create_a_post! do
    {:ok, result} = create_a_post()
    result
  end

  @doc """
  Retrieves all posts.
  """
  def retrieve_all_posts do
    req_url = Path.join @base_url, "/stream/0/posts"
    HTTPoison.request(:get, req_url)
  end

  def retrieve_all_posts! do
    {:ok, result} = retrieve_all_posts()
    result
  end

  # Stars

  @doc """
  Save a given Post to the current User’s stars. This is just a “save” action,
not a sharing action.

*Note: A repost cannot be starred. Please star the parent Post.*

  ## Parameters
    - postid: The id of the Post.
  """
  def star_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}/star"
    HTTPoison.request(:post, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def star_a_post!(postid \\ "") do
    {:ok, result} = star_a_post(postid)
    result
  end

  @doc """
  Remove a Star from a Post.

  ## Parameters
    - postid: The id of the Post.
  """
  def unstar_a_post(postid \\ "") do
    req_url = Path.join @base_url, "/stream/0/posts/{post_id}/star"
    HTTPoison.request(:delete, req_url, body: Poison.encode!(%{"post_id" => postid}), headers: ["Content-Type": "application/json"])
  end

  def unstar_a_post!(postid \\ "") do
    {:ok, result} = unstar_a_post(postid)
    result
  end

end

Have fun integrating with APIs :)

Read More

Diy 3d Printer With Cd Rom

1491130326291 DIY 3D printer with CD Roms build Today is last day of Nowruz holidays in Iran.

Having Fun?

I didn’t write much code in holidays. I’ve decided to spend more family time in holidays and have some fun. But as you can guess, i could’t resist the Cheap 3D printer Project. It was so damn fun :D

What did you do?

I’ve successfully competed the build. (Yayyyyy!) The X, Y and Z axis are good. the platform is fair. Extruder is a bit weak! and the Hotend Rocks! Im Using an Arduino UNO as controller with grbl 1.1f (Which is a CNC controller software for atmega32)

What can it do?

grbl supports G-Code but grbl doesn’t know about Extruder and Hotend temperatures. It only knows spindle and its direction.

Sure you can use spindle enable as extruder controller BUT the tricky part is slicers does not know spindle G-Code. So i had to design a script that translate Reprap flavor G-Code into grbl G-Code. It still needs some work.

But Currently i can do fallow paths on my printer with help of MakerCam.com I have done stars, hand, rocket, and eiffel tower.

3D printed objects

Build size?

It is roughly 35x35x35mm

Extruder and hot end?

I have used a 3D printing pen as hotend and extruder. I have completely removed pen’s circuit, connected the hotend to the power supply and the extruder to arduino PWM pin of grbl (D11)

Matterial?

The pen came with it’s own 1.75mm ABS filament.

What about the housing?

Uh… Due to small build size i cannot print NanoPi housing with it. NanoPi NEO has dimensions of 40x40x40mm.

But i’m planning on building a bigger Reprap printer without the kit. If i can finish my works i may be able to do that.

Wasn’t it a waste of your time?

Absolutely not! I’ve learned so many things with this project and this is really the important part. learning everyday and having fun. it was a lot of fun finishing this project as i didn’t think i could do that at the very begining.

If you are interested in building this you can click HERE for instructions

Thank you for reading this

T{:okT, :lets_3d_print!}T

Read More

Nanopi Neo Housing

1490014210891 NanoPi NEO Housing So in the last post Nanopi Neo as Webserver i came up to idea of serving alisinabh.com on a NanoPi Neo small arm based computer.

The latest things that i struggled with was:

  • Housing
  • Heatsync
  • Static IP

About the heatsync, I found an old asus mainboard which had a tiny heatsync near it’s south bridge. It fit perfectly. Even i used some thermal paste on back of board for better heat flow.

But about the housing… Things got out of hand. Seriously out of hand!

First i wanted to outsource printing of the normal NanoPi Neo housing (Since i cannot buy it from my location) But…

I really wanted a 3D printer for long time! Maybe buy one?

No shit! They’re really expensive and out of my budget.

So after some researching around 3D printers i ended up reading “Super Cheap 3D Printer From CD-Rom Drives”. Of course it is not going to be in any good shape or quality bit it can bdo the job i think.

So this is the reason for the delay of serving my blog on a NanoPi NEO board.

Thank you for reading this.

T{:okT, :cheers!}T

Read More

NanoPi Neo as Webserver

What could go wrong running my blog on a NanoPi NEO?

This weekend I was really bored. I just thought maybe its a good idea to play with the nerves-project a bit, So I went to find my raspberry pi from the closet and oh boy, It was a giant mess :|

Throw the process of finding the rpi_2 something caught my eye. A NanoPi NEO I bought it around a year ago to make an automatic garden keeper. but things went wrong after I sank it in mineral oil for cooling. the board went crazy and froze 10 seconds after bootup. I’ve wrapped it inside some paper towels and left it inside closet.

“Let’s try and see if it’s still broken.” I downloaded UbuntuCore with Qt-Embedded Image File for NanoPi NEO, flashed image into sd card and plugged it in. It worked!! Maybe after all time really heals some wounds ;)

Anyways, it was stable. I liked the performance of Allwinner H3 SOC and it is absolutely a charming feeling watching such a tiny computer working with fair performance. Wait a minute… Can I host this blog on it?

This single seed of idea has taken my entire weekend. I first tried to install Erlang on it. but binaries for arm71 was old. I’ve downloaded otp source code from github and build it on NanoPi NEO. It took about 40 minutes to build. I was not disappointed. In fact, I was surprised because I thought it may take a couple hours.

Then I tried building Elixir from source code. It was all good until it became to compiling unicode. As @josevalim said in issue #5856 to me, compiling unicode module requires loading large amount of unicode tables and optimizing it and because I only have 512mb of RAM and no swap, OS killed the build process. He suggested using precompiled Elixir. (Why I didn’t thought of that?). And Oh boy. It worked.

Elixir 1.4.2 and OTP 19.2 were installed and working on NanoPi NEO.

Now I just needed to run my blog on it. After cloning mix phoenix.server didn’t let me down. Alisinabh.com has successfully built and run on NanoPi NEO (since I use flat files as database, I don’t need anything else.)

So after all, I decided to host alisinabh.com on my NanoPi NEO. there is only a couple things I need to worry about for doing so:

  • Print a housing and get a heatsync for it.
  • Get an static IP address for my home internet connection or use DDNS (I don’t like this!)

Thank you for reading.

Read More

AWS S3 Worldwide Outage!

About a couple weeks ago we decided to use AWS S3 for storage of a mobile application which needed about 3gb of data dynamically.

Read More

Old code backfires!

As a daily programmer who lives by programming as a profession, Sometimes projects are not cool and they don’t cheer you up.

Specially if they are you OLD CODES -__-

Your old projects can kill you when they are still in use. Every day you learn more and they that code makes less sense. But sometimes things go wrong when you realize that the code is bad an unreliable AT THE SAME TIME!

For instance let me tell you my story about one.

I’ve developed an Asterisk AGI server with .net and AsterNET. AsterNET doesn’t use the async feature of .net and uses a Thread Pool.

So if you need 500 calls, you’ll need 500 threads!

We’ve been migrated our new projects to a new server in the last two years but our old projects are still running on AsterNET server. The problem with that server is it will crash from time to time. Because of StackOverflow exceptions. The old projects are not valuable enough to fix and not so useless to just go on.

So, I came up with a fairly good solution. Using a process supervisor (AKA Safe process runner).

It will simply run the process and wait for it to exit. If so, SafeProcess will re-run the process.

It’s not the perfect solution but it will buy me time and because this exception happens rarely (once or twice a month on average traffic) we can ignore it.

SafePorcess is available to use for everyone under no license: https://github.com/alisinabh/SafeProcess

Read More