user-0 提问: C语言怎样打印出当前的时间呢？

回复:
user: user-1
content: 引用 13 楼 ForestDB 的回复:system("date"); 时间呢？ system("@echo %date% %time%");

user: user-2
content: #include <stdio.h> #include <time.h> #include <unistd.h> int main (void ) {     time_t Curtime;     tm sTime = {0};     while(1)     {            time(&Curtime);          tm *RetTime = localtime( &Curtime );         printf("Time: %04d-%02d-%02d %02d:%02d:%02d \r"                 ,RetTime->tm_year + 1900                 ,RetTime->tm_mon + 1                  ,RetTime->tm_mday                 ,RetTime->tm_hour                 ,RetTime->tm_min                 ,RetTime->tm_sec);         sleep(1);         fflush(stdout);     }         return 0; }

user: user-3
content: http://hi.baidu.com/zhulin014/item/89df643c32be54fdde2221efc语言获取/设置时间的相关函数

user: user-4
content: system("date");

user: user-5
content: GetLocalTime

user: user-6
content: #include  <time.h>  #include  <stdio.h>  void main()  {           time_t timep;           time (&timep);           printf("%s",asctime(gmtime(&timep)));  }

user: user-7
content: 引用 3 楼 u012840458 的回复: #include  <time.h> #include  <stdio.h> void main() {      time_t timep;      time (&timep);      printf("%s",asctime(gmtime(&timep)));// printf("%s",ctime(&timep)); } 这个也可以

user: user-7
content: #include  <time.h> #include  <stdio.h> void main() {      time_t timep;      time (&timep);      printf("%s",asctime(gmtime(&timep))); }

user: user-8
content: time_t		tCurTime=time(NULL); 	struct tm*	ptmCurTime=localtime(&tCurTime);	  	printf("%04d%02d%02d %02d%02d%02d\n", 		ptmCurTime->tm_year+1900,ptmCurTime->tm_mon+1,ptmCurTime->tm_mday,ptmCurTime->tm_hour,ptmCurTime->tm_min,ptmCurTime->tm_sec);

user: user-0
content: 格式不做严格要求，只要能打印出日期和时间就行。

