获取Windows Server 2003的时间

/* GNU s_time is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. Function: Read Date and Time from Server or local machine. Author: Tody Kwok Built Date: 2010/01/14 Update Copyritht by Tody 2010 */ #include <stdio.h> #include <time.h> #include <string.h> #include <windows.h> #include <assert.h> #include <lm.h> #include <getopt.h> #ifndef UNICODE #define UNICODE #endif char *assicToUnicode(char *unicode, const char *assic) { char *temp = unicode; assert((assic!=NULL && unicode!=NULL)); while( (*temp++=*assic++)!=0 ) *temp++='/0'; *temp='/0'; return unicode; } time_t remote_server_time( LPWSTR pszServerName1 , int flag) { LPTIME_OF_DAY_INFO pBuf = NULL; NET_API_STATUS nStatus; time_t svr_time1 = 0; time_t svr_time2 = 0; struct tm *o; nStatus = NetRemoteTOD(pszServerName1,(LPBYTE *)&pBuf); /* Windows API: Net Remote Time of Day */ if (nStatus == NERR_Success) { if (pBuf != NULL) { svr_time1 = pBuf->tod_elapsedt; svr_time2 = pBuf->tod_elapsedt - pBuf->tod_timezone*60; /* Make time with time zone information*/ o = gmtime(&svr_time2); /* Convert seconds passed to Date and time format */ if ( flag ) fprintf(stderr,"/n :) Current Server Time: %04d-%02d-%02d %02d:%02d:%02d <%u>",o->tm_year+1900, o->tm_mon+1, o->tm_mday, o->tm_hour,o->tm_min, o->tm_sec,svr_time1); if ( flag == 2 ) { printf("/n set s_date=%02d%02d%04d/n set s_date1=%04d-%02d-%02d/n set S_time=%02d:%02d:%02d", o->tm_mon+1, o->tm_mday,o->tm_year+1900,o->tm_year+1900, o->tm_mon+1, o->tm_mday, o->tm_hour,o->tm_min, o->tm_sec); } NetApiBufferFree(pBuf); /* Free */ } } else { fprintf(stderr, "A system error has occurred: %d/n", nStatus); exit(1) ; } return (svr_time1); } time_t sys_local_time(int flag) { time_t t; struct tm *l; t = time(NULL); l = localtime(&t); /* get local time */ if ( flag ) fprintf(stderr,"/n :) Current System Time: %04d-%02d-%02d %02d:%02d:%02d <%u>",l->tm_year+1900, l->tm_mon+1, l->tm_mday, l->tm_hour,l->tm_min, l->tm_sec,t); if ( flag == 2 ) { printf("/n set l_date=%02d%02d%04d/n set l_date1=%04d-%02d-%02d/n set l_time=%02d:%02d:%02d", l->tm_mon+1, l->tm_mday,l->tm_year+1900,l->tm_year+1900, l->tm_mon+1, l->tm_mday, l->tm_hour,l->tm_min, l->tm_sec); } return (t); } void usage() { /* Here is document */ fprintf(stderr,"Obtain date and time utility 1.0.1 (C) Tody 2010/n"); fprintf(stderr,"Usage:/n"); fprintf(stderr," Option:/n"); fprintf(stderr," -h:/t Help/n"); fprintf(stderr," -H <host>:/t Specifly Host Name (Must)/n"); fprintf(stderr," -s:/t Fetching Server Time, with option -H/n"); fprintf(stderr," -l:/t Show Local Date and Time/n"); fprintf(stderr," -f:/t Don't show the Date and Time/n"); fprintf(stderr," -v:/t For Set Variable settings/n"); fprintf(stderr," -c <sec>:/t Check time different between Server and Local, with option -H/n"); fprintf(stderr,"Return Code:/n"); fprintf(stderr," OK(0): Err(1)/n"); fprintf(stderr,"Example:/n"); fprintf(stderr," 1, s_time.exe -H ShareServer -s/n"); fprintf(stderr," 2, s_time.exe -H ShareServer -s -v/n"); fprintf(stderr," 3, s_time.exe -H ShareServer -c 20/n"); fprintf(stderr," 4, s_time.exe -H ShareServer -s -v > setdate.bat/n"); fprintf(stderr," 5, s_time.exe -l/n"); exit(1); } int main(int argc, char *argv[]) { char *hostname = NULL, buf[256]={NULL}; int sec = 0; int retval = 1, isSetL = 0, isSetS = 0; int ch = 0, flag = 1; while( -1 != ch ) { /* phrase the parameters*/ ch = getopt(argc,argv,"hH:C:c:SslLfFvV"); switch(ch) { case 'h': usage(); case 'H': hostname = optarg; break; case 'C': case 'c': sec = atoi(optarg); break; case 'S': case 's': isSetS = 1; break; case 'l': case 'L': isSetL = 1; break; case 'f': case 'F': flag = 0; break; case 'V': case 'v': flag = 2; break; case '?': usage(); } } if ( argc ==1 || (argc == 3 && hostname != NULL) ) usage(); /* Code here show the result...*/ if ( isSetS && sec == 0){ if ( hostname != NULL ) { fprintf(stderr," Reading Server < %s > time... ",hostname); remote_server_time((WCHAR*)assicToUnicode(buf, hostname), flag); /* function invoke*/ retval = 0; } else usage(); } if ( isSetL ){ sys_local_time(flag); /* Show local time*/ retval = 0; } if ( sec != 0 && hostname != NULL ) { fprintf(stderr," Reading Server < %s > time... ",hostname); ch = abs(remote_server_time((WCHAR*)assicToUnicode(buf, hostname), flag) - sys_local_time(flag)); fprintf(stderr,"/n Different time: %ds",ch); if ( ch > sec ) /* Comparing the time difference */ { retval = 1; } } fprintf(stderr,"/n Return Code = %d/n",retval); /* Error Code showing...*/ return retval; }

你可能感兴趣的:(获取Windows Server 2003的时间)