Home AMX User Forum AMX General Discussion

Anyone need unix timestamps or pretty dates (the calendar kind)?

I was bored again today - so I decided to write a netlinx time/date to unix timestamp (and back) converter and a formatted date function similar to php's date() to make use of a timestamp. If you're interfacing with devices that want a unix timestamp, or want prettier / different dates for touchpanels, this code might help.

For instance, this code:
send_string 0, fmt_date('\T\h\e \d\a\t\e \i\s l, F jS Y  g:ia  \D\a\y #z', unixtime_utc(LDATE, TIME))
sends to the console:
(0000752985) The date is Saturday, March 27th 2010  9:12pm  Day #85

Unix timestamps are provided adjusted to the local timezone (ala clkmgr or a specified offset) with unixtime(LDATE, TIME) or unixtime_now(), and not adjusted (UTC-based) with unixtime_utc(LDATE, TIME). DST via clkmgr is only barely supported - when it's on, it applies a +1hr offset... anyone have better documentation for the clkmgr_ functions? Right now my fmt_date() isn't timezone-aware so it wants a UTC based time, so don't get too confused with the example above :) Even if you don't care about unix timestamps, having something more than the built-in date types on panels that's easily adjustable might appeal to you. Yay ordinal suffixes!

I've also got functions to convert unix timestamps (say, from a remote webserver or device) back into AMX-compatible DATE/LDATE/TIME strings - unixtime_to_netlinx_ldate(), etc. if you have these date types and would prefer to work with AMX date/time strings.

Since I'm working solely residential now, I wrote (not on company time, sigh) a no-ip sync module and I may also write a dyndns-protocol compatible module. Anyone have use for these?

edit: 10000th post in this forum

Comments

  • truetrue Posts: 307
    If anyone indeed is interested, I've added this as part of the Netlinx Common Libraries.
  • urbanurban Posts: 2
    not working?

    hi,
    timestamp -> date is not working when is a leapyear...

    ---
    Error on line 558 and 559: in the "unixtime_to_raw_values" Function

    if (unixtime_year_is_leapyear(yr)) {
    //w2 = UNIXTIME_SECONDS_PER_DAY
    >do not overwrite this stack_var!
    //w = w - w2
    w=w - UNIXTIME_SECONDS_PER_DAY //do this way

    }
  • urbanurban Posts: 2
    thanks

    for this... is very useful... for me
  • a_riot42a_riot42 Posts: 1,624
    Has anyone tried this module to get Unix time? I am getting results that seem to indicate its an hour off (3600 seconds), so I'm guessing DST is throwing things off. I've compared to unixtime calculators online in my time zone, and to get them to match I have to add 3600 seconds.

    The underlying OS has all the time/date functions you could ask for. Too bad there is no exec() function.
    Paul
Sign In or Register to comment.