Unix Epoch Time Conversion To Human Date and Time

Posted on 2013-02-04
Medium Priority
1 Endorsement
Last Modified: 2013-02-06
Hello Experts,
I am having a table with an attribute Epoch Time in Map Basic.
I just  want to know the way to convert this epoch time in human date time step by step in mathematical formulas or in programming.

For Example I am having an epoch time "1346432476"
From the reference of website "http://www.epochconverter.com/"
the result is
 "GMT: Fri, 31 Aug 2012 17:01:16 GMT" & 
Your time zone: 8/31/2012 10:31:16 PM GMT+5.5

I just want to know the in between process.

Thanks in advance.
Question by:nirdeshsingh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
LVL 84

Expert Comment

ID: 38850815
man mktime
man strftime

Author Comment

ID: 38850830
my platform is windows not linux or unix
LVL 37

Expert Comment

ID: 38852128
I just want to know the in between process.

The epoch time is the number of seconds since Midnight, January 1, 1970
The only complicated part to the calculation is leap years.
If you divide the number by 60*60*24, you'll get the number of days since the epoch, etc.
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.


Author Comment

ID: 38853732
Tommy.. The link i have given above , shows the same thing you said.
But i just want to know the in-between process in mathematical way like how they are converting!
like exact month & day
And also leap year is not a problem as i can add no of days for every leap year after 1970.
Please provide me a proper solution for this.
LVL 36

Expert Comment

ID: 38853879
I have no experience in Map Basic but all you need is the usual logic operators and IF functions and some basic math operators (I will leave for you to find out exactly how to do these in Map Basic). Apart from your normal add and subtract operators, you will need to use integer division (ie. division where the remainder is discarded... 9 / 4 = 2  NOT 2.25) and modulus operator (ie. the remainder from a division operation.... 9 / 4 equals 2 with 1 remainder, so 9 MOD 4 = 1)

Ok, here goes... assuming the epoch time is in a variable called 'epoch' (and note that epoch gets modified along the way here, so make a copy if you wish to keep the original value)

seconds = epoch MOD 60;
epoch = epoch / 60;
minutes = epoch MOD 60;
epoch = epoch / 60;
hours = epoch MOD 24;
epoch = epoch / 24;

            -- Now the epoch variable actually holds the number of days since 1/1/1970

leapdays = (epoch + 672) / 1461;
year = (epoch - leapdays) / 365;
epoch = epoch - ((year * 365) + ((year + 1) / 4));

            -- Now the epoch variable actually holds the number of days since the start of the year

month = 1;
if (epoch > 31) {
        epoch = epoch - 31;
        month = 2;
        febDays = 28 + (((year + 1) MOD 4) / 3);
        if (epoch > febDays) {
                epoch = epoch - febDays;
                month = 3;
                if (epoch > 31) {
                        epoch = epoch - 31;
                        month = 4;
                        if (epoch > 30) {
                            epoch = epoch - 30;
                            month = 5;
                                  -- Continue along this line for each month of the year
year = year + 1970;
dayOfMonth = epoch;

     -- Output the result. You would do better formatting than the below but again I don't know Map Basic
print dayOfMonth + "/" + month + "/" + year + "  " + hour + ":" + minute + ":" + second;

Open in new window

I haven't tested the above code, but it should be pretty much right. Note though that it will fail for dates after 28 Feb 2100 and probably for dates before 1 Mar 1900, as it doesn't include the special leap day handling for years evenly divisible by 100. If you really want that, it would require quite a bit more logic which I will leave up to you to work out!!

Author Comment

ID: 38854032
mccarl.. logic u have given i am sure that it will work fine but i want to know about how to find the exact month of given epoch time.
LVL 36

Expert Comment

ID: 38854086
Sorry, around line 15 of the above, add...

epoch = epoch + 1;

I forgot that as the day of month (or day of year) is 1-based where most of the rest of that maths is 0-based.

but i want to know about how to find the exact month of given epoch time
You are missing the point.... the above *IS* the way to find that out. Trust me, if there was an easier way, I wouldn't have spent the time doing all the above for you. I thought it would be pretty obvious to see that there is no magic solution to this. Different months have different numbers of days in them, so any logic will complicated by this. (along with the fact that leap days complicate things further)
LVL 37

Expert Comment

ID: 38855186
The other way to get the month is to build an array indexed on day of year.
I use
total_days = floor(epoch/(60*60*24)
year = floor((total_days+.5)/365.25)
day_of_year = floor(total_days - year*365.25 + 1.5)

if (year mod 4 == 0 and day_of_year >= 50) -- It's at or past Feb 29 in a leap year
  leap_day = 1
  leap_day = 0

month = month_array[day_of_year - leap_day]
day = day_of_year - days_array[month] - leap_day

if (month == 2 and leap_day = 1) -- It is Feb 29, fix day
  day = day + 1

Where month_array is 1,1,1,1,1..., 2,2,2...., 3,3,3... (31 1s, then 28 2s, then 31 3s, etc)
and days_array is 0, 31, 49, etc (total days in year before that month)

This runs a lot faster if you need to do it thousands of times but takes a bit more RAM (less that 1KB if you use 2 byte integers).

Author Comment

ID: 38858286
Hi Tommy, Thanks for the help.
I am just confused with "month = month_array[day_of_year - leap_day]" this line and afterwards ..Can you please elaborate? And i am bale to calculate years and day, but what about month!!!
LVL 37

Accepted Solution

TommySzalapski earned 1500 total points
ID: 38860166
That one was assuming you really cared about speed (unlikely) and wanted to create an array of 366 ints that mapped days of the year to months. Most likely you don't need to do millions of these per second so you should just use an easier/cleaner approach

(note that this is C code and % is modulo)

// Create the month array
int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) // all the correct leap year checks
  days_in_month[1]++; // Add 1 to February

month = 0; // 0 is January, we'll fix that later
day = day_of_year; // start will all the days
// As long as there are too many days, we need to add a month
while (days > days_in_month[month])
  days = days - days_in_month[month];
  month = month + 1;

month = month + 1; // This makes 1 be January, 2 be February, etc

Open in new window

When the loop exits, month will be the correct month, and day will be the correct day of the month

Author Closing Comment

ID: 38862662
Thanks for the solution Tommy,
50 % of your solution is working and gave me a way to do another 50 % ... thanks again .

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Six Sigma Control Plans
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question