FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

date class in ares

 
Post new topic   Reply to topic     Forum Index -> Ares
View previous topic :: View next topic  
Author Message
hotzenplotz



Joined: 25 Mar 2006
Posts: 15

PostPosted: Sun Jul 09, 2006 8:30 am    Post subject: date class in ares Reply with quote

I had written a date class a few weeks ago.
http://www.forum.d-language.com/forum/viewtopic.php?t=43
Interest to implement it in Ares?
Back to top
View user's profile Send private message
PsychoBrat



Joined: 23 Jul 2005
Posts: 22

PostPosted: Sun Jul 09, 2006 8:40 pm    Post subject: Reply with quote

I think if a time/date class were to be included in Ares, to be complete there should also be an accompanying class to handle time/date intervals (to make date math make sense). Is this a reasonable assumption, and has anyone looked into this?
Back to top
View user's profile Send private message
Sue D. Nymme



Joined: 26 Jul 2006
Posts: 13
Location: Philadelphia, PA, USA

PostPosted: Wed Jul 26, 2006 11:53 am    Post subject: Date/Time class ideas Reply with quote

A Date/Time class should definitely be included. It's very necessary for everyday programming, and application programmers should not waste their time reinventing the same wheel over and over again.

A Date/Time class should:

  • Accept and produce dates in a wide range of formats.
  • Be portable (for example, it should not matter that Windows and Unix have different notions of "epoch").
  • Conform to user's locale (including month/day names in the local language; and including time zone).
  • Be very fast.
  • Provide most or all of the features that application programmers will need, without their having to piece together the same building blocks over and over.
  • Include a time/date interval class, as PsychoBrat suggests.

I would suggest that having separate classes for dates and for times is probably a bad idea. I would further suggest that the class be called "Time" instead of "Date", since the english word "date" implies calendar units (days, months, years) only; while the english word "time" can refer to any expanse of time. Henceforth, for purposes of this discussion, I'll refer to the classes as "Time" and "TimeInterval".

Constructors

Constructors are problematic, because there are so many ways people will want to create a date/time object. It is reasonable to want to create a Time object based on:

  • A year, month, and day
  • A year and month only
  • A month and day only
  • An hour and minute only
  • An hour, minute, and second
  • A minute and second only
  • A year, month, day, hour, minute, and second.
  • Any of the last 3 plus a millisecond or microsecond unit.
  • Any of the above plus a timezone specification.

