This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-goldedplus/goldlib/gall/gtimall.h

247 lines
6.1 KiB
C
Raw Normal View History

2000-02-25 10:15:17 +00:00
// This may look like C code, but it is really -*- C++ -*-
// ------------------------------------------------------------------
// The Goldware Library
// Copyright (C) 1990-1999 Odinn Sorensen
// Copyright (C) 1999-2000 Alexander S. Aganichev
// ------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307, USA
// ------------------------------------------------------------------
// $Id$
// ------------------------------------------------------------------
// Time utility functions.
// ------------------------------------------------------------------
#ifndef __gtimall_h
#define __gtimall_h
// ------------------------------------------------------------------
#include <ctime>
#include <cstddef>
#include <gdefs.h>
#ifdef __UNIX__
#include <unistd.h>
2000-02-25 10:15:17 +00:00
#include <sys/times.h>
#endif
#ifdef __OS2__
#define INCL_BASE
#include <os2.h>
#endif
#ifdef __WIN32__
#include <windows.h>
#endif
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
#include <time.h>
#endif
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
typedef long Clock;
2002-09-24 10:16:04 +00:00
// ------------------------------------------------------------------
#if defined(GOLD_CANPACK)
#pragma pack(1)
#endif
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
// DOS "findfirst" timestamp
struct gfiletime {
unsigned ft_tsec : 5; // Second / 2
unsigned ft_min : 6; // Minutes
unsigned ft_hour : 5; // Hours
unsigned ft_day : 5; // Days
unsigned ft_month : 4; // Months
unsigned ft_year : 7; // Year - 80
const char* c_str(char* buf);
dword number() { return *(dword*)this; }
2002-09-24 10:16:04 +00:00
};
2000-02-25 10:15:17 +00:00
typedef gfiletime FFTime;
// ------------------------------------------------------------------
// Opus DOS-style file timestamp
struct gopustime {
unsigned ft_day : 5; // Days
unsigned ft_month : 4; // Months
unsigned ft_year : 7; // Year - 80
unsigned ft_tsec : 5; // Second / 2
unsigned ft_min : 6; // Minutes
unsigned ft_hour : 5; // Hours
const char* c_str(char* buf);
dword number() { return *(dword*)this; }
2002-09-24 10:16:04 +00:00
};
2000-02-25 10:15:17 +00:00
typedef gopustime FTime;
2002-09-24 10:16:04 +00:00
// ------------------------------------------------------------------
#if defined(GOLD_CANPACK)
#pragma pack()
#endif
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
// Externs for strftimei()
extern char* __gsweekday[];
extern char* __glweekday[];
extern char* __gsmonth[];
extern char* __glmonth[];
extern char* __gampm[];
extern char* gsweekday[];
extern char* glweekday[];
extern char* gsmonth[];
extern char* glmonth[];
extern char* gampm[];
extern const char* gmonths[];
// ------------------------------------------------------------------
// Prototypes
inline void ggmtime(struct tm *_tm, const time32_t *timep)
{
2005-10-20 21:10:42 +00:00
const time_t temp(*timep);
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
if (0 != gmtime_s(_tm, &temp))
{
const time_t zero(0);
gmtime_s(_tm, &zero);
}
#else
struct tm *time = gmtime(&temp);
#if defined(__WIN32__)
if (NULL == time)
{
const time_t zero(0);
time = gmtime(&zero);
}
#endif
*_tm = *time;
#endif
2000-02-25 10:15:17 +00:00
}
inline void glocaltime(struct tm *_tm, const time32_t *timep)
{
2005-10-20 21:10:42 +00:00
const time_t temp(*timep);
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
if (0 != localtime_s(_tm, &temp))
{
const time_t zero(0);
localtime_s(_tm, &zero);
}
#else
struct tm *time = localtime(&temp);
#if defined(__WIN32__)
if (NULL == time)
{
const time_t zero(0);
time = localtime(&zero);
}
#endif
*_tm = *time;
2000-02-25 10:15:17 +00:00
#endif
}
inline void gctime(TCHAR *buffer, size_t sizeInChars, const time32_t *timep)
{
const time_t temp(*timep);
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
if (0 != _tctime_s(buffer, sizeInChars, &temp))
{
const time_t zero(0);
_tctime_s(buffer, sizeInChars, &zero);
}
2005-10-20 21:10:42 +00:00
#else
const char *time = _tctime(&temp);
#if defined(__WIN32__)
if (NULL == time)
{
const time_t zero(0);
time = _tctime(&zero);
}
#endif
strxcpy(buffer, time, sizeInChars);
2005-10-20 21:10:42 +00:00
#endif
}
2005-10-20 21:10:42 +00:00
inline time32_t gtime(time32_t *timep)
{
2005-10-20 21:10:42 +00:00
time32_t temp = (time32_t)time(NULL);
return timep ? *timep = temp : temp;
}
2000-02-25 10:15:17 +00:00
2005-10-20 21:10:42 +00:00
inline time32_t gmktime(struct tm *timep)
{
return (time32_t)mktime(timep);
}
2000-02-25 10:15:17 +00:00
#if defined(__OS2__)
inline void usleep(long duration) { DosSleep(duration); }
2002-11-20 13:10:00 +00:00
#elif defined(__MINGW32__) || defined(_MSC_VER)
2000-02-25 10:15:17 +00:00
inline void usleep(long duration) { Sleep(duration); }
#endif
2002-09-24 10:16:04 +00:00
#ifndef CLK_TCK
#define CLK_TCK CLOCKS_PER_SEC
#endif
2000-02-25 10:15:17 +00:00
#ifdef __UNIX__
inline Clock gclock() { struct tms z; return Clock(times(&z)*10/sysconf(_SC_CLK_TCK)); }
2000-02-25 10:15:17 +00:00
#else
inline Clock gclock() { return Clock(clock()*10/CLK_TCK); }
#endif
int str2mon(const char* ptr) __attribute__ ((const));
int tzoffset();
2000-03-22 16:55:00 +00:00
char* strftimei(char* s, size_t maxsize, const char* fmt, const struct tm* t); // __attribute__ ((format (strftime, 3, 0)));
2000-02-25 10:15:17 +00:00
2005-10-20 21:10:42 +00:00
FTime TimeToFTime(time32_t __time) __attribute__ ((const));
time32_t FTimeToTime(FTime* __ftime, struct tm* __tm=NULL);
2000-02-25 10:15:17 +00:00
2005-10-20 21:10:42 +00:00
time32_t FidoTimeToUnix(char* __fidotime);
2000-02-25 10:15:17 +00:00
char* FTimeToStr(char* buf, FTime &t);
2005-10-20 21:10:42 +00:00
char* TimeToStr(char* buf, time32_t t);
2000-02-25 10:15:17 +00:00
// ------------------------------------------------------------------
long YMD2JDN(unsigned yr, unsigned mo, unsigned day) __attribute__ ((const));
void JDN2YMD(long scalar, unsigned *yr, unsigned *mo, unsigned *day);
// ------------------------------------------------------------------
#endif
// ------------------------------------------------------------------