473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Yet another date class, public-sourced

I'm tossing my date class into the public domain because I'm tired of it
sitting around gathering dust. It was first created over a decade ago
and has been evolving by bits and pieces since then.

Here is the doc with the source+test linked off of it:

http://home.twcny.rr.com/hinnant/cpp...rian_date.html

It has a relatively small footprint (2 words typical). It has a cute
construction interface that makes it very difficult, if not impossible,
to mistakenly create the wrong date:

date d1 = mar/19/2006;
date d2 = day(19)/3/2006;
date d3 = year(2006)/mar/19;

These all refer to the same day. Ambiguous date constructions fail at
compile time. Out of range date constructions fail at run time. It is
not possible for a date to hold an invalid value.

Date arithmetic is fully supported, including (for example) iterating
over the last saturday of each month in a year:

for (date d = last*sat/jan/2004, end = last*sat/dec/2004;
d <= end; d += month(1))
std::cout << d << '\n';

01/31/04
02/28/04
03/27/04
04/24/04
05/29/04
06/26/04
07/31/04
08/28/04
09/25/04
10/30/04
11/27/04
12/25/04

I/O is punted to the std::time_get/time_put facets.

It is a small package, 1 header, 1 source totaling less than 700 lines
of code. It has a fairly minimal interface:

* There's a wide choice of how to construct dates, but none of them
will allow an invalid date to be constructed.

* There is date arithmetic including dates +/- days, months or years,
and the notion of "last" applying to "day of the month" or "weekday of
the month". You can not compute an invalid date (an exception occurs if
you try).

* Five member observers:

int day() const;
int month() const;
int year() const;
int day_of_week() const;
bool is_leap() const;

* There are no mutators except for the arithmetic operators (e.g. +=).

If it is useful or amusing for you, great. If not, there's plenty more
choices out there (boost::date for example is a good one).

-Howard
Mar 19 '06 #1
0 1473

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dominic Messenger | last post by:
I have several elements that have optional child elements and attributes that are xs:dates. I have been using System.DateTime and the XmlAttribute/XmlElement attributes, but these will never work...
1
by: ESPNSTI | last post by:
Hi, I'd like to be able to distinguish between a DateTime variable that is allowed to hold both date and time and a DateTime variable that is allowed to only hold a Date. The reason's for this...
6
by: Mike Koerner | last post by:
Hi, I am having problems setting the HttpWebRequest Date header. I understand that it is a restricted header and I do receive the "This header must be modified with the appropriate property." ...
1
by: Wayne | last post by:
Hi all I'm trying to calculate the number of days (or workdays) between 2 given dates that do not include weekend days or public holidays (public holidays are user defined from a dbase, have a...
12
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for...
3
by: Jeremy Chapman | last post by:
I've created a web service method which looks like public MatchResult CheckMatch(MatchData MatchValues) MatchData is a class defined as: public class MatchData { public string GivenName;...
7
by: erekose666 | last post by:
I need a java prog to do the following: Create class Date with the following capabilities: a) Output the date in multiple formats, such as: MM/DD/YYYY June 14, 2005 DDD YYYY b) Use...
3
by: miss time | last post by:
Hi please tell me the wrong in this code becaus it not compile with me the question depend in other class that i do it before and this class her: public class GField { private int value;...
2
by: Andrus | last post by:
I need to display and edit only date part in DataGridView column whose data source type is DateTime?. The following code shows and allows to edit also time part. How to modify this code so that...
7
by: kr151080 | last post by:
Ok so I am messing around with a program and have no idea how to go about doing this but here is the code for the class date.... public class Date { private int dMonth; private int dDay;...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.