| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Unix timestamp |
* Reply to message originally in area NET_DEV
16 Oct 96 08:30, Paul Edwards wrote to Rowan_Crowe:
R>> I have a couple of routines in BASIC, and the above in C, but all 3
R>> return different results!
> The one that returns the same value as a commercial DOS compiler will
> probably be fine. Including Pacific C. BFN. Paul.
The following code returns different values with Borland, Microsoft and
Watcom's compilers:
#include
#include
#include
int main(void)
{
time_t now;
struct tm my_tm;
/* Set my_tm to Saturday, October 19, 1996 at 18:00:00 */
my_tm.tm_sec = 0;
my_tm.tm_min = 0;
my_tm.tm_hour = 18;
my_tm.tm_mday = 19;
my_tm.tm_mon = 9;
my_tm.tm_year = 96;
my_tm.tm_isdst = 0;
now = mktime(&my_tm);
if (now == (time_t) -1)
{
printf("Data contained in structure my_tm is invalid!\n");
return EXIT_FAILURE;
}
printf("asctime(&my_tm): %s", asctime(&my_tm));
printf("(unsigned long) now == %lu\n", (unsigned long) now);
return EXIT_SUCCESS;
}
Results:
Borland C++ for DOS 3.1 : 845762400
DJGPP 2.0 : 845712000
EMX : 845748000
Microsoft QuickC for DOS 2.5 : 845748000
Watcom C/C++ for DOS 10.0 : 845712000
Here was my reply to Rowan's request in C_ECHO:
* Forwarded from C_ECHO by andrew clarke (3:635/728.4{at}fidonet).
* Originally by: andrew clarke (3:635/728.4{at}fidonet), 16 Oct 96 03:48.
* Originally to: Rowan_Crowe (0/0).
{at}MSGID: 3:635/728.4{at}fidonet 323ccec8
{at}REPLY: 3:635/728.1{at}fidonet 214f477f
15 Oct 96 08:51, Rowan_Crowe wrote to All:
> Does anyone have a unix timestamp function?
Here's what I came up with.
/*
* UNIXTIME.C
*
* Returns the number of seconds since midnight on January 1, 1970.
* Written in October 1996 by Andrew Clarke.
* Released to the public domain.
*/
#include
#include
int isleap(int year)
{
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
#define UNIX_EPOCH 1970
unsigned long unixtime(const struct tm *tm)
{
int year, i;
unsigned long result;
result = 0UL;
year = tm->tm_year + 1900;
/* Traverse through each year */
for (i = UNIX_EPOCH; i < year; i++)
{
result += 31536000UL; /* 60s * 60m * 24h * 365d = 31536000s */
if (isleap(i))
{
/* It was a leap year; add a day's worth of seconds */
result += 86400UL; /* 60s * 60m * 24h = 86400s */
}
}
/* Traverse through each day of the year, adding a day's worth
* of seconds each time. */
for (i = 0; i tm_yday; i++)
{
result += 86400UL; /* 60s * 60m * 24h = 86400s */
}
/* Now add the number of seconds remaining */
result += 3600UL * tm->tm_hour;
result += 60UL * tm->tm_min;
result += (unsigned long) tm->tm_sec;
return result;
}
#ifdef TEST
int main(void)
{
time_t now;
struct tm *tm;
unsigned long unix_tm;
now = time(NULL);
tm = localtime(&now);
unix_tm = unixtime(tm);
printf("UNIX timestamp: %lu (0x%08lx), Local time: %s", unix_tm,
unix_tm, asctime(tm));
return 0;
}
#endif
Andrew Clarke, VidMgr author
Zone 3 moderator of the C_Echo
Melbourne, Victoria, Australia
randy{at}zws.com
-+- Msged/2 4.00
+ Origin: Blizzard of Ozz, Melbourne, Australia (3:635/728.4{at}fidonet)
--- Msged/2 4.00
* Origin: Blizzard of Ozz, Melbourne, Australia (3:635/728.4{at}fidonet)SEEN-BY: 50/99 620/243 623/630 635/503 544 727 728 670/218 711/409 410 413 SEEN-BY: 711/430 808 809 932 934 712/515 713/317 714/906 800/1 @PATH: 635/728 50/99 711/808 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.