[pure-lang-svn] SF.net SVN: pure-lang: [405] pure/trunk
Status: Beta
Brought to you by:
agraef
From: <ag...@us...> - 2008-07-06 23:57:54
|
Revision: 405 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=405&view=rev Author: agraef Date: 2008-07-06 16:58:01 -0700 (Sun, 06 Jul 2008) Log Message: ----------- Added strftime to system.pure. Modified Paths: -------------- pure/trunk/lib/system.pure pure/trunk/runtime.cc pure/trunk/runtime.h Modified: pure/trunk/lib/system.pure =================================================================== --- pure/trunk/lib/system.pure 2008-07-06 22:49:06 UTC (rev 404) +++ pure/trunk/lib/system.pure 2008-07-06 23:58:01 UTC (rev 405) @@ -49,14 +49,19 @@ /* Time functions. 'time' reports the current time in seconds since the "epoch" a.k.a. 00:00:00 UTC, Jan 1 1970. The result is always a bigint (in - fact, the time value is already 64 bit on many OSes nowadays). The ctime - and gmtime functions convert a time value to a string in either local time - or UTC. (Note that the latter is actually a combination of the C gmtime() - and asctime() functions.) */ + fact, the time value is already 64 bit on many OSes nowadays). */ extern long pure_time() = time; + +/* Functions to format a time value as a string. The ctime and gmtime + functions convert a time value to a string in either local time or UTC. + The strftime function also formats a time value as local time, using a + format specification supplied by the user. See ctime(3), gmtime(3) and + strftime(3) for details. */ + extern char* pure_ctime(long) = ctime; extern char* pure_gmtime(long) = gmtime; +extern char* pure_strftime(char* format, long t) = strftime; /* The gettimeofday function also returns wallclock time as seconds since the epoch, but theoretically offers resolutions in the microsec range (actual Modified: pure/trunk/runtime.cc =================================================================== --- pure/trunk/runtime.cc 2008-07-06 22:49:06 UTC (rev 404) +++ pure/trunk/runtime.cc 2008-07-06 23:58:01 UTC (rev 405) @@ -2349,6 +2349,9 @@ return (int64_t)time(NULL); } +/* Note that the following are not thread-safe as they use statically + allocated buffers. */ + extern "C" char *pure_ctime(int64_t t) { @@ -2363,6 +2366,20 @@ return asctime(gmtime(&time)); } +extern "C" +char *pure_strftime(const char *format, int64_t t) +{ + time_t time = (time_t)t; + static char buf[1024]; + if (!strftime(buf, 1024, format, localtime(&time))) + /* The interface to strftime is rather brain-damaged since it returns zero + both in case of a buffer overflow and when the resulting string is + empty. We just pretend that there cannot be any errors and return an + empty string in both cases. */ + buf[0] = 0; + return buf; +} + #ifdef HAVE_GETTIMEOFDAY #include <sys/time.h> extern "C" Modified: pure/trunk/runtime.h =================================================================== --- pure/trunk/runtime.h 2008-07-06 22:49:06 UTC (rev 404) +++ pure/trunk/runtime.h 2008-07-06 23:58:01 UTC (rev 405) @@ -550,11 +550,16 @@ platform incompatibilities. The result is always int64_t, as time_t nowadays is a 64 bit type on many OSes. We also provide wrappers for ctime() and gmtime() which convert a time value to a string, using either - local or UTC time. */ + the local timezone or UTC. */ int64_t pure_time(void); + +/* The following routines allow you to convert a time value to a string, using + different formats. See ctime(3), gmtime(3) and strftime(3) for details. */ + char *pure_ctime(int64_t t); char *pure_gmtime(int64_t t); +char *pure_strftime(const char *format, int64_t t); /* gettimeofday() interface. This may actually be implemented using different system functions, depending on what's available on the host OS. */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |