LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   converting epoch time to human readable format SDF (http://www.linuxquestions.org/questions/programming-9/converting-epoch-time-to-human-readable-format-sdf-925618/)

nano2 01-25-2012 06:51 AM

converting epoch time to human readable format SDF
 
Hi ,

Trying to convert the following epoch time say ( 1327486488 676088 ) to SDF using XHTML

Does anyone know if this is possible to do inside xhtml 1.0 ?

Thanks

Nominal Animal 01-25-2012 01:37 PM

To SDF? Using XHTML? XHTML is a content description language, and has no imperatives, nor facilities for data conversion.

If you can use Javascript (for example, if the XHTML is to be displayed in a standard web browser), you can create a Date object for the conversion:
Code:

var date = new Date(1000 * epoch);
var text = date.toString();

The current UNIX epoch time is seconds since 1970-01-01 00:00:00 UTC; Javascript uses milliseconds since the same epoch. That's where the multiply-by-thousand comes from.

If you encapsulate the epoch times in the XHTML content using for example a <span class="datemagic">epoch</span>, you should be able to use the onLoad handler for the body element to run a Javascript function that uses the Javascript DOM to convert the epoch numbers to a human-readable string in local time. (It would also mean that without Javascript, users would see the epoch numbers instead.) Although dynamic HTML is not recommended at all with XHTML content, in this case only the plain text string would be modified, and it should be completely safe.

Actually, it might be pretty nifty in any case: the page could display the times in local time, without any intervention from the server.

1327486488 seconds since epoch is "Wed Jan 25 10:14:48 2012 UTC", but I have no idea what the 676088 refers to, or how it affects the date.

nano2 01-26-2012 03:52 AM

I have tried using this but had no luck in getting the correct format

Code:

    <h:outputText value="#{timestamp}"> 
                        <f:convertDateTime dateStyle="full" /> 
    </h:outputText>


Have you ever used this above ?

lithos 01-26-2012 03:56 AM

Hi,

this site has all the conversions in one place, take what you need and implement it in your site.

good luck

Nominal Animal 01-26-2012 05:15 AM

Quote:

Originally Posted by nano2 (Post 4584768)
Have you ever used this above ?

No, I don't use Java server-side. Like I said, XHTML does not have any conversion facilities of its own. That syntax is parsed on the server side, most likely in some JSF (JavaServer Faces) or other server application framework. (XHTML is just the base storage format there, a sensible one because it can also be sent to a client, without any conversion, and handled as XML in the server application.) It will not work if you supply that XHTML to a client directly.

If you do use some framework (instead of publishing that XHTML as-is to clients), then you need to check the framework documentation. They differ, at least in the details.

If you do not use any server-side application or framework, but publish the XHTML content as-is, then the Javascript route is the one I would seriously recommend. In Javascript the conversion is very simple, and all sane browsers support the DOM interface you can use to convert the date/timestamps. (In fact, I do believe it would be easy to transform even text times in UTC to epoch time, then to local time using the user's preferred locale on the client side. If you want, I can try and post a real-life example you can try in your browser. Note that this approach also works even if you do use a server-side application framework; you just "post-process" the date/timestamps client-side.)

The page lithos linked to looks very good; it contains both examples, and links to implementations in different languages and situations. Thanks, lithos.


All times are GMT -5. The time now is 08:33 AM.