user: user-1
content: 接上帖 If a conversion specification does not correspond to any of the above, the behavior is undefined.  If a struct tm broken-down time structure is created by localtime() or localtime_r(), or modified by mktime(), and the value of TZ is subsequently modified, the results of the %Z and %z strftime() conversion specifiers are undefined, when strftime() is called with such a broken-down time structure.  If a struct tm broken-down time structure is created or modified by gmtime() or gmtime_r(), it is unspecified whether the result of the %Z and %z conversion specifiers shall refer to UTC or the current local timezone, when strftime() is called with such a broken-down time structure.     Modified Conversion Specifiers Some conversion specifiers can be modified by the E or O modifier characters to indicate that an alternative format or specification should be used rather than the one normally used by the unmodified conversion specifier. If the alternative format or specification does not exist for the current locale (see ERA in the Base Definitions volume of IEEE Std 1003.1-2001, Section 7.3.5, LC_TIME), the behavior shall be as if the unmodified conversion specification were used.   %Ec Replaced by the locale's alternative appropriate date and time representation.  %EC Replaced by the name of the base year (period) in the locale's alternative representation.  %Ex Replaced by the locale's alternative date representation.  %EX Replaced by the locale's alternative time representation.  %Ey Replaced by the offset from %EC (year only) in the locale's alternative representation.  %EY Replaced by the full alternative year representation.  %Od Replaced by the day of the month, using the locale's alternative numeric symbols, filled as needed with leading zeros if there is any alternative symbol for zero; otherwise, with leading spaces.  %Oe Replaced by the day of the month, using the locale's alternative numeric symbols, filled as needed with leading spaces.  %OH Replaced by the hour (24-hour clock) using the locale's alternative numeric symbols.  %OI Replaced by the hour (12-hour clock) using the locale's alternative numeric symbols.  %Om Replaced by the month using the locale's alternative numeric symbols.  %OM Replaced by the minutes using the locale's alternative numeric symbols.  %OS Replaced by the seconds using the locale's alternative numeric symbols.  %Ou Replaced by the weekday as a number in the locale's alternative representation (Monday=1).  %OU Replaced by the week number of the year (Sunday as the first day of the week, rules corresponding to %U ) using the locale's alternative numeric symbols.  %OV Replaced by the week number of the year (Monday as the first day of the week, rules corresponding to %V ) using the locale's alternative numeric symbols.  %Ow Replaced by the number of the weekday (Sunday=0) using the locale's alternative numeric symbols.  %OW Replaced by the week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols.  %Oy Replaced by the year (offset from %C ) using the locale's alternative numeric symbols.   %g , %G , and %V give values according to the ISO 8601:2000 standard week-based year. In this system, weeks begin on a Monday and week 1 of the year is the week that includes January 4th, which is also the week that includes the first Thursday of the year, and is also the first week that contains at least four days in the year. If the first Monday of January is the 2nd, 3rd, or 4th, the preceding days are part of the last week of the preceding year; thus, for Saturday 2nd January 1999, %G is replaced by 1998 and %V is replaced by 53. If December 29th, 30th, or 31st is a Monday, it and any following days are part of week 1 of the following year. Thus, for Tuesday 30th December 1997, %G is replaced by 1998 and %V is replaced by 01.   If a conversion specifier is not one of the above, the behavior is undefined.     RETURN VALUE If the total number of resulting bytes including the terminating null byte is not more than maxsize, strftime() shall return the number of bytes placed into the array pointed to by s, not including the terminating null byte. Otherwise, 0 shall be returned and the contents of the array are unspecified.     ERRORS No errors are defined.   The following sections are informative.     EXAMPLES    Getting a Localized Date String The following example first sets the locale to the user's default. The locale information will be used in the nl_langinfo() and strftime() functions. The nl_langinfo() function returns the localized date string which specifies how the date is laid out. The strftime() function takes this information and, using the tm structure for values, places the date and time information into datestring.     #include <time.h> #include <locale.h> #include <langinfo.h> ... struct tm *tm; char datestring[256]; ... setlocale (LC_ALL, ""); ... strftime (datestring, sizeof(datestring), nl_langinfo (D_T_FMT), tm); ...      APPLICATION USAGE The range of values for %S is [00,60] rather than [00,59] to allow for the occasional leap second.   Some of the conversion specifications are duplicates of others. They are included for compatibility with nl_cxtime() and nl_ascxtime(), which were published in Issue 2.   Applications should use %Y (4-digit years) in preference to %y (2-digit years).   In the C locale, the E and O modifiers are ignored and the replacement strings for the following specifiers are:   %a The first three characters of %A .  %A One of Sunday, Monday, ..., Saturday.  %b The first three characters of %B .  %B One of January, February, ..., December.  %c Equivalent to %a %b %e %T %Y .  %p One of AM or PM.  %r Equivalent to %I : %M : %S %p .  %x Equivalent to %m / %d / %y .  %X Equivalent to %T .  %Z Implementation-defined.      RATIONALE None.     FUTURE DIRECTIONS None.     SEE ALSO asctime() , clock() , ctime() , difftime() , getdate() , gmtime() , localtime() , mktime() , strptime() , time() , tzset() , utime() , Base Definitions volume of IEEE Std 1003.1-2001, Section 7.3.5, LC_TIME, <time.h>     COPYRIGHT Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .  --------------------------------------------------------------------------------

