View Full Version : Getting system date in integer format
biswajitnanda
08-05-2004, 12:49 PM
Dear all,
Is there any function to get the system date onlyin int format(not system time) in C, which will work in UNIX environment? If anyone has any information, plz reply soon... I m in a great problem. Plz help.
Regards n thanks,
Biswajit...
RobSeace
08-05-2004, 01:50 PM
You'll have to clarify just what you mean by that... What format
would you want this int to be in?? There's no standard int date-only
format that I'm aware of... (And, you said you didn't want time
info, which time() would give you, along with date, in Unix Epoch
seconds format...) But, you could certainly craft your own arbitrarily
formatted ints containing the date in whatever format you like...
Eg: just assuming you want an int that holds the date in a simple
YYYYMMDD format (so that, for instance, today would be stored
as the base-10 int 20040805), then you could generate it like so:
time_t now;
struct tm *ltm;
int y, m, d, fulldate;
time (&now);
ltm = localtime (&now);
y = ltm->year + 1900;
m = ltm->mon + 1;
d = ltm->mday;
fulldate = (y * 10000) + (m * 100) + d;
(If this is in code that needs to be thread-safe, you should probably
use localtime_r() instead of localtime(), if you have such a lib
function, anyway...)
If
fulldate = (y * 10000) + (m * 100) + d;
fulldate should be declared as long.
The program can be presented in a better way:-
time_t now;
struct tm *ltm;
int y, m, d;
char * str, * fulldate;
time (&now);
ltm = localtime (&now);
y = ltm->tm_year + 1900;
m = ltm->tm_mon + 1;
d = ltm->tm_mday;
strcat(strcat(fulldate, itoa(y, str, 10)), "-");
strcat(strcat(fulldate, itoa(m, str, 10)), "-");
strcat(fulldate, itoa(d, str, 10));
Beside that this would segvfault and that a single sprintf would be faster and also more readable, it just wasn't asked. He asked definitely for a way to get the date into a single pure INT.
RobSeace
09-13-2004, 07:57 PM
Exactly... If you're going to 'correct' someone else's code with a
self-proclaimed "better way", you really should make damn sure
the code isn't totally broken, as that code is... ;-) (Also, rather than
either that insane approach OR sprintf(), I'd recommend just using
strftime(), if you wanted a string result...)
And, the only situation in which "fulldate" would need to be a long
is on a 16-bit system... And, if you're still coding on a 16-bit system,
well you have my sympathy... ;-) But, you also probably already
know about such limitations in your ancient system, too... However,
on modern 32-bit systems (and, on most new 64-bit systems), a
plain old int is 32-bits, so is plenty big enough to hold YYYYMMDD...
Not too long ago I replaced strftime in my server code ( it's needed twice per file request ) and got a speed up of 6% by that alone. So Rob, as long as it's such an easy case I'd never touch that thing again.
RobSeace
09-14-2004, 12:55 PM
Really, it's that much of a pig?? Well, I suppose I'm not TOO
surprised by that... It does have an insane amount of functionality,
after all... But, still, for a relatively simple case, I wouldn't think it
should need to do anything too horrible... But, I've certainly never
looked at the code for it, either... *shrug*
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.