Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

How to Handle Date and Time with Simple Approac...

How to Handle Date and Time with Simple Approach !!

How to Handle Date and Time with Simple Approach !!

Avatar for Narendra Sisodiya

Narendra Sisodiya

August 05, 2016
Tweet

More Decks by Narendra Sisodiya

Other Decks in Technology

Transcript

  1. ABOUT ME @nsisodiya Narendra Sisodiya Engineering Manager cum Software Architect

    at Hotelsoft Inc. I ὑ JavaScript, I ὑ React.js, I ὑ Node.js,
  2. WTF FACTS ABOUT TIME/DATE ETC ➤ Samoa - 30 December

    2011 DO NOT EXIST ➤ Timezone Offset can change for future dates ➤ Storing time with timezone is problematic ➤ At the DST transition, there is time jump or time repeated. ➤ That means, Day duration is not exactly 24 Hours, for some days ! ➤ TIMEZONE etc is a big mess, DO NOT DEAL with It Until is absolute needed !!
  3. DIFFERENCE BETWEEN DAY AND TIMESTAMP 2016-07-14 start time of day

    end time of day FULL DAY = 86400000 MILLISECONDS (NOT ALWAYS TRUE) TIMESTAMP = EXACT MILLISECOND, TIME MOMENT
  4. 3 TYPES OF ATTRIBUTES DAY DAY + TIME Price of

    Hotel room is $200 per night on 29 July 2016 Rohan was absent on 1st April 2016 DAY + TIME + TIMEZONE In India, we will start meeting at 3:30PM at 2nd July 2016 we will start meeting at 3:30PM at 2nd July 2016 eat banana at 3:30PM of 3rd July 2016 and get rich TIMEZONE LESS WITH TIMEZONE TIMEZONE LESS
  5. 3 TYPES OF ATTRIBUTES DAY DAY + TIME DAY +

    TIME + TIMEZONE TIMEZONE LESS WITH TIMEZONE TIMEZONE LESS YYYY-MM-DD is just sufficient 2016-04-09T20:18:48-05:00 2016-04-09 2016-04-09T20:18:48Z Z = UTC 1469183888786 2016-04-09 T20:18:48Z Z = UTC UTC TIMESTAMPS
  6. ROOT CAUSE OF PROBLEMS - RC 1 JavaScript Dates are

    meant for exact `milliseconds with client timezone` & We do not have any Construct like new Day(‘2016-07-27’), WE end up using `Date` constructor for Day wise attributes.
  7. REMEMBER AGAIN If you try to represent a “Day Wise

    Attribute” with “millisecond” you will obviously get problems
  8. ROOT CAUSE OF PROBLEMS - RC 2 Serialised String flow

    over wire and Serialisation + DeSerialisation may not return exact object. DeSerialisation (Serialisation(☺)) = ☻
  9. YOU NEED TO UNDERSTAND JSON PROTOCOL you may or may

    not get the same object back in the whole process
  10. YOU NEED TO UNDERSTAND JSON PROTOCOL OUT IN undefined was

    removed String Object get converted into string Date Object get converted into UTC strings
  11. ROOT CAUSE OF PROBLEMS - RC 4 Irrespective of What

    Library you use, UTC TimeStamps (like -“2016-08-03T16:34:47.676Z" ) flow over wire which do not carry timezone info !
  12. WTF — ON WIRE - TIMEZONE INFO DO NOT FLOW

    “2016-07-17T10:00:00.000Z" flow over wire
  13. ROOT CAUSE OF PROBLEMS - RC 4 UTC string flow

    over wire and Client and Server may have there different understanding about timezone ! That create problem Client Server 2016-08-04T08:00:00.000Z 2016-08-04 1:30 PM In India 2016-08-03 10:00PM In US/Hawaii
  14. ROOT CAUSE OF PROBLEMS - RC 4 Slide from Maggi

    Pint - https://maggiepint.com/2016/04/28/javascript- mn-2016-04-27/ - Slider number 17 Even if you try to send 2016-04-09T19:39:00-05:00
  15. UTC STRINGS - FACTS ➤ On WIRE, UTC timestamp strings

    travel. ➤ You do not send timezone on wire. ➤ UTC Strings, DO NOT carry “TIMEZONE INFO” ➤ 2016-04-09T20:18:48Z ➤ Remember, Z means UTC
  16. ISO 8601 ➤ Date with time = 2016-07-22T10:38:08.786Z = 1469183888786

    AT INDIA AT USA ➤ JSON representation is always same
  17. FIRST DEFINE : TYPE OF ATTRIBUTE AND PURPOSE Q1 )

    Is it a DayWise attribute ? or Day + TIME ? or Day + TIME + TIMEZONE ?
  18. EXAMPLE 1 - HOTEL BOOKING MANAGER ➤ Assume, Hotel has

    only one room category and they have fixed rate during whole day. ➤ Hotel can be in any Country ➤ You need to show nearby Hotels to your current Location. ➤ Hotel Rates are too be shown with proper currency code. ➤ This is the most simple example and There is no possibility to have any DateTime or Timezone issue AT ALL.
  19. ➤ 10 August of New Delhi and Los Angeles are

    different there is a timezone difference. ➤ BUT ➤ IT DOES NOT MATTERS ➤ YES ➤ If on UI, Two Pages Look exactly similar than Technically there should not be any difference on their Backend Implementation!
  20. FROM DATE PICKER TO DB - USE “YYYY-MM-DD” This is

    a timezone Free Approach This is Simple and Tension free
  21. FROM DATE PICKER TO DB - USE “YYYY-MM-DD” 2014-01-05 JavaScript

    CLIENT DB There is no confusion between server to client communication 2014-01-05 2014-01-05
  22. SIMPLE MATH You can use any Date Library but use

    it for Processing. do not exchange Date or Moment Object.
  23. EXAMPLE 2 - TWITTER KIND APPLICATION ➤ Multiuser app. ➤

    User Tweet multiple times in a day. ➤ Tweet should show local time to a user, irrespective of where it is created !!
  24. FROM DATE PICKER TO DB - USE - UNIX TIMESTAMPS

    1468564804000 JavaScript CLIENT DB There is no confusion between server to client communication 1468564804000 1468564804000
  25. FROM DATE PICKER TO DB - USE - UNIX TIMESTAMPS

    This is a timezone Free Approach This is Simple and Tension free
  26. GIVE ME ALL TWEETS ON 2016-04-15 15 April 2016 will

    be different based on Location of User
  27. JavaScript DB APPLICATION SERVER $.ajax({ type: 'post', url: '/getAllTweetsOnDay', data:

    { giveDay: “2016-04-15” }, dataType: 'json' }); SELECT * FROM Tweets WHERE creation_dt BETWEEN 1460703600000 AND 1460789999999;