[Mono-list] System.TimeZone
Dwivedi , Ajay Kumar
AjayKumar.Dwivedi@dresdner-bank.com
Thu, 14 Feb 2002 14:52:24 -0000
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_000_01C1B567.369A8600
Content-Type: text/plain;
charset="iso-8859-1"
Hi all,
Here are System.TimeZone and System.CurrentTimeZone classes.
CurrentTimeZone class uses some hardcoded values for Testing purposes.
Regards,
Ajay
> How large is it?
> Send it to the list so ppl can review it.
------_=_NextPart_000_01C1B567.369A8600
Content-Type: application/octet-stream;
name="CurrentTimeZone.cs"
Content-Disposition: attachment;
filename="CurrentTimeZone.cs"
//Author : Dwivedi, Ajay kumar
//Email : adwiv@yahoo.com
using System.Globalization;
using System.Collections;
namespace System
{
/// <summary>
/// This inner class is implementation of timezone for the users machine.
/// </summary>
/// <remarks>This class is returned by CurrentTimeZone Property of System.TimeZone.</remarks>
/// <todo> FIXME: What about Thread Synchronization during native calls? </todo>
internal class CurrentTimeZone : TimeZone
{
/// <summary>
/// A yearwise cache of DaylightTime. Filled as needed. The current year
/// is filled by the constuctor.
/// </summary>
private static System.Collections.Hashtable daylightCache = new Hashtable(1);
/// <summary>
/// utcOffsetWithOutDLS represents the offset when daylightsaving is not on.
/// </summary>
private static TimeSpan utcOffsetWithOutDLS;
/// <summary>
/// utcOffsetWithDLS represents the offset when daylightsaving is on.
/// </summary>
private static TimeSpan utcOffsetWithDLS;
/// <summary>
/// Daylight Name. Empty if no DaylightSaving.
/// </summary>
private static string daylightName;
private static string standardName;
internal CurrentTimeZone()
:base()
{
//TODO: call some system method and fill all the fields.
//Doing Some Hardcoding for Testing purposes
// /me in Vladivostok? Nah.. Just for DaylightSaving Sake.
utcOffsetWithOutDLS = new TimeSpan(10,0,0);
utcOffsetWithDLS = new TimeSpan(11,0,0);
daylightName = "Vladivostok Daylight Time";
standardName = "Vladivostok Standard Time";
DaylightTime dlt = new DaylightTime(new DateTime(2002,3,31,2,0,0),
new DateTime(2002,10,27,3,0,0),new TimeSpan(1,0,0));
daylightCache.Add(2002,dlt);
}
public override string DaylightName
{
get
{
return daylightName;
}
}
public override string StandardName
{
get
{
return standardName;
}
}
public override DaylightTime GetDaylightChanges(int year)
{
if(year < 1 || year > 9999)
throw new ArgumentOutOfRangeException("year",year,"Range must be between 1 and 9999");
if(daylightCache[year]==null)
{
//TODO: do something and put the DaylightTime corresponding to the time in cache.
DaylightTime dlt = new DaylightTime(new DateTime(0),
new DateTime(0),new TimeSpan(0));
daylightCache.Add(year,dlt);
}
return (DaylightTime) daylightCache[year];
}
public override TimeSpan GetUtcOffset(DateTime time)
{
if(IsDaylightSavingTime(time))
{
return utcOffsetWithDLS;
}
return utcOffsetWithOutDLS;
}
}
}
------_=_NextPart_000_01C1B567.369A8600
Content-Type: application/octet-stream;
name="TimeZone.cs"
Content-Disposition: attachment;
filename="TimeZone.cs"
//Author : Dwivedi, Ajay kumar
//Email : adwiv@yahoo.com
using System.Globalization;
namespace System
{
/// <summary>
/// Summary description for TimeZone.
/// </summary>
public abstract class TimeZone
{
/// <summary>
/// This field stores the current TimeZone;
/// </summary>
private static TimeZone currentTimeZone;
/// <summary>
/// Initializes a new instance of the TimeZone class.
/// </summary>
/// <remarks>Does nothing</remarks>
protected TimeZone()
{}
public abstract string DaylightName {get;}
public abstract string StandardName {get;}
public abstract DaylightTime GetDaylightChanges(int year);
public abstract TimeSpan GetUtcOffset(DateTime time);
/// <summary>
/// Returns the TimeZone Class for the Systems TimeZone
/// </summary>
public static TimeZone CurrentTimeZone
{
get
{
//FIXME: What is the probability that local timezone may change
if(currentTimeZone == null)
{
currentTimeZone = new CurrentTimeZone();
}
return currentTimeZone;
}
}
/// <summary>
/// Returns a value indicating whether the specified date and time is
/// within the daylight saving time periods associated with that date.
/// </summary>
public static bool IsDaylightSavingTime(DateTime time,DaylightTime daylightTimes)
{
//FIXME: Are these start and End inclusive in the DaylightSaving?
// Who cares.. Its just a 0.1 microsecond.
if(daylightTimes == null)
throw new ArgumentNullException("daylightTimes");
// If Start==End, as in the case of No daylight saving
if(daylightTimes.Start.Ticks == daylightTimes.End.Ticks)
{
return false;
}
else if(daylightTimes.Start.Ticks < daylightTimes.End.Ticks)
{ //We are in northern hemisphere.
// If time lies between Start and End, daylight saving is on
if(daylightTimes.Start.Ticks < time.Ticks && daylightTimes.End.Ticks > time.Ticks)
return true;
}
else if(daylightTimes.Start.Ticks > daylightTimes.End.Ticks) // if is Redundant here
{ // We are in southern hemisphere.
// If years are same, and If time is less than End OR
// more than Start daylight saving is on
if(time.Year == daylightTimes.Start.Year && time.Year == daylightTimes.End.Year)
{
if( time.Ticks < daylightTimes.End.Ticks || time.Ticks > daylightTimes.Start.Ticks)
return true;
}
}
return false;
}
/// <summary>
/// Returns a value indicating whether the specified date and time is
/// within the daylight saving time periods associated with that date.
/// </summary>
public virtual bool IsDaylightSavingTime(DateTime time)
{
return IsDaylightSavingTime(time, GetDaylightChanges(time.Year));
}
/// <summary>
/// Returns the local time that corresponds to a specified coordinated universal time (UTC).
/// </summary>
/// <param name="time">A DateTime instance that specifies the UTC time.</param>
/// <returns>A DateTime instance whose value is the local time that corresponds to time.</returns>
public virtual DateTime ToLocalTime(DateTime time)
{
return new DateTime (time.Ticks + GetUtcOffset(time).Ticks);
}
/// <summary>
/// Returns the Universal time that corresponds to a specified local time (UTC).
/// </summary>
/// <param name="time">A DateTime instance that specifies the local time.</param>
/// <returns>A DateTime instance whose value is the UTC time that corresponds to time.</returns>
public virtual DateTime ToUniversalTime(DateTime time)
{
return new DateTime(time.Ticks - GetUtcOffset(time).Ticks);
}
}
}
------_=_NextPart_000_01C1B567.369A8600--