Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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.

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.

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.

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

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!!
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 timeYou 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)

I use

total_days = floor(epoch/(60*60*24)

year = floor((total_days+.5)/365.

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).

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.

All Courses

From novice to tech pro — start learning today.

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

Open in new window

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