1

I'm writing a macro for slick-edit (with the C similar language slick-C)
As far as I found there are no time functions to help me convert epoch time (1321357827) to a human readable date and time

(if any of you knows how to do it in slick-C it's great, those of you who doesn't know it - assume C without time.h or any other libs)

Boaz
  • 4,864
  • 12
  • 50
  • 90
  • yes, sorry - data as well. fixing the question – Boaz Nov 15 '11 at 12:12
  • 1
    Robert Grudin cited in the GNU date manual: "Our units of temporal measurement, from seconds on up to months, are so complicated, asymmetrical and disjunctive so as to make coherent mental reckoning in time all but impossible." Time with its DST, leap-years and leap-seconds is highly nontrivial, so don't get the idea to implement it yourself. – thiton Nov 15 '11 at 16:10
  • @thiton - you are absolutely right. That's why I'm desperately asking for some ready made slick-C alternative (so far with no luck) – Boaz Nov 15 '11 at 16:45

2 Answers2

1

If you need only the time you could do:

sec_of_day = epoch % (24 * 60 * 60);
hour       = sec_of_day / (60 * 60);
minute     = sec_of_day % (60 * 60) / 60;
second     = epoch % 60;

. This is of course not considering the timezone of your system.

If you need the date, you need to consider leap years.

EDIT: Warning: this code does not take into account leap seconds.

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
  • Thanks. I thought maybe there is something better to do then that. You'll get your vote if nothing else pops out... – Boaz Nov 15 '11 at 12:13
  • Better add extra parentheses for the calculation: `minute = (sec_of_day % (60 * 60)) / 60;` In C all is fine, but even there it's not too much to add parentheses IMHO. But SlickC is not bound to the C rules (even if it would be OK here). – Johan Bezem Nov 15 '11 at 14:48
  • There have been leap seconds since 1970. How does this code take them into consideration? – thiton Nov 15 '11 at 16:11
  • @thiton: Unix-epoch based time ignores it (i.e. there are values that repeat for 2 seconds). This is well known, see e.g: DJB's discussion on libtai's page (http://cr.yp.to/proto/utctai.html). – ninjalj Nov 15 '11 at 19:34
1

In SE16 you'll find a whole class for date/time manipulation in se/datetime/DateTime.e. In addition, the built-in function _time has an option to return the epoch time. You should find enough example code there.

And for the basic algorithm I found another SO question answered on this: Includes a link to gmtime source. From there you should be able to adapt to SlickEdit code.

Community
  • 1
  • 1
Johan Bezem
  • 2,582
  • 1
  • 20
  • 47
  • using 15.0.1.3. Found DateTime but it only has fromTimeB. 16 has fromTimeG? – Boaz Nov 15 '11 at 13:41
  • No, it seems to have only fromTimeB and fromTimeF, no luck there, sorry. You have a `strftime` and a `printtime`, but they seem to use timeB as well. – Johan Bezem Nov 15 '11 at 14:07
  • just noticed the edit - I'm still debugging this on slick-C but it's the right way. tahnks! – Boaz Nov 16 '11 at 16:05