00001
00002
00003 #include "PSTime/Time.h"
00004
00005 #include <stdio.h>
00006 #include <time.h>
00007
00008 #include <iostream>
00009 #include <string>
00010
00011 using namespace std;
00012 using namespace PSTime;
00013
00014 void print_struct_tm(struct tm * timeinfo)
00015 {
00016 printf ( "\nasctime (timeinfo) : %s", asctime(timeinfo));
00017 printf ( "\nTest timeinfo->tm_sec, timeinfo->tm_min, ...:\n");
00018
00019 printf ( "sec = %2d mday = %9d year = %9d\n", timeinfo->tm_sec, timeinfo->tm_mday, timeinfo->tm_year );
00020 printf ( "min = %2d mon = %9d wday = %9d\n", timeinfo->tm_min, timeinfo->tm_mon, timeinfo->tm_wday );
00021 printf ( "hour = %2d isdst= %9d yday = %9d\n", timeinfo->tm_hour, timeinfo->tm_isdst, timeinfo->tm_yday );
00022 }
00023
00024
00025 int main ()
00026 {
00027
00028 char strLine[]="----------------------------------";
00029 printf ("\n\nTest various standard time methods\n");
00030
00031 time_t seconds;
00032 seconds = time(NULL);
00033 printf ("\n\n\nTest seconds = time(NULL);\n %ld UTC seconds since January 1, 1970", seconds);
00034
00035 printf ("\n\n\nTest struct tm *timeinfo = localtime ( &seconds );\n");
00036 struct tm * timeinfo;
00037 timeinfo = localtime ( &seconds );
00038
00039 printf ("\nTest timeinfo->tm_sec, timeinfo->tm_min, ...:\n");
00040 printf ( "sec = %d\n", timeinfo->tm_sec );
00041 printf ( "min = %d\n", timeinfo->tm_min );
00042 printf ( "hour = %d\n", timeinfo->tm_hour );
00043 printf ( "mday = %d\n", timeinfo->tm_mday );
00044 printf ( "mon = %d\n", timeinfo->tm_mon );
00045 printf ( "year = %d\n", timeinfo->tm_year );
00046 printf ( "wday = %d\n", timeinfo->tm_wday );
00047 printf ( "yday = %d\n", timeinfo->tm_yday );
00048 printf ( "isdst= %d\n", timeinfo->tm_isdst);
00049 printf ( "\n asctime (timeinfo) : %s", asctime (timeinfo) );
00050 printf ( " or ctime (&seconds) : %s\n", ctime (&seconds) );
00051
00052 char str[256];
00053 size_t maxsize = 255;
00054 int len = strftime(str, maxsize, "Allows to constract a local time stamp though w/o nsec :\n %Y-%m-%d %H:%M:%S %Z", timeinfo);
00055 printf ( "strftime(...) gives string : %s of length = %d\n",str,len);
00056
00057
00058
00059 time_t local=time(NULL);
00060 tm locTime=*localtime(&local);
00061 tm utcTime=*gmtime(&local);
00062 time_t utc=(mktime(&utcTime));
00063 double diff=difftime(utc,local)/3600;
00064 printf ( "curr(s) = %d, utc(s) = %d, diff(h) = %f\n", (int)local, (int)utc, diff);
00065 printf ( " locTime.tm_hour = %d\n", locTime.tm_hour);
00066 printf ( " utcTime.tm_hour = %d\n", utcTime.tm_hour);
00067
00068
00069
00070 struct timespec currentHRTime;
00071 int gettimeStatus = clock_gettime( CLOCK_REALTIME, ¤tHRTime );
00072 printf ( "\n\n\nTest gettimeStatus = clock_gettime( CLOCK_REALTIME, ¤tHRTime ); \n gettimeHRStatus = %d currentHRTime.tv_sec = %ld .tv_nsec = %ld\n", gettimeStatus, currentHRTime.tv_sec, currentHRTime.tv_nsec );
00073
00074
00075
00076 printf ("%s\n",strLine);
00077 seconds = time(NULL);
00078 printf ("\nTest seconds = time(NULL);\n %ld UTC seconds since January 1, 1970", seconds);
00079
00080 timeinfo = localtime ( &seconds );
00081
00082 print_struct_tm(timeinfo);
00083
00084
00085 printf ("\n\nTest strptime(...)\n");
00086 printf ("%s\n",strLine);
00087
00088 struct tm tm_human;
00089 struct tm tm_basic;
00090
00091 char date_human[]="2010-10-25";
00092 char date_basic[]="20101025";
00093
00094 char time_human[]="17:24:51";
00095 char time_basic[]="172451";
00096
00097 if(strptime(time_human,"%H:%M:%S",&tm_human) == NULL) printf("\nstrptime failed\n");
00098 else print_struct_tm(&tm_human);
00099
00100 if(strptime(time_basic,"%H%M%S",&tm_basic) == NULL) printf("\nstrptime failed\n");
00101 else print_struct_tm(&tm_basic);
00102
00103 if(strptime(date_basic,"%Y%m%d",&tm_basic) == NULL) printf("\nstrptime failed\n");
00104 else print_struct_tm(&tm_basic);
00105
00106 if(strptime(date_human,"%Y-%m-%d",&tm_human) == NULL) printf("\nstrptime failed\n");
00107 else print_struct_tm(&tm_basic);
00108
00109 string str_test="ABCZ";
00110 printf ( "\n\n\nTest string.find %d;\n",(int)str_test.find('Z'));
00111
00112 }