Solved

Unix Epoch Time Conversion To Human Date and Time

Posted on 2013-02-04
11
1,386 Views
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.
1
Comment
Question by:nirdeshsingh
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 38850815
see
man mktime
and
man strftime
0
 
LVL 3

Author Comment

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

Expert Comment

by:TommySzalapski
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.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 3

Author Comment

by:nirdeshsingh
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.
Thanks
0
 
LVL 35

Expert Comment

by:mccarl
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!!
0
 
LVL 3

Author Comment

by:nirdeshsingh
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.
0
 
LVL 35

Expert Comment

by:mccarl
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)
0
 
LVL 37

Expert Comment

by:TommySzalapski
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
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
 
LVL 3

Author Comment

by:nirdeshsingh
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!!!
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 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
0
 
LVL 3

Author Closing Comment

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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

786 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