我用     char time_str[50];
_strtime(time_str);
取的系统时间字符串,通过串口发送过去.
但是不知道怎么通过这个字符串来设置系统时间.是否还有其他方法,

解决方案 »

  1.   

    SetSystemTime
    The SetSystemTime function sets the current system time and date. The system time is expressed in Coordinated Universal Time (UTC). BOOL SetSystemTime(
      CONST SYSTEMTIME *lpSystemTime   // system time
    );
    Parameters
    lpSystemTime 
    [in] Pointer to a SYSTEMTIME structure that contains the current system date and time. 
    The wDayOfWeek member of the SYSTEMTIME structure is ignored. Return Values
    If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. 
      

  2.   

    BOOL
    WINAPI
    SetLocalTime(
        CONST SYSTEMTIME *lpSystemTime
        );
      

  3.   

    typedef struct _SYSTEMTIME { 
        WORD wYear; 
        WORD wMonth; 
        WORD wDayOfWeek; 
        WORD wDay; 
        WORD wHour; 
        WORD wMinute; 
        WORD wSecond; 
        WORD wMilliseconds; 
    } SYSTEMTIME, *PSYSTEMTIME; 
    Members
    wYear 
    Specifies the current year. The year must be greater than 1601. 
    Windows XP: The year cannot be greater than 30827. wMonth 
    Specifies the current month; January = 1, February = 2, and so on. 
    wDayOfWeek 
    Specifies the current day of the week; Sunday = 0, Monday = 1, and so on. 
    wDay 
    Specifies the current day of the month. 
    wHour 
    Specifies the current hour. 
    wMinute 
    Specifies the current minute. 
    wSecond 
    Specifies the current second. 
    wMilliseconds 
    Specifies the current millisecond. 
      

  4.   

    // SetNewTime - sets system time
    // Return value - TRUE if successful, FALSE otherwise
    // hour     - new hour (0-23)
    // minutes  - new minutes (0-59)BOOL SetNewTime(WORD hour, WORD minutes)
    {
        SYSTEMTIME st;
        char *pc;    GetSystemTime(&st);       // gets current time
        st.wHour = hour;          // adjusts hours 
        st.wMinute = minutes;     // and minutes
        if (!SetSystemTime(&st))  // sets system time
            return FALSE;
        return TRUE;
    }
      

  5.   

    下面提供一个完整的例子// time zone test#include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <time.h>
    #include <sys\timeb.h>
    #include <windows.h>
    #include <process.h>
    #include <errno.h>
    #include <process.h>
    #define BIAS1 ( *((DWORD*)0x7FFe0020) )
    #define BIAS2 ( *((DWORD*)0x7FFe0024) )CHAR buf[200];   // message bufferVOID FormatSt( SYSTEMTIME st, CHAR* buf)
    {
        sprintf(buf,"%02d/%02d/%02d %02d:%02d:%02d",
            st.wYear, st.wMonth, st.wDay,
            st.wHour, st.wMinute, st.wSecond );
    }VOID PrintTZInfo()
    {
        TIME_ZONE_INFORMATION tzi;
        DWORD dwSta;    dwSta= GetTimeZoneInformation( &tzi );
       
        printf("GetTimeZoneInformation: \n ");
        switch( dwSta )
        {
            case TIME_ZONE_ID_UNKNOWN:
                printf("returned TIME_ZONE_ID_UNKNOWN\n");
                break;        case TIME_ZONE_ID_STANDARD:
                FormatSt( tzi.StandardDate, buf );
                printf("Bias %d  Name: %S  SysDate: %s  Bias: %d\n",
                       tzi.Bias, tzi.StandardName, buf, tzi.StandardBias );
                break;        case TIME_ZONE_ID_DAYLIGHT:
                FormatSt( tzi.DaylightDate, buf );
                printf("Bias %d  Name: %S  SysDate: %s  Bias: %d\n",
                       tzi.Bias, tzi.DaylightName, buf, tzi.DaylightBias );
                break;        default:
                printf("returned undoced status: %d",dwSta);
                break;
        }
        printf(" User_Shared_Data bias: %08x %08x\n\n",BIAS2, BIAS1 );
    }VOID TstSetTime( int year, int mon, int day, int hour, int minute, int sec)
    {
        SYSTEMTIME st,tst;
        BOOL bSta;    st.wYear=  year;
        st.wMonth= mon;
        st.wDay=   day;
        st.wHour=  hour;
        st.wMinute= minute;
        st.wSecond= sec;    st.wDayOfWeek= 0;
        st.wMilliseconds= 0;    bSta= SetLocalTime( &st );    if( bSta == FALSE )
        {
            FormatSt( st, buf);
            printf("Failed to set date/time: %s\n",buf);
        }
        else
        {
            FormatSt( st, buf);
            printf("SetLocalTime:  %s\n",buf);        GetLocalTime( &tst );
            FormatSt( tst, buf);
            printf("GetLocalTime:  %s\n", buf);        GetSystemTime( &tst );
            FormatSt( tst, buf );
            printf("GetSystemTime: %s\n", buf);
        }
        printf("\n");}VOID PrintTime( CHAR* msg )
    {
        SYSTEMTIME st;    GetLocalTime( &st );    FormatSt( st, (CHAR*) buf );    printf("%s %s\n", msg, buf);}int _cdecl  main(int argc, char** argv)
    {    // pick date in savings time    TstSetTime( 1998, 8, 30, 22, 59, 0 );
        PrintTZInfo();    // pick date outside of savings time    printf("\n");
        TstSetTime( 1998, 12, 29, 22, 59, 0 );
        PrintTZInfo();    return(0);
    }
      

  6.   

    TstSetTime( 1998, 12, 29, 22, 59, 0 );设置时间