473,786 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1488

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

Similar topics

0
363
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 as System.DateTime can never be null. So public class MyClass { public string a;
1
9834
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 is that I'd like to store DateTime variables that are allowed to hold date and time as UTC in the database. However I'd to store DateTime variables that are only allowed to hold a date (like a birthdate) not as UTC (since adding a +6h time offset...
6
4213
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." Is there a way to make sure that the date header is sent over or a way to work around this exception? Here's a sample of the code:
1
6135
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 start date & an end date & may span a weekend) If a start date (workday) & an end date (workday) are on the same day then the number of workdays will equal zero Some eg's.
12
2611
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 example: 21/05/2006
3
1486
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; public string FamilyName; public DateTime BirthDate; public string Gender;
7
3956
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 overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third...
3
1494
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; private int minimum; private int maximum; private int defu;
2
3115
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 only date is displayed and edited (without time part) ? Andrus.
7
4169
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; private in dYear; public Date() {
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.