[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Private/DateTimeFormat
From: |
Gopal.V <address@hidden> |
Subject: |
[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Private/DateTimeFormat DateTimeFormatTemplate.cs,NONE,1.1 DateTimeFormatter.cs,NONE,1.1 DateTimeParser.cs,NONE,1.1 ParsedDateTime.cs,NONE,1.1 |
Date: |
Sun, 02 Feb 2003 11:37:05 -0500 |
Update of /cvsroot/dotgnu-pnet/pnetlib/runtime/System/Private/DateTimeFormat
In directory subversions:/tmp/cvs-serv5965/runtime/System/Private/DateTimeFormat
Added Files:
DateTimeFormatTemplate.cs DateTimeFormatter.cs
DateTimeParser.cs ParsedDateTime.cs
Log Message:
Michael Moore's DateTime parsing & formatting updates
--- NEW FILE ---
/*
* DateTimeFormatTemplate.cs - Implementation of the
* "System.Private.DateTimeFormat.DateTimeFormatTemplate" class.
*
* Copyright (C) 2002 Southern Storm Software, Pty Ltd.
*
* Contributions from Michael Moore <address@hidden>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
[...975 lines suppressed...]
}
internal class ExplicitString: FormatTemplate
{
internal String Text="";
internal override String GetString(DateTime
d,DateTimeFormatInfo info)
{
return Text;
}
internal override int Parse(String s, int start,
DateTimeFormatInfo info)
{
return start+1;
}
internal override ParsedDateTime StoreTo(ParsedDateTime d)
{
return d;
}
}
}
--- NEW FILE ---
/*
* DateTimeFormatter.cs - Implementation of the
* "System.Private.DateTimeFormat.DateTimeFormatter" class.
*
* Copyright (C) 2002 Southern Storm Software, Pty Ltd.
*
* Contributions from Michael Moore <address@hidden>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace System.Private.DateTimeFormat
{
using System;
using System.Collections;
using System.Globalization;
using System.Text;
internal class DateTimeFormatter
{
public static String Format(DateTime date,
String format,
DateTimeFormatInfo info)
{
if(format.Length==1)
format=StandardFormatSpecifier(format);
StringBuilder builder=new StringBuilder(format.Length);
Queue q=new Queue();
if(info==null)
info=DateTimeFormatInfo.InvariantInfo;
FormatTemplate current=null;
current=new ExplicitString(); // marker for the start
bool literal=false;
foreach(char c in format)
{
if(literal)
{
if(!(current is ExplicitString))
{
q.Enqueue(current);
current=new ExplicitString();
}
(current as ExplicitString).Text+=c;
literal=false;
continue;
}
switch(c)
{
case 'd':
{
if(!(current is DayFormatter))
{
q.Enqueue(current);
//save the last node
current=new
DayFormatter();
}
(current as
DayFormatter).Count++;
break;
}
case 'f':
{
if(!(current is
FractionalSecondFormatter))
{
q.Enqueue(current);
//save the last node
current=new
FractionalSecondFormatter();
}
(current as
FractionalSecondFormatter).Count++;
break;
}
case 'g':
{
if(!(current is EraFormatter))
{
q.Enqueue(current);
//save the last node
current=new
EraFormatter();
}
(current as
EraFormatter).Count++;
break;
}
case 'h':
{
if(!(current is
TwelveHourFormatter))
{
q.Enqueue(current);
//save the last node
current=new
TwelveHourFormatter();
}
(current as
TwelveHourFormatter).Count++;
break;
}
case 'H':
{
if(!(current is
TwentyFourHourFormatter))
{
q.Enqueue(current);
//save the last node
current=new
TwentyFourHourFormatter();
}
(current as
TwentyFourHourFormatter).Count++;
break;
}
case 'm':
{
if(!(current is
MinuteFormatter))
{
q.Enqueue(current);
//save the last node
current=new
MinuteFormatter();
}
(current as
MinuteFormatter).Count++;
break;
}
case 'M':
{
if(!(current is MonthFormatter))
{
q.Enqueue(current);
//save the last node
current=new
MonthFormatter();
}
(current as
MonthFormatter).Count++;
break;
}
case 's':
{
if(!(current is
SecondFormatter))
{
q.Enqueue(current);
//save the last node
current=new
SecondFormatter();
}
(current as
SecondFormatter).Count++;
break;
}
case 't':
{
if(!(current is AMPMFormatter))
{
q.Enqueue(current);
//save the last node
current=new
AMPMFormatter();
}
(current as
AMPMFormatter).Count++;
break;
}
case 'y':
{
if(!(current is YearFormatter))
{
q.Enqueue(current);
//save the last node
current=new
YearFormatter();
}
(current as
YearFormatter).Count++;
break;
}
case 'z':
{
if(!(current is UTCFormatter))
{
q.Enqueue(current);
//save the last node
current=new
UTCFormatter();
}
(current as
UTCFormatter).Count++;
break;
}
case '/':
{
if(!(current is
DateSeparatorFormatter))
{
q.Enqueue(current);
//save the last node
current=new
DateSeparatorFormatter();
}
(current as
DateSeparatorFormatter).Count++;
break;
}
case ':':
{
if(!(current is
TimeSeparatorFormatter))
{
q.Enqueue(current);
//save the last node
current=new
TimeSeparatorFormatter();
}
(current as
TimeSeparatorFormatter).Count++;
break;
}
case '%':
{
//% is just a place holder so a
single char
// can be a custom formatter
break;
}
case '\\':
{
literal=true;
break;
}
default:
{
if(!(current is ExplicitString))
{
q.Enqueue(current);
current=new
ExplicitString();
}
(current as
ExplicitString).Text+=c;
break;
}
}
}
q.Enqueue(current);
foreach(Object x in q)
{
builder.Append((x as
FormatTemplate).GetString(date,info));
/* traverse the list and add the stuff */
}
return builder.ToString();
}
internal static String StandardFormatSpecifier(String format)
{
switch(format)
{
case "d":
{
return "MM/dd/yyyy";
}
case "D":
{
return "dddd, MMMM dd, yyyy";
}
case "f":
{
return "dddd, MMMM dd, yyyy HH:mm";
}
case "F":
{
return "dddd, MMMM dd, yyyy HH:mm:ss";
}
case "g":
{
return "MM/dd/yyyy HH:mm";
}
case "G":
{
return "MM/dd/yyyy HH:mm:ss";
}
case "m":
{
return "MMMM dd";
}
case "M":
{
return "MMMM dd";
}
case "t":
{
return "HH:mm";
}
case "T":
{
return "HH:mm:ss";
}
case "U":
{
return "dddd, MMMM dd, yyyy HH:mm:ss";
}
case "y":
{
return "yyyy MMMM";
}
case "Y":
{
return "yyyy MMMM";
}
default:
{
return format;
}
}
return format;
}
}
}
--- NEW FILE ---
/*
* DateTimeParser.cs - Implementation of the
* "System.Private.DateTimeFormat.DateTimeParser"
class.
*
* Copyright (C) 2002 Southern Storm Software, Pty Ltd.
*
* Contributions from Michael Moore <address@hidden>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace System.Private.DateTimeFormat
{
using System;
using System.Collections;
using System.Globalization;
internal class DateTimeParser
{
public static DateTime Parse(String s, DateTimeFormatInfo info,
DateTimeStyles style)
{
if(info==null) info=DateTimeFormatInfo.InvariantInfo;
String[] formatSpecifiers = new String[] {"d", "D",
"f", "F", "g",
"G", "m", "M", "t", "T", "U", "y", "Y"};
String[] infoPatterns = new String[]
{info.FullDateTimePattern,
info.LongDatePattern, info.LongTimePattern,
info.MonthDayPattern,
info.ShortDatePattern, info.ShortTimePattern,
info.YearMonthPattern};
int fsl = formatSpecifiers.Length;
int ipl = infoPatterns.Length;
String[] patterns = new String [fsl + ipl];
for( int i = 0; i < fsl; i++ )
patterns[i] =
DateTimeFormatter.StandardFormatSpecifier(formatSpecifiers[i]);
for( int i = fsl; i < fsl+ipl; i++ )
patterns[i] = infoPatterns[i-fsl];
DateTime result = new DateTime(0);
bool success=false;
for( int i = 0; (!success) && i < patterns.Length; i++ )
{
try
{
result=ParseExact(s, patterns[i], info,
style);
success=true; // it worked !
}
// catch day and month names which are > than
string indexes
catch(ArgumentOutOfRangeException){}
// catch failing to parse with any given pattern
catch(FormatException){}
}
if(!success)
{
// throw FormatException if no default pattern
matched s
throw new FormatException("No DateTime pattern
parsed correctly");
}
else
return result;
}
public static DateTime ParseExact(String s, String[] patterns,
DateTimeFormatInfo info, DateTimeStyles
style)
{
if(info==null)
info=DateTimeFormatInfo.InvariantInfo;
DateTime result = new DateTime(0);
bool success=false;
for( int i=0; (!success) && i < patterns.Length; i++ )
{
try
{
result=ParseExact(s, patterns[i], info,
DateTimeStyles.NoCurrentDateDefault);
success=true;
}
// catch matching name, months that are out of
range
catch ( ArgumentOutOfRangeException ){}
// catch parses that do not succed
catch ( FormatException ){}
}
if(!success)
{
throw new FormatException("No DateTime pattern
parsed correctly");
}
else
return result;
}
public static DateTime ParseExact(String s, String format,
DateTimeFormatInfo info, DateTimeStyles
style)
{
if( (format.Length) == 1 )
format=DateTimeFormatter.StandardFormatSpecifier(format);
Queue q=new Queue();
if(info==null)
info=DateTimeFormatInfo.InvariantInfo;
FormatTemplate current=null;
current=new ExplicitString(); //mark the start
bool literal=false;
foreach(char c in format)
{
if(literal) {
if(!(current is ExplicitString))
{
q.Enqueue(current);
current=new ExplicitString();
}
(current as ExplicitString).Text+=c;
literal=false;
continue;
}
switch(c)
{
case 'd':
{
if(!(current is DayFormatter))
{
q.Enqueue(current);
//save the last node
current=new
DayFormatter();
}
(current as
DayFormatter).Count++;
break;
}
case 'f':
{
if(!(current is
FractionalSecondFormatter))
{
q.Enqueue(current);
//save the last node
current=new
FractionalSecondFormatter();
}
(current as
FractionalSecondFormatter).Count++;
break;
}
case 'g':
{
if(!(current is EraFormatter))
{
q.Enqueue(current);
//save the last node
current=new
EraFormatter();
}
(current as
EraFormatter).Count++;
break;
}
case 'h':
{
if(!(current is
TwelveHourFormatter))
{
q.Enqueue(current);
//save the last node
current=new
TwelveHourFormatter();
}
(current as
TwelveHourFormatter).Count++;
break;
}
case 'H':
{
if(!(current is
TwentyFourHourFormatter))
{
q.Enqueue(current);
//save the last node
current=new
TwentyFourHourFormatter();
}
(current as
TwentyFourHourFormatter).Count++;
break;
}
case 'm':
{
if(!(current is
MinuteFormatter))
{
q.Enqueue(current);
//save the last node
current=new
MinuteFormatter();
}
(current as
MinuteFormatter).Count++;
break;
}
case 'M':
{
if(!(current is MonthFormatter))
{
q.Enqueue(current);
//save the last node
current=new
MonthFormatter();
}
(current as
MonthFormatter).Count++;
break;
}
case 's':
{
if(!(current is
SecondFormatter))
{
q.Enqueue(current);
//save the last node
current=new
SecondFormatter();
}
(current as
SecondFormatter).Count++;
break;
}
case 't':
{
if(!(current is AMPMFormatter))
{
q.Enqueue(current);
//save the last node
current=new
AMPMFormatter();
}
(current as
AMPMFormatter).Count++;
break;
}
case 'y':
{
if(!(current is YearFormatter))
{
q.Enqueue(current);
//save the last node
current=new
YearFormatter();
}
(current as
YearFormatter).Count++;
break;
}
case 'z':
{
if(!(current is UTCFormatter))
{
q.Enqueue(current);
//save the last node
current=new
UTCFormatter();
}
(current as
UTCFormatter).Count++;
break;
}
case '/':
{
if(!(current is
DateSeparatorFormatter))
{
q.Enqueue(current);
//save the last node
current=new
DateSeparatorFormatter();
}
(current as
DateSeparatorFormatter).Count++;
break;
}
case ':':
{
if(!(current is
TimeSeparatorFormatter))
{
q.Enqueue(current);
//save the last node
current=new
TimeSeparatorFormatter();
}
(current as
TimeSeparatorFormatter).Count++;
break;
}
case '%':
{
//% is just a place holder so a
single char
// can be a custom formatter
break;
}
case '\\':
{
literal=true;
break;
}
default:
{
q.Enqueue(current);
current=new ExplicitString();
break;
}
}
}
q.Enqueue(current);
ParsedDateTime d= new ParsedDateTime();
int start = -1;
foreach(FormatTemplate temp in q)
{
start = temp.Parse(s, start, info); // parse or
throw error
}
if(start!=s.Length)
{
throw new FormatException("Input string not
fully consumed");
}
foreach(FormatTemplate temp in q)
{
d=temp.StoreTo(d);
}
DateTime stored = d.storeInDateTime(style);
return stored;
}
}
}
--- NEW FILE ---
/*
* ParsedDateTime.cs - Implementation of the
* "System.Private.DateTimeFormat.ParsedDateTime"
class.
*
* Copyright (C) 2002 Southern Storm Software, Pty Ltd.
*
* Contributions from Michael Moore <address@hidden>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace System.Private.DateTimeFormat
{
using System;
using System.Globalization;
internal class ParsedDateTime
{
bool hasDay = false;
bool hasMonth = false;
bool hasYear = false;
bool hasTwentyfourHour = false;
bool hasTwelveHour = false;
bool hasMinute = false;
bool hasSecond = false;
bool hasAMPM = false;
bool hasFractionalSecond = false;
bool hasStyle = false;
bool hasTicks = false;
bool hasUTC = false;
int day;
int month;
int yr;
int hr;
int min;
int sec;
int ap;
long fs;
internal DateTime storeInDateTime(DateTimeStyles style)
{
bool hasDate = hasDay && hasMonth && hasYear;
bool hasTime = (hasTwentyfourHour || hasTwelveHour) &&
hasMinute;
DateTime new_dt;
if( style == DateTimeStyles.NoCurrentDateDefault)
{
hasStyle=true;
}
if( hasUTC && (hasDate || hasTime ) )
throw new FormatException("Cannot set UTC and
other time value");
if( hasAMPM && hasTwelveHour)
{
// time is in 12 hour format and set to PM, add
12, mod 24
if( (this.TwelveHour<13) && (this.AMPM==1) )
hr=((this.TwelveHour+12)%24);
}
// begin picking which items to construct datetime
object with
if( hasUTC )
{
if( hasStyle )
return new_dt = new DateTime(1, 1, 1,
hr, this.Minute, 0 );
return new_dt = new DateTime(this.Year,
this.Month, this.Day,
hr, this.Minute, this.Second);
}
else if( hasDate && !hasTime )
{
// set date of object, time is 00:00:00
new_dt = new DateTime(this.Year, this.Month,
this.Day);
}
else if( hasTime && !hasDate )
{
if( hasStyle )
{
this.Year=1;
this.Month=1;
this.Day=1;
new_dt = new DateTime(this.Year,
this.Month, this.Day, hr,
this.Minute, this.Second);
}
else
{
new_dt = new
DateTime((DateTime.Now).Year, (DateTime.Now).Month,
(DateTime.Now).Day, hr,
this.Minute, this.Second);
}
}
else if( hasDate && hasTime )
{
new_dt = new DateTime(this.Year, this.Month,
this.Day, hr,
this.Minute, this.Second);
}
else if( hasDay && hasMonth )
{
new_dt = new DateTime((DateTime.Now).Year,
this.Month, this.Day);
}
else if( hasTwentyfourHour && hasMinute )
{
new_dt = new DateTime((DateTime.Now).Year,
(DateTime.Now).Month,
(DateTime.Now).Day, hr, this.Minute,
(DateTime.Now).Second);
}
else if( hasTwentyfourHour && hasMinute )
{
new_dt = new DateTime((DateTime.Now).Year,
(DateTime.Now).Month,
(DateTime.Now).Day, hr, this.Minute,
(DateTime.Now).Second);
}
else if( hasYear && hasMonth )
{
new_dt = new DateTime(this.Year, this.Month,
(DateTime.Now).Day);
}
else
{
throw new FormatException("Could not store
parsed date");
}
if( hasFractionalSecond )
new_dt = new_dt.Add(new TimeSpan(fs) );
return new_dt;
}
internal int Day
{
set
{
if(value<=0)
throw new FormatException("Invalid Day
value");
hasDay = true;
day=value;
}
get
{
return day;
}
}
internal int Month
{
set
{
if(value<=0)
throw new FormatException("Invalid
Month value");
hasMonth=true;
month=value;
}
get
{
return month;
}
}
internal int Year
{
set
{
hasYear=true;
yr=value;
}
get
{
return yr;
}
}
internal int TwentyFourHour
{
set
{
if( (value<0) || (value>24) )
throw new
FormatException("TwentyFourHour value out of range");
hasTwentyfourHour=true;
hr=value;
}
get
{
return hr;
}
}
internal int TwelveHour
{
set
{
if( (value<0) || (value>12) )
throw new FormatException("TwelveHour
value out of range");
hasTwelveHour=true;
hr=value;
}
get
{
return hr;
}
}
internal int AMPM
{
// 0 == AM, 1==PM
set
{
hasAMPM=true;
ap = value;
}
get
{
return ap;
}
}
internal int Minute
{
set
{
if( (value<0) || (value>60) )
throw new FormatException("Minute value
out of range");
hasMinute=true;
min=value;
}
get
{
return min;
}
}
internal int Second
{
set
{
if( (value<0) || (value>60) )
throw new FormatException("Second value
out of range");
hasSecond=true;
sec=value;
}
get
{
return sec;
}
}
internal long FractionalSecond
{
set
{
hasFractionalSecond=true;
fs=value;
}
get
{
return fs;
}
}
internal bool UTC
{
set
{
hasUTC=value;
}
get
{
return hasUTC;
}
}
}
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Private/DateTimeFormat DateTimeFormatTemplate.cs,NONE,1.1 DateTimeFormatter.cs,NONE,1.1 DateTimeParser.cs,NONE,1.1 ParsedDateTime.cs,NONE,1.1,
Gopal.V <address@hidden> <=
- Prev by Date:
[Dotgnu-pnet-commits] CVS: pnetlib ChangeLog,1.668,1.669
- Next by Date:
[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Private/NumberFormat Formatter.cs,1.2,1.3 GeneralFormatter.cs,1.4,1.5 HexadecimalFormatter.cs,1.2,1.3
- Previous by thread:
[Dotgnu-pnet-commits] CVS: pnetlib ChangeLog,1.668,1.669
- Next by thread:
[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Private/NumberFormat Formatter.cs,1.2,1.3 GeneralFormatter.cs,1.4,1.5 HexadecimalFormatter.cs,1.2,1.3
- Index(es):