 |
|
 |
|
07-20-07 06:20 PM
SLES9 and RHES4. Stock GCC and CLIB
I need seconds past midnight for the local machine. I can do this by
a series of conversions, but since I have to do a lot of them I've
been looking for a single call (or minimal number of calls) that
produces this.
Google and man coming up empty so far. Anyone have a good reference
I can check?
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: seconds past midnight? |
 |
 |
|
|
07-20-07 06:20 PM
On Fri, 20 Jul 2007 16:06:14 GMT, Richard Eich <richard.eich@domain.invalid> wrote:
> SLES9 and RHES4. Stock GCC and CLIB
>
> I need seconds past midnight for the local machine. I can do this by
> a series of conversions, but since I have to do a lot of them I've
> been looking for a single call (or minimal number of calls) that
> produces this.
>
> Google and man coming up empty so far. Anyone have a good reference
> I can check?
Try Googling for the manpages of strftime(), localtime(), gmtime(),
mktime(), and similar calls. A sample program using them is shown
below. It doesn't *print* the difference in seconds of the current time
and today's midnight, but it comes pretty close and should be _very_
easy to adapt:
------------------------------------------------------------------------
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void ptime(const char *, struct tm *);
int
main(void)
{
char errbuf[LINE_MAX + 1];
struct tm now, midnight;
time_t now_sec;
now_sec = time(NULL);
if (localtime_r(&now_sec, &now) == NULL) {
int save_errno = errno;
if (strerror_r(save_errno, errbuf, sizeof(errbuf)) != 0)
exit(EXIT_FAILURE);
fprintf(stderr, "localtime_r: error %d: %s\n",
save_errno, errbuf);
exit(EXIT_FAILURE);
}
/* Copy the current time to 'midnight' and tweak. */
midnight = now;
midnight.tm_sec = 0;
midnight.tm_min = 0;
midnight.tm_hour = 0;
ptime("now", &now);
ptime("midnight", &midnight);
return 0;
}
void
ptime(const char *txt, struct tm *tm)
{
char errbuf[LINE_MAX + 1];
char timebuf[LINE_MAX + 1];
time_t sec;
if (tm == NULL)
return;
if (txt == NULL)
txt = "??";
sec = mktime(tm);
if (strftime(timebuf, sizeof(timebuf) - 1,
"%Y-%m-%d %H:%M:%S", tm) >= sizeof(timebuf)) {
int save_errno = errno;
if (strerror_r(save_errno, errbuf, sizeof(errbuf)) != 0)
exit(EXIT_FAILURE);
fprintf(stderr, "strftime @ %p: error %d: %s\n", tm,
save_errno, errbuf);
exit(EXIT_FAILURE);
}
printf("%-10s | %12llu | %s\n", txt, (unsigned long long)sec,
timebuf);
}
------------------------------------------------------------------------
Compile this with your C compiler, run it and it should print something
similar to:
$ ./a.out
now | 1184950846 | 2007-07-20 20:00:46
midnight | 1184878800 | 2007-07-20 00:00:00
$
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: seconds past midnight? |
 |
 |
|
|
07-20-07 06:20 PM
On Jul 20, 12:06 pm, Richard Eich <richard.e...@domain.invalid> wrote:
> SLES9 and RHES4. Stock GCC and CLIB
>
> I need seconds past midnight for the local machine. I can do this by
> a series of conversions, but since I have to do a lot of them I've
> been looking for a single call (or minimal number of calls) that
> produces this.
>
> Google and man coming up empty so far. Anyone have a good reference
> I can check
Isn't it just time(0) % 86400, for GMT at least?
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: seconds past midnight? |
 |
 |
|
|
07-20-07 06:20 PM
Richard Eich wrote On 07/20/07 12:06,:
> SLES9 and RHES4. Stock GCC and CLIB
>
> I need seconds past midnight for the local machine. I can do this by
> a series of conversions, but since I have to do a lot of them I've
> been looking for a single call (or minimal number of calls) that
> produces this.
>
> Google and man coming up empty so far. Anyone have a good reference
> I can check?
It ought to be pretty simple to cache the result of
the conversions so that you only need to do them once
per day (or per execution). Something like
int secondsSinceMidnight(void) {
static time_t midnight = 0;
time_t now = time(NULL);
if (now - midnight >= 24 * 60 * 60) {
struct tm *p = localtime(&now);
p->tm_hour = 0;
p->tm_min = 0;
p->tm_sec = 0;
midnight = mktime(p);
}
return now - midnight;
}
As it stands, this isn't quite good enough: it will get
confused by the start and end of daylight saving time.
The changes you'll need depend on just how you want to
define "seconds past midnight" in light of DST, and that
in turn depends on what use you're making of the result.
But the complications are largely beside the point: the
basic idea of caching the result allows you to amortize
the cost of those complications over a long period.
--
Eric.Sosman@sun.com
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: seconds past midnight? |
 |
 |
|
|
07-21-07 12:21 AM
Richard Eich wrote:
> SLES9 and RHES4. Stock GCC and CLIB
>
> I need seconds past midnight for the local machine. I can do this by
> a series of conversions, but since I have to do a lot of them I've
> been looking for a single call (or minimal number of calls) that
> produces this.
$ PERL -MTime::Local -le'print time - timelocal 0,0,0, (localtime)[3..8]
'
49716
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: seconds past midnight? |
 |
 |
