<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Others on DecipherMiddleware</title><link>https://blog.deciphermiddleware.in/categories/others/</link><description>Recent content in Others on DecipherMiddleware</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Wed, 30 Dec 2020 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.deciphermiddleware.in/categories/others/index.xml" rel="self" type="application/rss+xml"/><item><title>A brief about time in milliseconds and conversion to a human-readable form</title><link>https://blog.deciphermiddleware.in/posts/a-brief-about-time-in-milliseconds-and/</link><pubDate>Wed, 30 Dec 2020 00:00:00 +0000</pubDate><guid>https://blog.deciphermiddleware.in/posts/a-brief-about-time-in-milliseconds-and/</guid><description>&lt;p&gt;Many a time, we come across situations wherein we need to convert the timestamp available(a human-readable form of date-time representation) into milliseconds(long number representation of date-time) or vice-versa. For example&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;DateTime in ISO 8601&lt;/th&gt;
&lt;th&gt;DateTime in milliseconds&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2020-11-15T08:00:00+00:00&lt;/td&gt;
&lt;td&gt;1605427200000 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2020-07-01T13:00:00+00:00&lt;/td&gt;
&lt;td&gt;1593608400000 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Seeing the long millisecond number, we generally get confused about how it is converted. We have conversion functions available in almost every programming language which we can think of. But, we always wonder about the logic behind the same. This article will give a brief idea about such questions. Hope you enjoy reading this article. Please share your thoughts in below comment section.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; UTC time zone is considered as standard for this article. For other time zones, offset might need to be considered.&lt;/p&gt;
&lt;h2 id="time-in-milliseconds"&gt;Time in milliseconds &lt;a href="#time-in-milliseconds" class="anchor"&gt;🔗&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Time in milliseconds is the number of milliseconds elapsed since &amp;ldquo;1st January'1970 00:00:00.000 UTC&amp;rdquo;. For e.g. 1st January'1970 12:00 PM, value in milliseconds will be 43200000 ms, i.e. 12 hours after 1st January'1970 00:00:00.000 UTC.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;12 hours = 12&lt;em&gt;60&lt;/em&gt;60*1000 ms = 43200000 ms&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id="table-for-unit-conversions-of-time"&gt;Table for unit conversions of time. &lt;a href="#table-for-unit-conversions-of-time" class="anchor"&gt;🔗&lt;/a&gt;&lt;/h3&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Unit&lt;/th&gt;
&lt;th&gt;Conversions Table&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1 sec&lt;/td&gt;
&lt;td&gt;1000 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 minute&lt;/td&gt;
&lt;td&gt;60 secs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 hour&lt;/td&gt;
&lt;td&gt;60 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 day&lt;/td&gt;
&lt;td&gt;24 hours&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 week&lt;/td&gt;
&lt;td&gt;7 days&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 year&lt;/td&gt;
&lt;td&gt;365.2425 days(as per tropical year)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="calculations"&gt;Calculations &lt;a href="#calculations" class="anchor"&gt;🔗&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;To convert time in milliseconds, we need to calculate the total milliseconds elapsed since &amp;ldquo;1st January'1970 00:00:00.000 UTC&amp;rdquo;. Basic formulae to calculate date-time will be:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Time in milliseconds = (((365.24&lt;em&gt;Y+D)&lt;em&gt;24&lt;/em&gt;60&lt;/em&gt;60&lt;em&gt;1000) + (hh&lt;/em&gt;60&lt;em&gt;60 + mm&lt;/em&gt;60 + ss)*1000 + sss) ms&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Where, Taking 2010-01-10T14:23:59.000+00:00 as reference&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Representation of DateTime in ISO 8601 : YYYY-MM-DDThh:mm:ss.sss&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;YYYY = Year representation. For e.g. 2010&lt;/p&gt;
&lt;p&gt;MM = 2-digit representation of month. For e.g. 01(January)&lt;/p&gt;
&lt;p&gt;DD = 2-digit representation of Date. For e.g. 10&lt;/p&gt;
&lt;p&gt;Y = Year-1970. For eg., if Year is 2010, then the value of Y will be 2010-1970=40.&lt;/p&gt;
&lt;p&gt;D= Number of days past in that calendar year. For eg. the date is 10-01-2010, then D= 10, i.e. 10 days are passed in the 2010 calendar year.&lt;/p&gt;
&lt;p&gt;hh= 2 digit representation of Hours in 24-hour clock, e.g. 14,&lt;/p&gt;
&lt;p&gt;mm=2 digit representation of Minutes, e.g. 23&lt;/p&gt;
&lt;p&gt;ss= 2 digit representation of seconds, e.g. 59&lt;/p&gt;
&lt;p&gt;sss= milliseconds, e.g. 000&lt;/p&gt;
&lt;p&gt;For example, utilizing the above formulae.&lt;/p&gt;
&lt;p&gt;2020-11-15T08:23:59.000+00:00&lt;/p&gt;
&lt;p&gt;Time in milliseconds = (((365.24&lt;em&gt;50+320)&lt;em&gt;24&lt;/em&gt;60&lt;/em&gt;60&lt;em&gt;1000)+(14&lt;/em&gt;60&lt;em&gt;60 + 23&lt;/em&gt;60+59)*1000 +000)ms&lt;/p&gt;
&lt;p&gt;(18582*86400)*1000 + (50400+1380+59)*1000&lt;/p&gt;
&lt;p&gt;1605484800000 + 51839000&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;1605536639000 ms&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="time-in-human-readable-form"&gt;Time in human-readable form &lt;a href="#time-in-human-readable-form" class="anchor"&gt;🔗&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;We generally see timestamp in ISO 8601 format but if we are provided the time in milliseconds then it is hard to read. We can convert date time information in format like &amp;ldquo;YYYY-MM-DDThh:mm:ss.sss+00:00&amp;rdquo; which is nothing but the time format in ISO 8601 format as shown in above example, from given value of time in milliseconds. To do so, let us take &amp;ldquo;1605622617000 ms&amp;rdquo; as example.&lt;/p&gt;
&lt;p&gt;Let T= 1605622617000 ms&lt;/p&gt;
&lt;p&gt;sss= T % 1000 =1605622617000 % 1000 ms = 000 ms&lt;/p&gt;
&lt;p&gt;Let tt, represents time in seconds=floor(T/1000 )seconds = 1605622617 s&lt;/p&gt;
&lt;p&gt;ss = (tt) % 60 = (1605622617) % 60 = 57&lt;/p&gt;
&lt;p&gt;mm = ((tt-ss)/60) % 60 = ((1605622617-57)/60) % 60 = 26760376 % 60 = 16&lt;/p&gt;
&lt;p&gt;hh = ((tt-ss-mm*60)/3600) % 24 = (1605622617-960-57)=(1605621600/3600) % 24 = 446006 % 24= 14&lt;/p&gt;
&lt;p&gt;Year_d(Year difference) = floor(((tt-hh&lt;em&gt;3600-mm&lt;/em&gt;60-ss) /86400) /365.24)=floor(((1605622617-50400-960-57)/ 86400) /365.24) = floor((1605571200/86400) /365.24) = floor(18583/365.24) = floor(50.88) = 50&lt;/p&gt;
&lt;p&gt;YYYY = 1970 + Year_d = 1970 + 50 = 2020&lt;/p&gt;
&lt;p&gt;To calculate month and day values, we need to extract day of the year.&lt;/p&gt;
&lt;p&gt;Day_Y_v = (tt - Year_d&lt;em&gt;365.24&lt;/em&gt;86400 - hh&lt;em&gt;3600 - mm&lt;/em&gt;60 - ss)/86400 =(1605622617-1577836800-50400-960-57)/86400 = 27734400/86400 =321&lt;/p&gt;
&lt;p&gt;Day of the year, Day_Y = ceiling(Day_Y_v)&lt;/p&gt;
&lt;p&gt;Note: If the year value is a leap year, then we need to add 1 to the value of day of the year to adjust tropical year and Sidereal year.&lt;/p&gt;
&lt;p&gt;Day_Y = ceiling(Day_Y_v) + 1 = (321) + 1 =322&lt;/p&gt;
&lt;p&gt;Since 2020 is leap year.&lt;/p&gt;
&lt;p&gt;Now the day number 322 is 17th November. Therefore:&lt;/p&gt;
&lt;p&gt;DD = 17&lt;/p&gt;
&lt;p&gt;MM = 11&lt;/p&gt;
&lt;p&gt;Taking the extracted values, date time in ISO 8601 format will be &amp;ldquo;2020-11-17T14:16:57.000+00:00&amp;rdquo;&lt;/p&gt;</description></item></channel></rss>