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 ...
随机推荐
- poj 1704 阶梯博弈
转自http://blog.sina.com.cn/s/blog_63e4cf2f0100tq4i.html 今天在POJ做了一道博弈题..进而了解到了阶梯博弈...下面阐述一下我对于阶梯博弈的理解. ...
- nodejs学习:sails框架的学习
上周通过搭建CMS系统接触到了sails框架,知道一些ORM的概念.这周开始深入后台数据交互,发现twenty框架的数据结构在sails上又设计了一番(比如node.category),不得不说师哥就 ...
- SQL2008--SQL语句-存储过程-触发器-事务处理-基本语法-函数
环境 SQL2008 1.存储过程建立 库-可编程性-存储过程-右击“存储过程”-点击“新建存储过程” SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCRE ...
- 浅析作用域链–JS基础核心之一
JS中的作用域,大家都知道的,分为全局作用域和局部作用域,没有块级作用域,听起来其实很简单的,可是作用域是否能够有深入的了解,对于JS代码逻辑的编写成功率,BUG的解决能力,以及是否能写出更优秀的代码 ...
- Eclipse能否把选中的一段代码向前或向后缩进一个tab的位
选中代码按TAB /shift + tab
- C++面向对象三大特性
面向对象三大特性 继承 public protected private public继承 public protected 不可见 private继承 private private 不可见 pro ...
- cocos2d-x中CCTextureCache图片资源的异步加载<转>
如果没有预先加载图片,则可以通过addImageAsync()函数实现异步加载,该函数通过创建一个加载线程来加载图片,并且在主线程中通过调用回调函数来读取该图片资源纹理.其主要过程如下: 1.创建线程 ...
- Android上实现仿IOS弹性ScrollView
[转]http://www.tuicool.com/articles/ummIJb
- Castle IOC FOR MVC 使用方法
Castle Web.API 使用方法 一.创建 WindsorActivator 继承 IHttpControllerActivator public class WindsorActivator ...
- [GIF] Parenting in GIF Loop Coder
In this lesson, we look at how you can build up complex animations by assigning one shape as the par ...