C# 3.0 基本框架
使用TimeZoneInfo:
static void Main()
{
TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById
("W. Australia Standard Time"); Console.WriteLine (wa.Id); // W. Australia Standard Time
Console.WriteLine (wa.DisplayName); // (GMT+08:00) Perth
Console.WriteLine (wa.BaseUtcOffset); // 08:00:00
Console.WriteLine (wa.SupportsDaylightSavingTime); // True foreach (TimeZoneInfo.AdjustmentRule rule in wa.GetAdjustmentRules())
{
Console.WriteLine ("Rule: applies from " + rule.DateStart +
" to " + rule.DateEnd); Console.WriteLine (" Delta: " + rule.DaylightDelta); Console.WriteLine (" Start: " + FormatTransitionTime
(rule.DaylightTransitionStart, false)); Console.WriteLine (" End: " + FormatTransitionTime
(rule.DaylightTransitionEnd, true));
Console.WriteLine();
}
} static string FormatTransitionTime (TimeZoneInfo.TransitionTime tt,
bool endTime)
{
if (endTime && tt.IsFixedDateRule
&& tt.Day == 1 && tt.Month == 1
&& tt.TimeOfDay == DateTime.MinValue)
return "-"; string s;
if (tt.IsFixedDateRule)
s = tt.Day.ToString();
else
s = "The " +
"first second third fourth last".Split() [tt.Week - 1] +
" " + tt.DayOfWeek + " in"; return s + " " + DateTimeFormatInfo.CurrentInfo.MonthNames [tt.Month-1]
+ " at " + tt.TimeOfDay.TimeOfDay;
}
DateTime和daylight保存:
DaylightTime changes = TimeZone.CurrentTimeZone.GetDaylightChanges (2008);
TimeSpan halfDelta = new TimeSpan (changes.Delta.Ticks / 2); DateTime utc1 = changes.End.ToUniversalTime() - halfDelta;
DateTime utc2 = utc1 - changes.Delta; // Converting these variables to local times demonstrates why you should use
// UTC and not local time if your code relies on time moving forward: DateTime loc1 = utc1.ToLocalTime(); // (Pacific Standard Time)
DateTime loc2 = utc2.ToLocalTime();
Console.WriteLine (loc1); // 2/11/2008 1:30:00 AM
Console.WriteLine (loc2); // 2/11/2008 1:30:00 AM
Console.WriteLine (loc1 == loc2); // True Console.Write (loc1.ToString ("o")); // 2008-11-02T02:30:00.0000000-08:00
Console.Write (loc2.ToString ("o")); // 2008-11-02T02:30:00.0000000-07:00 Console.WriteLine (loc1.ToUniversalTime() == utc1); // True
Console.WriteLine (loc2.ToUniversalTime() == utc2); // True
写一个自定义格式provider:
public class WordyFormatProvider : IFormatProvider, ICustomFormatter
{
static readonly string[] _numberWords =
"zero one two three four five six seven eight nine minus point".Split(); IFormatProvider _parent; // Allows consumers to chain format providers public WordyFormatProvider() : this (CultureInfo.CurrentCulture) { }
public WordyFormatProvider (IFormatProvider parent)
{
_parent = parent;
} public object GetFormat (Type formatType)
{
if (formatType == typeof (ICustomFormatter)) return this;
return null;
} public string Format (string format, object arg, IFormatProvider prov)
{
// If it's not our format string, defer to the parent provider:
if (arg == null || format != "W")
return string.Format (_parent, "{0:" + format + "}", arg); StringBuilder result = new StringBuilder();
string digitList = string.Format (CultureInfo.InvariantCulture,
"{0}", arg);
foreach (char digit in digitList)
{
int i = "0123456789-.".IndexOf (digit);
if (i == -1) continue;
if (result.Length > 0) result.Append (' ');
result.Append (_numberWords[i]);
}
return result.ToString();
}
}
重载相等语义:
public struct Area : IEquatable <Area>
{
public readonly int Measure1;
public readonly int Measure2; public Area (int m1, int m2)
{
Measure1 = m1;
Measure2 = m2;
} public override bool Equals (object other)
{
if (!(other is Area)) return false;
return Equals ((Area) other); // Calls method below
} public bool Equals (Area other) // Implements IEquatable<Area>
{
return Measure1 == other.Measure1 && Measure2 == other.Measure2
|| Measure1 == other.Measure2 && Measure2 == other.Measure1;
} public override int GetHashCode()
{
if (Measure1 > Measure2)
return Measure1 * 37 + Measure2; // 37 = a prime number
else
return Measure2 * 37 + Measure1;
} public static bool operator == (Area a1, Area a2)
{
return a1.Equals (a2);
} public static bool operator != (Area a1, Area a2)
{
return !a1.Equals (a2);
}
}
实现IComparable接口:
public struct Note : IComparable<Note>, IEquatable<Note>, IComparable
{
int _semitonesFromA; public Note (int semitonesFromA)
{
_semitonesFromA = semitonesFromA;
} public int CompareTo (Note other) // Generic IComparable<T>
{
if (Equals (other)) return 0; // Fail-safe check
return _semitonesFromA.CompareTo (other._semitonesFromA);
} int IComparable.CompareTo (object other) // Nongeneric IComparable
{
if (!(other is Note))
throw new InvalidOperationException ("CompareTo: Not a note");
return CompareTo ((Note) other);
} public static bool operator < (Note n1, Note n2)
{
return n1.CompareTo (n2) < 0;
} public static bool operator > (Note n1, Note n2)
{
return n1.CompareTo (n2) > 0;
} public bool Equals (Note other) // for IEquatable<Note>
{
return _semitonesFromA == other._semitonesFromA;
} public override bool Equals (object other)
{
if (!(other is Note)) return false;
return Equals ((Note) other);
} public override int GetHashCode()
{
return _semitonesFromA.GetHashCode();
} public static bool operator == (Note n1, Note n2)
{
return n1.Equals (n2);
} public static bool operator != (Note n1, Note n2)
{
return !(n1 == n2);
}
}
C# 3.0 基本框架的更多相关文章
- Yii2.0高级框架数据库增删改查的一些操作(转)
		yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ... 
