user-0 提问: 如何给time_t变量赋一个固定值

回复:
user: user-1
content: mktime Converts the local time to a calendar value.  time_t mktime( struct tm *timeptr );  Routine Required Header Compatibility  mktime <time.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  mktime returns the specified calendar time encoded as a value of type time_t. If timeptr references a date before midnight, January 1, 1970, or if the calendar time cannot be represented, the function returns –1 cast to type time_t.  Parameter  timeptr  Pointer to time structure  Remarks  The mktime function converts the supplied time structure (possibly incomplete) pointed to by timeptr into a fully defined structure with normalized values and then converts it to a time_t calendar time value. For description of tm structure fields, see asctime. The converted time has the same encoding as the values returned by the time function. The original values of the tm_wday and tm_yday components of the timeptr structure are ignored, and the original values of the other components are not restricted to their normal ranges.  mktime handles dates in any time zone from midnight, January 1, 1970, to January 18, 19:14:07, 2038. If successful, mktime sets the values of tm_wday and tm_yday as appropriate and sets the other components to represent the specified calendar time, but with their values forced to the normal ranges; the final value of tm_mday is not set until tm_mon and tm_year are determined. When specifying a tm structure time, set the tm_isdst field to 0 to indicate that standard time is in effect, or to a value greater than 0 to indicate that daylight savings time is in effect, or to a value less than zero to have the C run-time library code compute whether standard time or daylight savings time is in effect. (The C run-time library assumes the United States’s rules for implementing the calculation of Daylight Saving Time). tm_isdst is a required field. If not set, its value is undefined and the return value from mktime is unpredictable. If timeptr points to a tm structure returned by a previous call to asctime, gmtime, or localtime, the tm_isdst field contains the correct value.  Note that gmtime and localtime use a single statically allocated buffer for the conversion. If you supply this buffer to mktime, the previous contents are destroyed.  Example  /* MKTIME.C: The example takes a number of days  * as input and returns the time, the current  * date, and the specified number of days.  */  #include <time.h> #include <stdio.h>  void main( void ) {    struct tm when;    time_t now, result;    int    days;     time( &now );    when = *localtime( &now );    printf( "Current time is %s\n", asctime( &when ) );    printf( "How many days to look ahead: " );    scanf( "%d", &days );     when.tm_mday = when.tm_mday + days;    if( (result = mktime( &when )) != (time_t)-1 )       printf( "In %d days the time will be %s\n",                days, asctime( &when ) );    else       perror( "mktime failed" ); }   Output  Current time is Tue May 03 12:45:47 1994  How many days to look ahead: 29 In 29 days the time will be Wed Jun 01 12:45:47 1994   Time Management Routines  See Also   asctime, gmtime, localtime, time localtime Converts a time value and corrects for the local time zone.  struct tm *localtime( const time_t *timer );  Routine Required Header Compatibility  localtime <time.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  localtime returns a pointer to the structure result. If the value in timer represents a date before midnight, January 1, 1970, localtime returns NULL. The fields of the structure type tm store the following values, each of which is an int:  tm_sec  Seconds after minute (0 – 59)  tm_min  Minutes after hour (0 – 59)  tm_hour  Hours after midnight (0 – 23)  tm_mday  Day of month (1 – 31)  tm_mon  Month (0 – 11; January = 0)  tm_year  Year (current year minus 1900)  tm_wday  Day of week (0 – 6; Sunday = 0)  tm_yday  Day of year (0 – 365; January 1 = 0)  tm_isdst  Positive value if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative value if status of daylight saving time is unknown. The C run-time library assumes the United States’s rules for implementing the calculation of Daylight Saving Time (DST).   Parameter  timer  Pointer to stored time  Remarks  The localtime function converts a time stored as a time_t value and stores the result in a structure of type tm. The long value timer represents the seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). This value is usually obtained from the time function.  gmtime, mktime, and localtime all use a single statically allocated tm structure for the conversion. Each call to one of these routines destroys the result of the previous call.  localtime corrects for the local time zone if the user first sets the global environment variable TZ. When TZ is set, three other environment variables (_timezone, _daylight, and _tzname) are automatically set as well. See _tzset for a description of these variables. TZ is a Microsoft extension and not part of the ANSI standard definition of localtime.  Note   The target environment should try to determine whether daylight saving time is in effect.  Example  /* LOCALTIM.C: This program uses time to get the current time   * and then uses localtime to convert this time to a structure   * representing the local time. The program converts the result   * from a 24-hour clock to a 12-hour clock and determines the   * proper extension (AM or PM).  */  #include <stdio.h> #include <string.h> #include <time.h>  void main( void ) {         struct tm *newtime;         char am_pm[] = "AM";         time_t long_time;          time( &long_time );                /* Get time as long integer. */         newtime = localtime( &long_time ); /* Convert to local time. */          if( newtime->tm_hour > 12 )        /* Set up extension. */                 strcpy( am_pm, "PM" );         if( newtime->tm_hour > 12 )        /* Convert from 24-hour */                 newtime->tm_hour -= 12;    /*   to 12-hour clock.  */         if( newtime->tm_hour == 0 )        /*Set hour to 12 if midnight. */                 newtime->tm_hour = 12;          printf( "%.19s %s\n", asctime( newtime ), am_pm ); }   Output  Tue Mar 23 11:28:17 AM   Time Management Routines  See Also   asctime, ctime, _ftime, gmtime, time, _tzset

user: user-2
content: tm tm_;     int year, month, day, hour, minute,second;     tm_.tm_year  = 2013-1900;     tm_.tm_mon   = 9-1;     tm_.tm_mday  = 1;     tm_.tm_hour  = 0;     tm_.tm_min   = 0;     tm_.tm_sec   = 0;     tm_.tm_isdst = 0;  xtm = localtime( &tm);