A TimeInterval (how's that name sound?) object should also be able to be created with the same sorts of arguments.

How to create a constructor to do all this? I can think of several ways:

  1. Provide an argument to the constructor that indicates what the remaining arguments will be. For example:
    Code:
    obj = new Time ("ymd", year, month, day);
    obj = new Time ("hms", hour, minute, second);
    obj = new Time ("ym", year, month);

    There would be a limited number of such descriptor strings, so they wouldn't be something the constructor would have to parse. For example, the following would be illegal:
    Code:
    obj = new Time ("mdy", month, day, year);
    obh = new Time ("s", second);

  2. Require all six (seven? eight?) arguments to be specified each time, with dummy placeholders for unused ones:
    Code:
    obj =  new Time (year, month, day, Time.UNUSED, Time.UNUSED, Time.UNUSED);
    obj =  new Time (Time.UNUSED, Time.UNUSED, Time.UNUSED, hour, minute, second);

  3. Have several class functions which act as constructors, in that they return a new Time or TimeInterval object:
    Code:
    obj = new_Time_ymd (year, month, day);

    where:
    Code:
    module time;
    ...
    Time new_Time_ymd(int year, int month, int day)
    {
        return new Time (some combination of arguments here);
    }


Of these, I prefer the first, but what does the community think?

Time Intervals

The relationship between Time and a TimeInterval object types is as follows:

  • Time - Time yields TimeInterval
  • Time + Time is illegal
  • Time + TimeInterval yields Time
  • Time - TimeInterval yields Time
  • TimeInterval * number yields TimeInterval
  • TimeInterval / number yields TimeInterval
  • TimeInterval + TimeInterval yields TimeInterval
  • TimeInterval + TimeInterval yields TimeInterval

(those are all probably pretty obvious) Comparisons (<, >, etc) should also do have the obvious behavior.

General Time methods

The following operations are likely to be needed by programmers frequently:

  • Compute day of week (as number, full name (in locale's language), or abbreviation)
  • Compute month name and/or abbreviation
  • Compute last day of the same month (e.g. 7/26/2006 -> 7/31/2006)
  • Compute previous/successive day.
  • Convert to unix epoch / windows epoch
  • Change time zone
  • Compute whether Daylight Savings Time applies or not
  • Extract any time component (e.g. hour)
  • Set any time component
  • Is it a leap year?
  • Format to a string
  • Parse a date/time from an input string
  • Compute the week of the year (ISO definition, I guess)
  • Compute the quarter (1st, 2nd, 3rd, 4th)
  • Compute the quarter based on a fiscal-start date
  • Compute the week of the year
  • Compute the date of Sunday of the preceeding/current/next week
  • (and perhaps similarly for Monday, Tuesday...Saturday)
  • Compute the offset from UTC, based on the timezone.

Formatting

Here is a personal gripe of mine: extremely terse formatting codes. POSIX's strftime() function uses horrible ? codes, apparently in an attempt to be more like printf. I can never remember whether ?A is the month and ?B is the weekday, or vice-versa. Is ?Y four digits? Is month ?M or ?m?

Ditto for PHP's terse codes. Microsoft does a little better, using "mmm" for month abbreviation, "mmmm" for month name, etc.

In Perl, I implemented a module called Time::Format, which uses what I consider to be very natural, easy-to-remember formatting codes. A table of these codes can be found at http://search.cpan.org/~roode/Time-Format-1.02/Format.pm#VARIABLES. I would humbly (?) suggest that these formatting codes, or something similar, be considered for the Time and TimeInterval classes.

In brief, the formatting codes I came up with are "yy" for two-digit year, "yyyy" for four-digit year, "mm" for two-digit month, "Mon" for month abbreviation, "Month" for full month name, etc. One key feature of this set of codes was that the width of the code matched the width of the output as much as possible. Another was that "mm" stood for both "month" and "minute", depending on context.

Things I don't like about this set of codes:

  • The one-or-two digit codes (where numbers come out taking up one column if they're less than 10, or two columns if they're greater than 10) all begin with "?", as in "?d". I chose question-mark because the character in that position would either be a space or nonexistent -- hence, there was a "question" about how many columns it would take up. This may be overly-cute, and maybe some other format code would make more sense. Perhaps an underscore, as "_d".
  • If the "m" codes are ambiguous, and whether it's "month" or "minute" cannot be determined from context, the programmer can use a suffix to disambiguate it. The suffixes are "{in}" and "{on}", which I think are unnecessarily verbose and ugly. I don't have a better suggestion, though.

There should also be a method to output a Time in ISO8601 standard format.

Parsing

Parsing dates is a very tricky, difficult business. I have written Perl regular expressions to parse dates, and they are nasty. One was over 1500 characters. This is definitely something that should be encapsulated into a module; it's just too frequently-needed and error-prone to expect applications programmers to roll their own.

There are two types of parsing that generally needs to be done: fixed-format and variable-format. In fixed-format parsing, the input is in a known format -- perhaps coming from a data file. This is considerably easier than variable-format parsing, in which the input format is not known, or only vaguely known, such as user input.

For fixed-format parsing, the programmer should be able to specify the format precisely -- and I would suggest, using the same formatting codes as for output.

For variable-format parsing, the class should implement some sort of robust heuristics: try "year/month/day", then try "month/day/year", then try "day/month/year", etc, or maybe it could be smarter than that. It would have to throw an exception when it gets ambiguous input, such as "02/03/04".

Time Zones

Time Zones are annoying and difficult, because there are several standards for them, and because they vary so much around the world. The ISO8601 standard specifies a simple offset from UTC, expressed as +08:00 or -08:00. In America, there are two customs: a short identifier (eg EST, PST), or a city name. There is also the added complication of the annoying (imho) custom of Daylight Savings Time in much of the world.

I would suggest that D's Time class be able to accept most or all existing timezone formats, and to output timezone information in the standard +HH:MM format. I don't know how DST is handled in the non-US world, so I'm not sure what to suggest for that.

Conclusion

I hope I have given the community some useful ideas, and hope that some constructive discussion is stimulated. Smile

-- SueDNymme
_________________
"If anyone finds the above offensive, I am prepared not only to retract it, but also to deny under oath that I ever said it." -- Tom Lehrer
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
sean



Joined: 24 Jun 2004
Posts: 609
Location: Bay Area, CA

PostPosted: Wed Jul 26, 2006 12:34 pm    Post subject: Reply with quote

Great suggestions! Mango has some of these features as a part of its locale package, but I suspect it isn't nearly as complete as the above. Still, I'd like any such class to be compatible with mango.locale so I'll probably consider it within that context. No timeframe at the moment as I've been too busy to do much work on Ares, but I'll definately keep it in mind.
Back to top
View user's profile Send private message
pragma



Joined: 28 May 2004
Posts: 607
Location: Washington, DC

PostPosted: Wed Jul 26, 2006 12:47 pm    Post subject: Re: Date/Time class ideas Reply with quote

Sue D. Nymme wrote:
How to create a constructor to do all this? I can think of several ways:


Another possibility is to take advantage of D's type system and use typedefs for the date subtypes.

Code:

typedef uint millisecond;
typedef uint second;
typedef uint minute;
typedef uint hour;
typedef uint day;
typedef uint month;
typedef uint year;

auto today = new Date(cast(day)26,cast(year)2006,cast(month)7);


The date constructor would then use the variadic typeinfo to handle the information appropriately. The data is now position-independent, can work with a wide variety of argument combinations and requires absolutely no parsing. Also, for the minor exception of 'cast', I find this highly readable, and impossible to misinterpret. Smile

The system could then be further expanded to use other types:

Code:

typedef char[] dayname;
typedef char[] monthname;
typedef char[] yearname;
typedef char[] datename;

auto today = new Date(cast(datename)"today");
auto yesterday = new Date(cast(datename)"yesterday");
auto todayLastMonth = new Date(cast(dayname)"today",cast(yearname)"this year",cast(monthname)"last month");


I think you're dead-on with the other concepts and comments. Although I do perfer PHP's handling of date formatting and I most certainly cannot advise using the formatting put forward by Oracle SQL (it's awful).
_________________
-- !Eric.t.Anderton at gmail
Back to top
View user's profile Send private message Yahoo Messenger
Sue D. Nymme



Joined: 26 Jul 2006
Posts: 13
Location: Philadelphia, PA, USA

PostPosted: Wed Jul 26, 2006 2:03 pm    Post subject: Reply with quote

sean wrote:
Great suggestions! Mango has some of these features as a part of its locale package, but I suspect it isn't nearly as complete as the above. Still, I'd like any such class to be compatible with mango.locale so I'll probably consider it within that context. No timeframe at the moment as I've been too busy to do much work on Ares, but I'll definately keep it in mind.


I will look up mango.locale and see what it has, and think some thoughts about how that relates to the date/time class.

Since you don't have time at the moment, would you mind a volunteer? I am very new to D (one week!), but am very experienced in C (so the learning curve is pretty easy), and have done quite a lot of OO date programming in Perl. I would like to take some of these ideas and run with them, and present them to the group for feedback/improvement.

Is this agreeable to you?
_________________
"If anyone finds the above offensive, I am prepared not only to retract it, but also to deny under oath that I ever said it." -- Tom Lehrer
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
Sue D. Nymme



Joined: 26 Jul 2006
Posts: 13
Location: Philadelphia, PA, USA

PostPosted: Wed Jul 26, 2006 2:12 pm    Post subject: Re: Date/Time class ideas Reply with quote

pragma wrote:
Another possibility is to take advantage of D's type system and use typedefs for the date subtypes.


This is an intriguing idea. I don't like the look of all those casts in the constructor call, but I have to agree with you that it is quite readable.
_________________
"If anyone finds the above offensive, I am prepared not only to retract it, but also to deny under oath that I ever said it." -- Tom Lehrer
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
sean



Joined: 24 Jun 2004
Posts: 609
Location: Bay Area, CA

PostPosted: Wed Jul 26, 2006 2:44 pm    Post subject: Reply with quote

Sue D. Nymme wrote:
I will look up mango.locale and see what it has, and think some thoughts about how that relates to the date/time class.

Since you don't have time at the moment, would you mind a volunteer? I am very new to D (one week!), but am very experienced in C (so the learning curve is pretty easy), and have done quite a lot of OO date programming in Perl. I would like to take some of these ideas and run with them, and present them to the group for feedback/improvement.

Is this agreeable to you?

Sure thing Smile
Back to top
View user's profile Send private message
torhu



Joined: 30 Mar 2006
Posts: 56

PostPosted: Sat Sep 02, 2006 9:37 am    Post subject: Re: Date/Time class ideas Reply with quote

pragma wrote:
Sue D. Nymme wrote:
How to create a constructor to do all this? I can think of several ways:


Another possibility is to take advantage of D's type system and use typedefs for the date subtypes.

Code:

typedef uint millisecond;
typedef uint second;
typedef uint minute;
typedef uint hour;
typedef uint day;
typedef uint month;
typedef uint year;

auto today = new Date(cast(day)26,cast(year)2006,cast(month)7);

Is all this complexity really needed? Why not just:

DateTime d = new DateTime(2006, 12, 31);

All arguments are in order from most to least significant, like in ISO 8601.

Arguments could have defaults, like:

class DateTime {
this(int year=1900, int month=1, int day=1, hour=0, min=0, sec=0);
}

There would obviously be a constructor that will accept a date given as a string too.

But this would probably get messy if the Date constructor were to accept a time too. If you want to specify only a time and not a date, using a string might be the simplest solution. Or a static method like Sue suggested:

DateTime d = DateTime.fromTime(23, 41, 12); / / 11:41:12 PM
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Ares All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group