- 使用DotNetOpenAuth搭建OAuth2.0授权框架
		标题还是一如既往的难取. 我认为对于一个普遍问题,必有对应的一个简洁优美的解决方案.当然这也许只是我的一厢情愿,因为根据宇宙法则,所有事物总归趋于混沌,而OAuth协议就是混沌中的产物,不管是1.0. ... 
- Yii2.0高级框架数据库增删改查的一些操作
		yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ... 
- Tao 1.2.0图形框架发布
		Tao 1.2.0图形框架发布 Tao图形框架是方便在Mono和.Net环境下进行游戏相关开发的库绑定和实用工具集.目前,对以下库提供支持: Cg - [Cg website] Dev ... 
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之四 || Swagger的使用 3.2
		前言 如果想直接在域名的根目录直接加载 swagger 比如访问:localhost:8001 就能访问,可以这样设置: app.UseSwaggerUI(c => { c.SwaggerEnd ... 
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十二 || 三种跨域方式比较,DTOs(数据传输对象)初探
		更新反馈 1.博友@落幕残情童鞋说到了,Nginx反向代理实现跨域,因为我目前还没有使用到,给忽略了,这次记录下,为下次补充.此坑已填 2.提示:跨域的姊妹篇——<三十三║ ⅖ 种方法实现完美跨 ... 
- OAuth 2.0 RFC 框架 中文
		Internet Engineering Task Force (IETF) D. Hardt, Ed.Request for Comments: 6749 MicrosoftObsoletes: 5 ... 
- Chisel辅助iOS 应用程序调试,MusicApp模仿酷狗4.0 UI框架
		本文转载至 http://www.cocoachina.com/ios/20140825/9446.html Chisel Chisel集合了大量的LLDB 命令来辅助iOS 应用程序调试,并支持添 ... 
- Z从壹开始前后端分离【 .NET Core2.0/3.0 +Vue2.0 】框架之四 || Swagger的使用 3.2
		本文梯子 本文3.0版本文章 前言 一.swagger的一般用法 0.设置swagger页面为首页——开发环境 1.设置默认直接首页访问 —— 生产环境 2.为接口添加注释 3.对 Model 也添加 ... 
- OAuth 2.0授权框架详解
		目录 简介 OAuth的构成 refresh Token Authorization Code模式 隐式授权 Resource Owner 授权密码认证 Client 认证授权 github的OAut ... 
随机推荐
- 【poj2478】Farey Sequence
			题意: 求前n项的欧拉函数之和 题解: 预处理出所有欧拉函数 赤裸裸的模版题- - 没什么好说的 代码: #include <cstdio> typedef long long ll; ; ... 
- A Tour of Go  Errors
			An error is anything that can describe itself as an error string. The idea is captured by the predef ... 
- hdu 1034 Candy Sharing Game
			Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ... 
- HDU 4628
			这是一个大水题啊... 因为比赛时不会算复杂度耽误半天. i从0到2^n枚举集合i的所有分割两半的情况的复杂度为O(3^n),可以想象这个过程相当于是给每个位标记0,1,2(0表示不选,1,2表示两个 ... 
- ASP.NET奇葩说
			1.form表单 在asp.net中(即runat="server"特性时)method默认值为post 在html中默认是get. 2.Web应用程序项目和网站项目:前者后台文 ... 
- 使用ttXactAdmin、ttSQLCmdCacheInfo、ttSQLCmdQueryPlan获取SQL相关具体信息[TimesTen运维]
			使用ttXactAdmin.ttSQLCmdCacheInfo.ttSQLCmdQueryPlan获取SQL相关具体信息,适合于tt11以上版本号. $ ttversion TimesTen Rele ... 
- 详解Android Handler的使用
			我们进行Android开发时,Handler可以说是使用非常频繁的一个概念,它的用处不言而喻.本文就详细介绍Handler的基本概念和用法. Handler的基本概念 Handler主 ... 
- [Python]linux自己定义Python脚本命令
			在window下写好的程序配置到Linux上,要实现随意文件夹下的命令调用. 因为初学Linux,这里从文件传输等最主要的方法入手,记录配置的过程中遇到的各种问题. 连接远端server 这里使用pu ... 
- android134 360 07  归属地查询
			<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ... 
- string与数值之间的转换
			9.50 编写程序处理一个vector<string>,其元素都表示整数型.计算vector中所有元素之和.修改程序,使之计算表示浮点值的string之和. 程序如下: #include& ... 
