Archives de septembre 2005
Algorithme pour calculer Pâques
///
/// Algorithm for calculating the date of Easter Sunday
/// (Meeus/Jones/Butcher Gregorian algorithm)
/// http://en.wikipedia.org/wiki/Computus#Meeus.2FJones.2FButcher_Gregorian_algorithm
///
/// A valid Gregorian year
/// Easter Sunday
public static DateTime EasterDate(int year) {
int Y = year;
int a = Y % 19;
int b = Y / 100;
int c = Y % 100;
int d = b / 4;
int e = b % 4;
int f = (b +
/ 25;
int g = (b - f + 1) / 3;
int h = (19 * a + b - d - g + 15) % 30;
int i = c / 4;
int k = c % 4;
int L = (32 + 2 * e + 2 * i - h - k) % 7;
int m = (a + 11 * h + 22 * L) / 451;
int month = (h + L - 7 * m + 114) / 31;
int day = ((h + L - 7 * m + 114) % 31) + 1;
DateTime dt = new DateTime(year, month, day);
return dt;
}
- Lundi de Pâques : Easter Monday = Easter Sunday + 1
- Jeudi de l’Ascension : Ascension Day = Easter Sunday + 39
- Dimanche de Pentecôte : Pentecost Sunday = Easter Sunday + 49
- Lundi de Pentecôte : Pentecost Monday = Easter Sunday + 50
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/765)
Définir une valeur Identity sous Sql Server
SET IDENTITY_INSERT MyTable ON;
INSERT INTO MyTable
(MyIdentityField, MyFirstField, MySecondField)
VALUES
(12345, 'ABCDE', 'etc...');
SET IDENTITY_INSERT MyTable OFF;
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/755)
Literal DateTime pour SqlServer
CONVERT(DATETIME, 'yyyymmdd', 112)
112 -> ISO date cf. CAST and CONVERT
Edit : une bien meilleure solution de Wild Richard :
{d 'yyyy-mm-dd'}
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/754)
Capitalization
D’après C# Regular Expressions :
using System.Text.RegularExpressions;
public class MyClass {
public static void Main() {
string text = "the quick red fox jumped over the lazy brown DOG.";
System.Console.WriteLine("text=[" + text + "]");
string result = Regex.Replace(text, @"w+", new MatchEvaluator(MyClass.CapText));
System.Console.WriteLine("result=[" + result + "]");
System.Console.ReadLine();
}
static string CapText(Match m) {
string temp = m.ToString();
temp = char.ToUpper(temp[0]) + temp.Substring(1, temp.Length - 1).ToLower();
return temp;
}
}
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/705)
Edit : commentaire de utagger :
Here’s a shorter version:
protected void Button1_Click(object sender, EventArgs e) {
Label1.Text = Regex.Replace(TextBox1.Text, @”\b\w”, new MatchEvaluator(stam));
}protected string stam(Match m) {
return m.Value.ToUpper();
}(the trick is using \b which is a 0-length match of word boundaries, including ^ and \s)