|
|
07-21-07 12:27 PM
keramida@ceid.upatras.gr wrote...
> On Fri, 20 Jul 2007 16:06:14 GMT, Richard Eich <richard.eich@domain.invali
d> wrote:
>
> Try Googling for the manpages of strftime(), localtime(), gmtime(),
> mktime(), and similar calls. A sample program using them is shown
> below. It doesn't *print* the difference in seconds of the current time
> and today's midnight, but it comes pretty close and should be _very_
> easy to adapt:
>
> ------------------------------------------------------------------------
> #include <errno.h>
> #include <limits.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <time.h>
>
> void ptime(const char *, struct tm *);
>
> int
> main(void)
> {
> char errbuf[LINE_MAX + 1];
> struct tm now, midnight;
> time_t now_sec;
>
> now_sec = time(NULL);
> if (localtime_r(&now_sec, &now) == NULL) {
> int save_errno = errno;
>
> if (strerror_r(save_errno, errbuf, sizeof(errbuf)) != 0)
> exit(EXIT_FAILURE);
> fprintf(stderr, "localtime_r: error %d: %s\n",
> save_errno, errbuf);
> exit(EXIT_FAILURE);
> }
>
> /* Copy the current time to 'midnight' and tweak. */
> midnight = now;
> midnight.tm_sec = 0;
> midnight.tm_min = 0;
> midnight.tm_hour = 0;
>
> ptime("now", &now);
> ptime("midnight", &midnight);
>
> return 0;
> }
>
> void
> ptime(const char *txt, struct tm *tm)
> {
> char errbuf[LINE_MAX + 1];
> char timebuf[LINE_MAX + 1];
> time_t sec;
>
> if (tm == NULL)
> return;
> if (txt == NULL)
> txt = "??";
>
> sec = mktime(tm);
> if (strftime(timebuf, sizeof(timebuf) - 1,
> "%Y-%m-%d %H:%M:%S", tm) >= sizeof(timebuf)) {
> int save_errno = errno;
>
> if (strerror_r(save_errno, errbuf, sizeof(errbuf)) != 0)
> exit(EXIT_FAILURE);
> fprintf(stderr, "strftime @ %p: error %d: %s\n", tm,
> save_errno, errbuf);
> exit(EXIT_FAILURE);
> }
> printf("%-10s | %12llu | %s\n", txt, (unsigned long long)sec,
> timebuf);
> }
> ------------------------------------------------------------------------
>
> Compile this with your C compiler, run it and it should print something
> similar to:
>
> $ ./a.out
> now | 1184950846 | 2007-07-20 20:00:46
> midnight | 1184878800 | 2007-07-20 00:00:00
> $
Thanks very much -- it's amazing how much reading the right resource
helps. ;)
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: seconds past midnight? |
 |
 |
|
|
07-21-07 12:27 PM
Eric.Sosman@sun.com wrote...
> Richard Eich wrote On 07/20/07 12:06,:
>
> It ought to be pretty simple to cache the result of
> the conversions so that you only need to do them once
> per day (or per execution). Something like
>
> int secondsSinceMidnight(void) {
> static time_t midnight = 0;
> time_t now = time(NULL);
> if (now - midnight >= 24 * 60 * 60) {
> struct tm *p = localtime(&now);
> p->tm_hour = 0;
> p->tm_min = 0;
> p->tm_sec = 0;
> midnight = mktime(p);
> }
> return now - midnight;
> }
>
> As it stands, this isn't quite good enough: it will get
> confused by the start and end of daylight saving time.
> The changes you'll need depend on just how you want to
> define "seconds past midnight" in light of DST, and that
> in turn depends on what use you're making of the result.
> But the complications are largely beside the point: the
> basic idea of caching the result allows you to amortize
> the cost of those complications over a long period.
This is pretty much how I'm doing it now, only without the caching
idea. This is workable -- thank you very much.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: seconds past midnight? |
 |
 |
|
|
07-21-07 12:27 PM
shakahshakah@gmail.com wrote...
> On Jul 20, 12:06 pm, Richard Eich <richard.e...@domain.invalid> wrote:
>
> Isn't it just time(0) % 86400, for GMT at least?
LOL. Boy, is this answer ever humiliating...
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: seconds past midnight? |
 |
 |
|
|
07-23-07 12:22 PM
On Jul 21, 8:04 am, Richard Eich <richard.e...@domain.invalid> wrote:
> shakahsha...@gmail.com wrote...
>
>
>
>
> LOL. Boy, is this answer ever humiliating...
Granted I posted without thinking too much about it, but feel free to
enlighten me.
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: seconds past midnight? |
 |
 |
|
|
07-23-07 06:21 PM
shakahshakah@gmail.com wrote:
> On Jul 21, 8:04 am, Richard Eich <richard.e...@domain.invalid> wrote:
>
> Granted I posted without thinking too much about it, but feel free to
> enlighten me.
I think he meant humiliating for him because it's so simple and obvious.
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 09:54 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|