nsprpub/pr/tests/timemac.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/timemac.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * file: timemac.c
    1.11 + * description: test time and date routines on the Mac
    1.12 + */
    1.13 +#include <stdio.h>
    1.14 +#include "prinit.h"
    1.15 +#include "prtime.h"
    1.16 +
    1.17 +
    1.18 +static char *dayOfWeek[] =
    1.19 +	{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };
    1.20 +static char *month[] =
    1.21 +	{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    1.22 +	  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" };
    1.23 +
    1.24 +static void printExplodedTime(const PRExplodedTime *et) {
    1.25 +    PRInt32 totalOffset;
    1.26 +    PRInt32 hourOffset, minOffset;
    1.27 +    const char *sign;
    1.28 +
    1.29 +    /* Print day of the week, month, day, hour, minute, and second */
    1.30 +    printf( "%s %s %ld %02ld:%02ld:%02ld ",
    1.31 +	    dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
    1.32 +	    et->tm_hour, et->tm_min, et->tm_sec);
    1.33 +
    1.34 +    /* Print time zone */
    1.35 +    totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;
    1.36 +    if (totalOffset == 0) {
    1.37 +	printf("UTC ");
    1.38 +    } else {
    1.39 +        sign = "";
    1.40 +        if (totalOffset < 0) {
    1.41 +	    totalOffset = -totalOffset;
    1.42 +	    sign = "-";
    1.43 +        }
    1.44 +        hourOffset = totalOffset / 3600;
    1.45 +        minOffset = (totalOffset % 3600) / 60;
    1.46 +        printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
    1.47 +    }
    1.48 +
    1.49 +    /* Print year */
    1.50 +    printf("%d", et->tm_year);
    1.51 +}
    1.52 +
    1.53 +int main(int argc, char** argv)
    1.54 +{
    1.55 +    PR_STDIO_INIT();
    1.56 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
    1.57 +
    1.58 + 
    1.59 +   /*
    1.60 +     *************************************************************
    1.61 +     **
    1.62 +     **  Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime
    1.63 +     **  on the current time
    1.64 +     **
    1.65 +     *************************************************************
    1.66 +     */
    1.67 +
    1.68 +    {
    1.69 +	PRTime t1, t2;
    1.70 +	PRExplodedTime et;
    1.71 +
    1.72 +	printf("*********************************************\n");
    1.73 +	printf("**                                         **\n");
    1.74 +        printf("** Testing PR_Now(), PR_ExplodeTime, and   **\n");
    1.75 +	printf("** PR_ImplodeTime on the current time      **\n");
    1.76 +	printf("**                                         **\n");
    1.77 +	printf("*********************************************\n\n");
    1.78 +        t1 = PR_Now();
    1.79 +
    1.80 +        /* First try converting to UTC */
    1.81 +
    1.82 +        PR_ExplodeTime(t1, PR_GMTParameters, &et);
    1.83 +        if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) {
    1.84 +	    printf("ERROR: UTC has nonzero gmt or dst offset.\n");
    1.85 +	    return 1;
    1.86 +        }
    1.87 +        printf("Current UTC is ");
    1.88 +	printExplodedTime(&et);
    1.89 +	printf("\n");
    1.90 +
    1.91 +        t2 = PR_ImplodeTime(&et);
    1.92 +        if (LL_NE(t1, t2)) {
    1.93 +	    printf("ERROR: Explode and implode are NOT inverse.\n");
    1.94 +	    return 1;
    1.95 +        }
    1.96 +
    1.97 +        /* Next, try converting to local (US Pacific) time */
    1.98 +
    1.99 +        PR_ExplodeTime(t1, PR_LocalTimeParameters, &et);
   1.100 +        printf("Current local time is ");
   1.101 +	printExplodedTime(&et);
   1.102 +	printf("\n");
   1.103 +	printf("GMT offset is %ld, DST offset is %ld\n",
   1.104 +		et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset);
   1.105 +        t2 = PR_ImplodeTime(&et);
   1.106 +        if (LL_NE(t1, t2)) {
   1.107 +	    printf("ERROR: Explode and implode are NOT inverse.\n");
   1.108 +	    return 1;
   1.109 +	}
   1.110 +    }
   1.111 +
   1.112 +    printf("Please examine the results\n");
   1.113 +    return 0;
   1.114 +}

mercurial