
/*----------------------------------------------------------------------+
 |                                                                      |
 |      main-wtime.c -- dumb tester for wtime_t                         |
 |                                                                      |
 +----------------------------------------------------------------------*/

#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>

#include "base.h"
#include "wtime.h"

int main(void)
{
        char buf[WTIME_STRING_LEN_MAX+1+1];
        buf[sizeof(buf)-2] = 2;
        buf[sizeof(buf)-1] = 1;

        wtime_init();

        wtime_t timestamp;

        /* test: current time */
        wtime_get_current(&timestamp);
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* test: hack it as solar west */
        timestamp = (timestamp & ~63) | wtime_zone_solar_west;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* test: hack it as solar east */
        timestamp = (timestamp & ~63) | wtime_zone_solar_east;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* 1 second before epoch */
        timestamp = (~(~0LL << 38) << 26) | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* highest timestamp */
        timestamp = (~(~0LL << 37) << 26) | (999999L << 6) | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* highest timestamp, -1200 */
        timestamp = (~(~0LL << 37) << 26) | (999999L << 6) | wtime_zone_w1200;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* highest timestamp, +1200 */
        timestamp = (~(~0LL << 37) << 26) | (999999L << 6) | wtime_zone_e1200;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* lowest timestamp */
        timestamp = ((~0LL << 37) << 26) | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* lowest timestamp + 11 hours */
        timestamp = (((~0LL << 37) + 11L * 3600) << 26) | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* lowest timestamp + 13 hours */
        timestamp = (((~0LL << 37) + 13L * 3600) << 26) | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* lowest timestamp -1200 */
        timestamp = ((~0LL << 37) << 26) | wtime_zone_w1200;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* lowest timestamp +1200 */
        timestamp = ((~0LL << 37) << 26) | wtime_zone_e1200;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* epoch start + 12 hours UTC+1200 */
        timestamp = 0x800002a300000031;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* epoch start + 24 hours UTC+0000 */
        timestamp = 0x8000054600000019;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* epoch center */
        timestamp = 0LL | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* epoch center + 30 hours */
        timestamp = ((30LL*3600)<<26) | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* fake leap second */
        timestamp = (((~0LL<<37)+59LL)<<26) | (1000000L<<6) | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* fake leap second */
        timestamp = (((~0LL<<37)+59LL)<<26) | (1000001L<<6) | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* fake leap second at day-end */
        timestamp = (((~0LL<<37)+59LL)<<26) | (1009876L<<6) | wtime_zone_z0000;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* fake leap in a solar time zone */
        timestamp = (((~0LL<<37)+59LL)<<26) | (1009L<<16) | (35L<<6) | wtime_zone_solar_east;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* fake leap in a solar time zone */
        timestamp = (((~0LL<<37)+59LL)<<26) | (1009L<<16) | (989L<<6) | wtime_zone_solar_west;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* fake leap second at day-end */
        /* some random */

        timestamp = 0x5cba23f8564f28f9LL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        timestamp = 0x43dd103dca5e9303LL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        timestamp = 0x8b4ce47ea13df051LL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        timestamp = 0xf571cc26b66f9177LL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        timestamp = 0x91580d585c948c1cLL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        timestamp = 0x496ace4709e6cd04LL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        timestamp = 0x5786d4586ef8c6c0LL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        timestamp = 0x30a442b0d161a4a3LL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        timestamp = 0LL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        timestamp = -1LL;
        wtime_to_string(buf, sizeof buf, timestamp);
        printf("[0x%016llx] %s\n", timestamp, buf);

        /* loop over the whole range */
#if 0
        timestamp = ((~0LL << 37) << 26) | wtime_zone_z0000;
        for (;;) {
                wtime_to_string(buf, sizeof buf, timestamp);
                printf("[0x%016llx] %s\n", timestamp, buf);

                wtime_t next = timestamp + ( (24LL * 3600) << 26 );
                if (next < timestamp) break;
                timestamp = next;
        }
#endif
        assert(buf[(sizeof buf)-1] != 2);
        assert(buf[(sizeof buf)-1] == 1);

        return 0;
}

/*----------------------------------------------------------------------+
 |                                                                      |
 +----------------------------------------------------------------------*/

