michelc Blog

Just <strong>another</strong> WordPress.com weblog

Archiver dans la catégorie ‘c#

Retrouver un numéro de semaine

sans commentaires

public static int GetWeekNumber (DateTime dt) {
	CultureInfo culture = CultureInfo.CurrentCulture;
	int intWeek = culture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
	return intWeek;
}

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/856)

Rédigé par michel

4 novembre 2005 à 4:39

Publié dans Code Snippets, c#

Convertir un type String en Enum

sans commentaires

enum EngineType {
	unknow,
	access,
	db2,
	mysql,
	odbc,
	oledb,
	oracle,
	postgre,
	sqlserver
}
string cnxTypeString = "mysql";
EngineType cnxTypeEngine = EngineType.unknow;
if (Enum.IsDefined(typeof(EngineType), cnxTypeString)) {
	cnxTypeEngine = (EngineType) Enum.Parse(typeof(EngineType), cnxTypeString, true);
}

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/824)

Rédigé par michel

20 octobre 2005 à 1:15

Publié dans Code Snippets, c#

Algorithme pour calculer Pâques

sans commentaires

///
/// 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 + 8) / 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)

Rédigé par michel

27 septembre 2005 à 7:29

Publié dans Code Snippets, c#

Capitalization

sans commentaires

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)

Rédigé par michel

14 septembre 2005 à 4:58

Publié dans Code Snippets, c#, regex

yyyymmdd to DateTime

sans commentaires

DateTime myDate;

myDate = System.DateTime.ParseExact("20050802",

                                    "yyyyMMdd",

                                    System.Globalization.CultureInfo.InvariantCulture);

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/541)

Rédigé par michel

2 août 2005 à 4:56

Publié dans Code Snippets, c#

DateTime to yyyymmdd

sans commentaires

string myString = myDate.ToString("yyyyMMdd");

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/540)

Rédigé par michel

2 août 2005 à 4:53

Publié dans Code Snippets, c#

Set ValueToCompare at run time

sans commentaires

Lorsque un contrôle CompareValidator est utilisé pour contrôler une date et que la valeur de comparaison est définie côté code, il faut formatter cette date au format de date courte :

myCompareValidator.ValueToCompare = myDateTime.ToShortDateString();

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/524)

Rédigé par michel

29 juillet 2005 à 5:33

Publié dans Code Snippets, c#

Générer une chaine aléatoire

sans commentaires

///
/// Build a random string (for id, login, password...)
///
public static string randomString() {
int length = new Random().Next(6, 10);
return randomString(length);
}

///
/// Build a random string (for id, login, password...)
///
public static string randomString(int length) {
string tempString = Guid.NewGuid().ToString().ToLower();
tempString = tempString.Replace("-", "");
while (tempString.Length

(Publié à l'origine sur http://www.bigbold.com/snippets/posts/show/518)

Rédigé par michel

27 juillet 2005 à 5:20

Publié dans Code Snippets, c#

Encoder une chaine en SHA1 et hexa

sans commentaires

public static string SHA1_ComputeHexaHash (string text) {
// Gets the SHA1 hash for text
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(text);
byte[] hash = sha1.ComputeHash(data);
// Transforms as hexa
string hexaHash = "";
foreach (byte b in hash) {
hexaHash += String.Format("{0:x2}", b);
}
// Returns SHA1 hexa hash
return hexaHash;
}

(publié à l’origine sur http://www.bigbold.com/snippets/posts/show/517)

Rédigé par michel

27 juillet 2005 à 5:18

Publié dans Code Snippets, c#

Encoder une chaine en MD5 et hexa

sans commentaires

public static string MD5_ComputeHexaHash (string text) {
// Gets the MD5 hash for text
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(text);
byte[] hash = md5.ComputeHash(data);
// Transforms as hexa
string hexaHash = "";
foreach (byte b in hash) {
hexaHash += String.Format("{0:x2}", b);
}
// Returns MD5 hexa hash
return hexaHash;
}

(publié à l’origine sur http://www.bigbold.com/snippets/posts/show/516)

Rédigé par michel

27 juillet 2005 à 4:45

Publié dans Code Snippets, c#