user: user-1
content: STRFTIME Section: POSIX Programmer's Manual (P) Updated: 2003  --------------------------------------------------------------------------------    NAME strftime - convert date and time to a string    SYNOPSIS #include <time.h>    size_t strftime(char *restrict s, size_t maxsize,         const char *restrict format, const struct tm *restrict timeptr);      DESCRIPTION The strftime() function shall place bytes into the array pointed to by s as controlled by the string pointed to by format. The format is a character string, beginning and ending in its initial shift state, if any. The format string consists of zero or more conversion specifications and ordinary characters. A conversion specification consists of a '%' character, possibly followed by an E or O modifier, and a terminating conversion specifier character that determines the conversion specification's behavior. All ordinary characters (including the terminating null byte) are copied unchanged into the array. If copying takes place between objects that overlap, the behavior is undefined. No more than maxsize bytes are placed into the array. Each conversion specifier is replaced by appropriate characters as described in the following list. The appropriate characters are determined using the LC_TIME category of the current locale and by the values of zero or more members of the broken-down time structure pointed to by timeptr, as specified in brackets in the description. If any of the specified values are outside the normal range, the characters stored are unspecified.   Local timezone information is used as though strftime() called tzset().   The following conversion specifications are supported:   %a Replaced by the locale's abbreviated weekday name. [ tm_wday]  %A Replaced by the locale's full weekday name. [ tm_wday]  %b Replaced by the locale's abbreviated month name. [ tm_mon]  %B Replaced by the locale's full month name. [ tm_mon]  %c Replaced by the locale's appropriate date and time representation. (See the Base Definitions volume of IEEE Std 1003.1-2001, <time.h>.)  %C Replaced by the year divided by 100 and truncated to an integer, as a decimal number [00,99]. [ tm_year]  %d Replaced by the day of the month as a decimal number [01,31]. [ tm_mday]  %D Equivalent to %m / %d / %y . [ tm_mon, tm_mday, tm_year]  %e Replaced by the day of the month as a decimal number [1,31]; a single digit is preceded by a space. [ tm_mday]  %F Equivalent to %Y - %m - %d (the ISO 8601:2000 standard date format). [ tm_year, tm_mon, tm_mday]  %g Replaced by the last 2 digits of the week-based year (see below) as a decimal number [00,99]. [ tm_year, tm_wday, tm_yday]  %G Replaced by the week-based year (see below) as a decimal number (for example, 1977). [ tm_year, tm_wday, tm_yday]  %h Equivalent to %b . [ tm_mon]  %H Replaced by the hour (24-hour clock) as a decimal number [00,23]. [ tm_hour]  %I Replaced by the hour (12-hour clock) as a decimal number [01,12]. [ tm_hour]  %j Replaced by the day of the year as a decimal number [001,366]. [ tm_yday]  %m Replaced by the month as a decimal number [01,12]. [ tm_mon]  %M Replaced by the minute as a decimal number [00,59]. [ tm_min]  %n Replaced by a <newline>. %p Replaced by the locale's equivalent of either a.m. or p.m. [ tm_hour]  %r Replaced by the time in a.m. and p.m. notation;  in the POSIX locale this shall be equivalent to %I : %M : %S %p . [ tm_hour, tm_min, tm_sec]  %R Replaced by the time in 24-hour notation ( %H : %M ). [ tm_hour, tm_min]  %S Replaced by the second as a decimal number [00,60]. [ tm_sec]  %t Replaced by a <tab>.  %T Replaced by the time ( %H : %M : %S ). [ tm_hour, tm_min, tm_sec]  %u Replaced by the weekday as a decimal number [1,7], with 1 representing Monday. [ tm_wday]  %U Replaced by the week number of the year as a decimal number [00,53]. The first Sunday of January is the first day of week 1; days in the new year before this are in week 0. [ tm_year, tm_wday, tm_yday]  %V Replaced by the week number of the year (Monday as the first day of the week) as a decimal number [01,53]. If the week containing 1 January has four or more days in the new year, then it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1. Both January 4th and the first Thursday of January are always in week 1. [ tm_year, tm_wday, tm_yday]  %w Replaced by the weekday as a decimal number [0,6], with 0 representing Sunday. [ tm_wday]  %W Replaced by the week number of the year as a decimal number [00,53]. The first Monday of January is the first day of week 1; days in the new year before this are in week 0. [ tm_year, tm_wday, tm_yday] %x Replaced by the locale's appropriate date representation. (See the Base Definitions volume of IEEE Std 1003.1-2001, <time.h>.) %X Replaced by the locale's appropriate time representation. (See the Base Definitions volume of IEEE Std 1003.1-2001, <time.h>.) %y Replaced by the last two digits of the year as a decimal number [00,99]. [ tm_year] %Y Replaced by the year as a decimal number (for example, 1997). [ tm_year] %z Replaced by the offset from UTC in the ISO 8601:2000 standard format ( +hhmm or -hhmm ), or by no characters if no timezone is determinable. For example, "-0430" means 4 hours 30 minutes behind UTC (west of Greenwich).  If tm_isdst is zero, the standard time offset is used. If tm_isdst is greater than zero, the daylight savings time offset is used. If tm_isdst is negative, no characters are returned. [ tm_isdst] %Z Replaced by the timezone name or abbreviation, or by no bytes if no timezone information exists. [ tm_isdst] %% Replaced by % .  未完待续

