monotone

monotone Mtn Source Tree

Root/src/dates.hh

1// Copyright (C) 2007 Zack Weinberg <zackw@panix.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#ifndef DATES_HH
11#define DATES_HH
12
13// This file provides a straightforward wrapper class around the standard
14// C time functions. Note that all operations are done in UTC, *not* the
15// user's time zone.
16
17#include "numeric_vocab.hh"
18
19struct date_t
20{
21 // initialize to an invalid date
22 date_t();
23
24 // initialize from milliseconds since the unix epoch
25 date_t(s64 d);
26
27 // initialize from broken-down time
28 date_t(int year, int month, int day,
29 int hour=0, int min=0, int sec=0, int millisec=0);
30
31 // initialize from a string; presently recognizes only
32 // ISO 8601 "basic" and "extended" time formats.
33 date_t(std::string const & s);
34
35 // initialize to the current date and time
36 static date_t now();
37
38 bool valid() const;
39
40 // Retrieve the date as a string.
41 std::string as_iso_8601_extended() const;
42
43 // Retrieve the date as a string, formatted using the strftime(3)
44 // specification in 'fmt', and converted to local time. For user
45 // display only.
46 std::string as_formatted_localtime(std::string const & fmt) const;
47
48 static date_t from_formatted_localtime(std::string const & s,
49 std::string const & fmt);
50
51 // Retrieve the internal milliseconds count since the Unix epoch.
52 s64 as_millisecs_since_unix_epoch() const;
53
54 // Date comparison operators
55 bool operator <(date_t const & other) const
56 { return d < other.d; };
57 bool operator <=(date_t const & other) const
58 { return d <= other.d; };
59 bool operator >(date_t const & other) const
60 { return d > other.d; };
61 bool operator >=(date_t const & other) const
62 { return d >= other.d; };
63 bool operator ==(date_t const & other) const
64 { return d == other.d; };
65 bool operator !=(date_t const & other) const
66 { return d != other.d; };
67
68 // Addition and subtraction of millisecond amounts
69 date_t & operator +=(s64 const other);
70 date_t & operator -=(s64 const other);
71 date_t operator +(s64 const other) const;
72 date_t operator -(s64 const other) const;
73
74 // Difference between two dates in milliseconds
75 s64 operator -(date_t const & other) const;
76
77private:
78 // The date as a signed 64-bit count of milliseconds since
79 // the Unix epoch (1970-01-01T00:00:00.000).
80 s64 d;
81};
82
83std::ostream & operator<< (std::ostream & o, date_t const & d);
84template <> void dump(date_t const & d, std::string & s);
85
86#endif // dates.hh
87
88// Local Variables:
89// mode: C++
90// fill-column: 76
91// c-file-style: "gnu"
92// indent-tabs-mode: nil
93// End:
94// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status