Can anyone spot why this code only partially works?

Previous topic - Next topic

Hatonastick

Code (glbasic) Select
INLINE
}
typedef long int time_t;
typedef unsigned int size_t;

// A structure for storing all kinds of useful information about the
// current (or another) time.
struct tm
{
// Seconds: 0-59 (K&R says 0-61?)
int tm_sec;
// Minutes: 0-59
int tm_min;
// Hours since midnight: 0-23
int tm_hour;
// Day of the month: 1-31
int tm_mday;
// Months since January: 0-11
int tm_mon;
// Years since 1900
int tm_year;
// Days since Sunday: 0-6
int tm_wday;
// Days since January 1: 0-365
int tm_yday;
// +1 Daylight Savings Time, 0 No DST, -1 Don't know
int tm_isdst;
};

// ANSI C time functions.  Using these might break GLBMUD when compiled for
// certain platforms.  Hopefully it will be fine under Win32, Linux and
// MacOS X.
extern "C" time_t time( time_t *time );
extern "C" size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp);
extern "C" struct tm *localtime( const time_t *time );

namespace __GLBASIC__ {
ENDINLINE

// Get the system time in the form of a formatted string, using the ANSI C time
// function strftime().
FUNCTION GetTime: fstring$, size%
LOCAL tstring$
INLINE
struct tm *ltime = NULL;
time_t t = 0;

// Get local time.
t = time(0);
ltime = localtime(&t);

// Allocate space for our string to the length given in the size variable.
tstring_Str.Alloc(size);

// Get the time in a formatted string.
strftime(tstring_Str.GetStrData(), size, fstring_Str.c_str(), ltime);

// Force GLBasic to recalculate length of the string.
tstring_Str.CalcLen();
ENDINLINE
RETURN tstring$
ENDFUNCTION


If you test it like this:
Code (glbasic) Select
LOCAL date$
LOCAL time$

// Grab current time and date.
date$ = GetTime("%Y%m%d", 10)
time$ = GetTime("%I:%M %p", 10)


date$ does seem to work and should contain a string of the year, month and day eg. '20090307', however time$ contains only the hour. eg. instead of getting '3:33 PM' I get just '3'.

Over all this is pretty nasty, and I should probably do something like recreate strftime() (as best as I can) in GLB using the output from PLATFORMINFO$("TIME") without resorting to C calls and INLINE.

MrTAToad

CalcLen seems to cause it - if you DEBUG the tstring variable before that, you get the correct time.  However, even with that removed, you still get just the hours.

Doh!  GetTime should bet GetTime$

Code (glbasic) Select

date$ = GetTime$("%Y%m%d", 10)
time$ = GetTime$("%I:%M %p", 10)
DEBUG date$+"\n"+time$+"\n"

FUNCTION GetTime$: fstring$, size%
...
ENDFUNCTION


to give :

Code (glbasic) Select

20090306
06:31 PM

Hatonastick

Whoops!  :whistle:  Thanks for pointing that out.  Explains why only numbers are returned then. :)