00001 // more/sys/date.h 00002 // Copyright (C) 2001 Petter Urkedal (petter.urkedal@matfys.lth.se) 00003 // 00004 // This file is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This file is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 // 00018 // As a special exception, you may use this file as part of a free 00019 // software library without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to 00022 // produce an executable, this file does not by itself cause the 00023 // resulting executable to be covered by the GNU General Public 00024 // License. This exception does not however invalidate any other 00025 // reasons why the executable file might be covered by the GNU General 00026 // Public License. 00027 // 00028 // $Id: date.h,v 1.1 2002/05/30 18:01:40 petter_urkedal Exp $ 00029 00030 00031 #ifndef MORE_SYS_DATE_H 00032 #define MORE_SYS_DATE_H 00033 00034 #include <more/sys/time.h> 00035 #include <more/io/fwd.h> 00036 00037 00038 namespace more { 00039 namespace sys { 00040 00041 /** cultural notion of time. That is, year, month, ..., h, min, s. */ 00042 struct date 00043 { 00044 /** an arithmetic type with implicit conversion to time_t, used 00045 * to express the timezone. */ 00046 typedef long timezone_type; 00047 /** used to specify the precision of the date. */ 00048 enum precision_type { 00049 undefined, 00050 within_1_year, within_1_month, within_1_day, 00051 within_1_h, within_1_min, within_1_s 00052 }; 00053 /** trivial default constructor. */ 00054 date() : m_prec(undefined) {} 00055 // date(date cosnt&) = implicit; 00056 // date& operator=(date const&) = implicit; 00057 /** construct a date from time_t. Use m_tz = 0 to make an UTC date. */ 00058 date(time_t t, precision_type tp, timezone_type tz); 00059 /** construct the a local date from a time_t. */ 00060 date(time_t, precision_type tp = within_1_s); 00061 /** construct from tm data. */ 00062 date(tm* c_tm) : m_tm(*c_tm) {} 00063 /** Construct local date from year, month, ..., down to given 00064 precision. */ 00065 date(int year, int month = -1, int day = -1, 00066 int h = -1, int min = -1, int s = -1); 00067 00068 /** True unless an unchanged default constructed object or a 00069 copy of such. */ 00070 bool is_defined() const { return m_prec != undefined; } 00071 00072 /** seconds, 0..59. */ 00073 int s() const { return m_tm.tm_sec; } 00074 /** minutes, 0..59. */ 00075 int min() const { return m_tm.tm_min; } 00076 /** hours, 0..23. */ 00077 int h() const { return m_tm.tm_hour; } 00078 /** day of the month 1..31. */ 00079 int day() const { return m_tm.tm_mday; } 00080 /** month of the year 1..12. */ 00081 int month() const { return m_tm.tm_mon + 1; } 00082 /** the year. */ 00083 int year() const { return m_tm.tm_year + 1900; } 00084 /** days passed since previus new year. */ 00085 int day_of_year() const { return m_tm.tm_yday; } 00086 /** days passed since previous sunday. */ 00087 int day_of_week() const { return m_tm.tm_wday; } 00088 /** true if daylight saving time is in effect. */ 00089 bool is_dst() const { return m_tm.tm_isdst; } 00090 00091 operator time_t() const { return std::mktime(&m_tm) - timezone(); } 00092 00093 /** returns tm data for interfacing C functions. */ 00094 std::tm const* c_tm() const { return &m_tm; } 00095 00096 /** returns the difference between UTC and the timezone for this date. */ 00097 timezone_type timezone() const { return m_tz; } 00098 00099 /** used by more::io::syncstream, don't call directly. */ 00100 void sync(more::io::syncstream&); 00101 00102 private: 00103 mutable tm m_tm; 00104 long m_tz; 00105 precision_type m_prec; 00106 00107 template<typename CharT, typename Traits> 00108 friend std::basic_istream<CharT, Traits>& 00109 operator>>(std::basic_istream<CharT, Traits>& is, date& d); 00110 template<typename CharT, typename Traits> 00111 friend std::basic_ostream<CharT, Traits>& 00112 operator<<(std::basic_ostream<CharT, Traits>&, date const&); 00113 }; 00114 00115 }} // more::sys 00116 00117 #endif