user: user-1
content: #include <stdio.h> #include <time.h> int main() {     time_t tt;     char tmpbuf[80];      tt=time(NULL);     strftime(tmpbuf,80,"%Y-%m-%d %H:%M:%S\n",localtime(&tt));     printf(tmpbuf);     return 0; }  strftime, wcsftime Format a time string.  size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );  size_t wcsftime( wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr );  Routine Required Header Compatibility  strftime <time.h> ANSI, Win 95, Win NT  wcsftime <time.h> or <wchar.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  strftime returns the number of characters placed in strDest if the total number of resulting characters, including the terminating null, is not more than maxsize. wcsftime returns the corresponding number of wide characters. Otherwise, the functions return 0, and the contents of strDest is indeterminate.  Parameters  strDest  Output string  maxsize  Maximum length of string  format  Format-control string  timeptr  tm data structure  Remarks  The strftime and wcsftime functions format the tm time value in timeptr according to the supplied format argument and store the result in the buffer strDest. At most, maxsize characters are placed in the string. For a description of the fields in the timeptr structure, see asctime. wcsftime is the wide-character equivalent of strftime; its string-pointer argument points to a wide-character string. These functions behave identically otherwise.  Note   Prior to this version of Visual C++, the documentation described the format parameter of wcsftime as having the datatype const wchar_t *, but the actual implementation of the format datatype was const char *. In this version, the implementation of the format datatype has been updated to reflect the previous and current documentation, that is: const wchar_t *.  Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _tcsftime strftime  strftime wcsftime    The format argument consists of one or more codes; as in printf, the formatting codes are preceded by a percent sign (%). Characters that do not begin with % are copied unchanged to strDest. The LC_TIME category of the current locale affects the output formatting of strftime.(For more information on LC_TIME, see setlocale.) The formatting codes for strftime are listed below:  %a  Abbreviated weekday name  %A  Full weekday name  %b  Abbreviated month name  %B  Full month name  %c  Date and time representation appropriate for locale  %d  Day of month as decimal number (01 – 31)  %H  Hour in 24-hour format (00 – 23)  %I  Hour in 12-hour format (01 – 12)  %j  Day of year as decimal number (001 – 366)  %m  Month as decimal number (01 – 12)  %M  Minute as decimal number (00 – 59)  %p  Current locale’s A.M./P.M. indicator for 12-hour clock  %S  Second as decimal number (00 – 59)  %U  Week of year as decimal number, with Sunday as first day of week (00 – 53)  %w  Weekday as decimal number (0 – 6; Sunday is 0)  %W  Week of year as decimal number, with Monday as first day of week (00 – 53)  %x  Date representation for current locale  %X  Time representation for current locale  %y  Year without century, as decimal number (00 – 99)  %Y  Year with century, as decimal number  %z, %Z  Time-zone name or abbreviation; no characters if time zone is unknown  %%  Percent sign  As in the printf function, the # flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows.  Format Code Meaning  %#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% # flag is ignored.  %#c Long date and time representation, appropriate for current locale. For example: “Tuesday, March 14, 1995, 12:41:29”.  %#x Long date representation, appropriate to current locale. For example: “Tuesday, March 14, 1995”.  %#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y Remove leading zeros (if any).    Example  /* TIMES.C illustrates various time and date functions including:  *      time            _ftime          ctime       asctime  *      localtime       gmtime          mktime      _tzset  *      _strtime        _strdate        strftime  *  * Also the global variable:  *      _tzname  */  #include <time.h> #include <stdio.h> #include <sys/types.h> #include <sys/timeb.h> #include <string.h>  void main() {     char tmpbuf[128], ampm[] = "AM";     time_t ltime;     struct _timeb tstruct;     struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };      /* Set time zone from TZ environment variable. If TZ is not set,      * the operating system is queried to obtain the default value       * for the variable.       */     _tzset();      /* Display operating system-style date and time. */     _strtime( tmpbuf );     printf( "OS time:\t\t\t\t%s\n", tmpbuf );     _strdate( tmpbuf );     printf( "OS date:\t\t\t\t%s\n", tmpbuf );      /* Get UNIX-style time and display as number and string. */     time( <ime );     printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );     printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );      /* Display UTC. */     gmt = gmtime( <ime );     printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );      /* Convert to time structure and adjust for PM if necessary. */     today = localtime( <ime );     if( today->tm_hour > 12 )     {    strcpy( ampm, "PM" );    today->tm_hour -= 12;     }     if( today->tm_hour == 0 )  /* Adjust if midnight hour. */    today->tm_hour = 12;      /* Note how pointer addition is used to skip the first 11       * characters and printf is used to trim off terminating       * characters.      */     printf( "12-hour time:\t\t\t\t%.8s %s\n",        asctime( today ) + 11, ampm );      /* Print additional time information. */     _ftime( &tstruct );     printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );     printf( "Zone difference in seconds from UTC:\t%u\n",               tstruct.timezone );     printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );     printf( "Daylight savings:\t\t\t%s\n",               tstruct.dstflag ? "YES" : "NO" );      /* Make time for noon on Christmas, 1993. */     if( mktime( &xmas ) != (time_t)-1 )    printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );      /* Use time structure to build a customized time string. */     today = localtime( <ime );      /* Use strftime to build a customized time string. */     strftime( tmpbuf, 128,          "Today is %A, day %d of %B in the year %Y.\n", today );     printf( tmpbuf ); }    Output  OS time:                                21:51:03 OS date:                                05/03/94 Time in seconds since UTC 1/1/70:       768027063 UNIX time and date:                     Tue May 03 21:51:03 1994 Coordinated universal time:             Wed May 04 04:51:03 1994 12-hour time:                           09:51:03 PM Plus milliseconds:                      279 Zone difference in seconds from UTC:    480 Time zone name:                          Daylight savings:                       YES Christmas                               Sat Dec 25 12:00:00 1993  Today is Tuesday, day 03 of May in the year 1994.   Locale Routines, Time Management Routines |  String Manipulation Routines  See Also   localeconv, setlocale, strcoll, _stricoll, strxfrm

user: user-9
content: linux下直接date命令