# Unix Epoch Time Conversion To Human Date and Time

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.

LVL 3
###### Who is Participating?

Commented:
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
``````
When the loop exits, month will be the correct month, and day will be the correct day of the month
0

Commented:
see
man mktime
and
man strftime
0

Author Commented:
my platform is windows not linux or unix
0

Commented:
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.
0

Author Commented:
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.
Thanks
0

IT Business Systems Analyst / Software DeveloperCommented:
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;
``````
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!!
0

Author Commented:
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.
0

IT Business Systems Analyst / Software DeveloperCommented:
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)
0

Commented:
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
else
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).
0

Author Commented:
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!!!
0

Author Commented:
Thanks for the solution Tommy,
50 % of your solution is working and gave me a way to do another 50 % ... thanks again .
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.