time.h

Go to the documentation of this file.
00001 ///
00002 /// \file       time.h
00003 ///             Time related conversion routines.
00004 ///             time_t is the POSIX time.
00005 ///             min1900_t is the minutes from Jan 1, 1900
00006 ///
00007 
00008 /*
00009     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00010 
00011     This program is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     This program is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00019 
00020     See the GNU General Public License in the COPYING file at the
00021     root directory of this project for more details.
00022 */
00023 
00024 #ifndef __BARRY_TIME_H__
00025 #define __BARRY_TIME_H__
00026 
00027 #include "dll.h"
00028 #include <sys/time.h>           // for struct timespec
00029 #include <time.h>
00030 #include <stdint.h>
00031 
00032 //
00033 // Calculate the number of minutes between Jan 01, 1900 and Jan 01, 1970
00034 //
00035 // There are 17 leap years between 1900 and 1970
00036 //    (1969-1900) / 4 = 17.25
00037 //
00038 // 1900 itself is not a leap year (not divisible by 400)
00039 //
00040 #define DAY_MINUTES (24 * 60)
00041 #define YEAR_MINUTES (365 * DAY_MINUTES)
00042 #define LEAP_YEAR_COUNT ((1970-1901) / 4)
00043 #define YEAR_COUNT (1970 - 1900)
00044 
00045 // therefore, the difference between standard C's time and min1900_t's
00046 // time in minutes:
00047 #define STDC_MIN1900_DIFF (YEAR_COUNT * YEAR_MINUTES + LEAP_YEAR_COUNT * DAY_MINUTES)
00048 
00049 namespace Barry {
00050 
00051 typedef long min1900_t;
00052 
00053 BXEXPORT min1900_t time2min(time_t t);
00054 BXEXPORT time_t min2time(min1900_t m);
00055 
00056 // FIXME - turn StaticTimeZone into a C typedef and wrap this in extern "C"
00057 // so the data can be used in both C and C++ libraries
00058 //
00059 // This is named StaticTimeZone since the time zone table is hard coded
00060 // in the library.  If you want to know what the device's idea of time zones
00061 // is, then extract the time zone database using the TimeZone record class.
00062 //
00063 // See also the TimeZones class, which unifies access to the static and
00064 // dynamic time zone tables in one class API.
00065 //
00066 struct BXEXPORT StaticTimeZone
00067 {
00068         uint16_t Code;
00069         signed short HourOffset;
00070         signed short MinOffset;
00071         const char *Name;
00072 };
00073 
00074 // FIXME - put this somewhere for both C and C++
00075 #define STATIC_TIME_ZONE_CODE_ERR       0xffff
00076 
00077 BXEXPORT const StaticTimeZone* GetStaticTimeZoneTable();
00078 BXEXPORT const StaticTimeZone* GetStaticTimeZone(uint16_t Code);
00079 BXEXPORT unsigned short GetStaticTimeZoneCode(signed short HourOffset,
00080         signed short MinOffset = 0);
00081 
00082 // Message time conversion stuff
00083 BXEXPORT time_t DayToDate( uint16_t Day );
00084 BXEXPORT time_t Message2Time(uint16_t r_date, uint16_t r_time);
00085 
00086 // Thread timeout creation
00087 BXEXPORT struct timespec* ThreadTimeout(int timeout_ms, struct timespec *spec);
00088 
00089 // Utility functions
00090 BXEXPORT int DaysInMonth(struct tm &t);
00091 
00092 } // namespace Barry
00093 
00094 #endif
00095