• geoffrey (unregistered)

    This was obviously done to make the code portable across platforms. This is a crucial requirement of an enterprise system.

  • Chuck (unregistered)

    Not Frist.

  • (cs)

    ...or to anticipate for when we add an 8th day of the week.

  • (cs)

    This is not portable in other languages/countries where the first day of the week may not be sunday..

  • Chronomium (unregistered)
    (int) DateTime.Now.DayOfWeek
    Given that the pairs in the hash table are things like ("Sunday", "0"), it's more like:
    ((int) DateTime.Now.DayOfWeek).ToString()
    ...which has to be re-int'd anyway given that you probably want to use the result as a number.
  • Some Jerk (unregistered)

    Ummm... WOW! I believe what we have here is a new IdiotSavant<Idiot>();

  • C-Derb (unregistered)

    Safe to assume DRX and OBL "commonly" have performance issues?

  • M (unregistered)

    I especially love how it creates a new hashtable each time. Actually I'm surprised someone who would write this is able to use a hashtable in the first place.

  • wonk (unregistered)

    Obviously, this code was offshored to Turkmenistan:

    http://en.wikipedia.org/wiki/Renaming_of_Turkmen_months_and_days_of_week,_2002

  • Some Jerk (unregistered) in reply to M

    I suspect that this individual must have been looking at source code and attempting to discern its' purpose. Apparently, this individual did not understand what they were doing, or perhaps the hash table might have been replaced with an equally rediculous coding structure... you know, like a REAL ENUM!?

  • C-Derb (unregistered)

    TRWTF is the hashtable. That data should be stored in xml.

  • Some Jerk (unregistered) in reply to C-Derb

    nah, a sql database with a table for each day, containing only 1 row and 2 columns. then they can Select top 1 * From {0} with {0} = Today.

  • Leo (unregistered)

    Where's Blernsday?

  • (cs)

    I have wanted to do something like this but never felt right doing it.

    Essentially the thinking goes like this: I need an enum, enum Values { Yes, No, Maybe, NotSure }; ok done.

    Now I need to display the names of the enums for the user. Values.GetName() ok Done... err no wait, the "NotSure" should be two words not one. now how do I do that? Hash table (which this is), dictionary? Oh the conundrum here.

    Then I go back and go with string resources and load up that resource file via the enum for the proper value. And that is how you get dynamically changing text for different languages, just swap reaource files.

  • DaveShaw (unregistered)

    The real WTF is the comments... they feel the need to explain what they are doing (even though an 12 year old can figure that out), but not why they are doing it.

    I guess we will never know.

  • Some Jerk (unregistered) in reply to DaveShaw
    DaveShaw:
    The real WTF is the comments... they feel the need to explain what they are doing (even though an 12 year old can figure that out), but not why they are doing it.

    I guess we will never know.

    Well DUHH! Obviously they are trying to memorize the days of the week! Can you think of a better way than writing arbitrary code to represent what you are attempting to learn?

  • abico (unregistered) in reply to KattMan
    KattMan:
    I have wanted to do something like this but never felt right doing it.

    Essentially the thinking goes like this: I need an enum, enum Values { Yes, No, Maybe, NotSure }; ok done.

    Now I need to display the names of the enums for the user. Values.GetName() ok Done... err no wait, the "NotSure" should be two words not one. now how do I do that? Hash table (which this is), dictionary? Oh the conundrum here.

    Then I go back and go with string resources and load up that resource file via the enum for the proper value. And that is how you get dynamically changing text for different languages, just swap reaource files.

    This is how: http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute

    Google before you do anything, will you?

  • (cs) in reply to abico
    abico:

    This is how: http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute

    Google before you do anything, will you?

    Nice, now how do you get different ones for English, Spanish, Russian, Pirate Day and Klingon.

    This works well if you don't need to worry about internationalization.

  • AGray (unregistered)

    I'm going to have a go at rewriting this function in one line...wish me luck!!! (I may or may not need it)

    C#:

    public static string getDayEnum()
    {
        return DateTime.Now.DayOfWeek.ToString();
    }

    Java:

    import java.util.Date;
    
    public static string getDayEnum()
    {
        return new Calendar.DAY_OF_WEEK.toString();
    }

    ...I have great faith in the C# one, since I use C# on a day-to-day basis. Java, not so much, I could have botched that...

    CAPTCHA: abico - Seriously, the guy who wrote the code in the WTF should use one to crunch numbers, not a computer.

  • (cs)

    The best part is that the author actually did DateTime.Now.DayOfWeek; in two parts, but he had the number he wanted.

    To even make this kluge work, he had to understand that today.DayOfWeek was a number from 0 to 6...so why didn't he just use the number?

    Paid by the line?

  • Botia (unregistered)

    The real WTF is using a hash table when everyone knows a list is faster for collections of 10 or less, right?

  • C-Derb (unregistered) in reply to Coyne
    Coyne:
    The best part is that the author actually did DateTime.Now.DayOfWeek; in two parts, but he had the number he wanted.

    To even make this kluge work, he had to understand that today.DayOfWeek was a number from 0 to 6...so why didn't he just use the number?

    Paid by the line?

    Agreed.

    But assuming he didn't know that he could just cast from enum to int, as the original submitter pointed out ( (int) DateTime.Now.DayOfWeek ), why would he choose to load up a hash table every time this is called instead of just using a switch statement? Egads, even a nested if statement would have made more sense.

    Bonus points for using String.Format instead of ToString() though. I really liked that.

  • Meep (unregistered) in reply to KattMan
    KattMan:
    I have wanted to do something like this but never felt right doing it.

    Essentially the thinking goes like this: I need an enum, enum Values { Yes, No, Maybe, NotSure }; ok done.

    Now I need to display the names of the enums for the user. Values.GetName() ok Done... err no wait, the "NotSure" should be two words not one. now how do I do that?

    Easy, just s/([a-z])([A-Z])/\1 \2/g.

  • Spewin Coffee (unregistered)

    The real fail is using C#.

  • Rob (unregistered)

    TRWTF is calling ToString() on a string.

  • James (unregistered) in reply to Rob

    It's not a string, it's an object. The HashTable is not generic.

  • Bill (unregistered)

    Does C# have arrays?

  • Rob (unregistered) in reply to James
    James:
    It's not a string, it's an object. The HashTable is not generic.

    It's an object, but it's still a string. You just need a cast, not a ToString() call.

  • Mathlete (unregistered)

    Using (int)DayOfWeek.Sunday is a terrible idea. The underlying values are not guaranteed to be 0..6 starting with Sunday, the aren't even guaranteed to be consecutive, or the same across different versions of .NET!

    Relying on the underlying integer values of an Enum is asking for trouble.

    That said, I would have written this as switch (DayOfWeek) { ... }instead of this awful mess.

  • NitPiC# (unregistered) in reply to Mathlete
    Mathlete:
    Using (int)DayOfWeek.Sunday is a terrible idea. The underlying values are not guaranteed to be 0..6 starting with Sunday, the aren't even guaranteed to be consecutive, or the same across different versions of .NET!

    Relying on the underlying integer values of an Enum is asking for trouble.

    Are you sure about that?

    This sounds like a reasonable guarantee to me:

    "The DayOfWeek enumeration represents the day of the week in calendars that have seven days per week. The value of the constants in this enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday)."

    Granted, there is the caveat about only working on calendars that have seven days per week. But I think it's a reasonable assumption to assume that there is no requirement here to handle such calendars.

  • geoffrey (unregistered) in reply to Coyne
    Coyne:
    The best part is that the author actually did DateTime.Now.DayOfWeek; in two parts, but he had the number he wanted.

    To even make this kluge work, he had to understand that today.DayOfWeek was a number from 0 to 6...so why didn't he just use the number?

    Paid by the line?

    But suppose the code is ported to a platform that does not support date/time primitives. Better to wrap it in a function call, then. That way you only have one thing to change.

  • (cs)

    It fascinates me that so many cultures have a seven-day week, especially since there is no seven-day trigger for it in Nature (as far as I am aware.) The 28-ish day month has a couple of likely causes, and the year is also easily explainable. But the week? Where did that come from?

  • NitPiC# (unregistered) in reply to Maurits
    Maurits:
    It fascinates me that so many cultures have a seven-day week, especially since there is no seven-day trigger for it in Nature (as far as I am aware.) The 28-ish day month has a couple of likely causes, and the year is also easily explainable. But the week? Where did that come from?
    Yeah, that is a bit curious. I assume you've already read the wiki on the subject

    7*4 == 28 is one possible link, but as Wiki points out, that is fairly week (yeah, pun intended, sorry).

    Since it is referenced right in the first chapter of the Bible, the idea must have been around and firmly entrenched for ages, long before people started committing ancient myths to written documents.

    Curious stuff indeed.

  • Radu V. (unregistered)

    For key, value pairs collections under 10 items it is recommended to use Dictionary.

  • Errr.... (unregistered)

    He/she wants a string with the name of day of the week, not an integer:

    using System; using System.Globalization; DateTime.Now.ToString("dddd", CultureInfo.CurrentCulture); // Or your favourite culture.

    CAPTCHA: facilisi == easy peasy.

  • PHP Vigilante (unregistered)

    The real WTF is Java.

  • Jason (unregistered) in reply to Errr....

    No, it's actually the int as a string. For example, I'm writing this on Friday, and if I run the code, the return value of that method is the string "5".

    So what this is actualy doing is:

    ((int)DateTime.Now.DayOfWeek).ToString();

  • Kryptus (unregistered)

    string dayOfWeekStr = String.Format("{0}", today.DayOfWeek);

    This is the best use of String.Format() I've ever seen !

  • barleyguy (unregistered) in reply to Radu V.

    On modern superscalar processors, the break even point for linear searches is higher than 10. One test showed it was between 64 and 128.

  • wtf (unregistered)

    Y U No(I had to do it) know that 7days week is derrived from 7 days moon phases?

  • (cs) in reply to PHP Vigilante
    PHP Vigilante:
    The real WTF is Java.
    It's a good job he didn't use it then.
  • Cbuttius (unregistered)

    No, 7 days in the week derives from creation, when G-d created the world in 6 days then rested on the 7th.

    Subsequently when the Israelites left Egypt and were given the Manna, they were told about the Sabbath, on which no manna would fall.

    In addition there were more rules placed upon them for this day such that publicly going out to gather wood on this day was a capital offence.

    As this tradition has been kept ever since, and given that there are names for each day in a 7-day cycle, the Sabbath always falls on the same named day, and that happens to be the one that is named "Saturday". Thus it is the 7th day in a 1-based calendar, but in a 0-based weekday index it should probably be numbered 0 not 6.

  • A. Nonymous (unregistered) in reply to PHP Vigilante
    PHP Vigilante:
    The real WTF is not knowing Java.
    TFTFY
  • Luc (unregistered)

    TRWTF is that the week starts on Monday :-p

  • lmm (unregistered) in reply to KattMan
    KattMan:
    Nice, now how do you get different ones for English, Spanish, Russian, Pirate Day and Klingon.

    This works well if you don't need to worry about internationalization.

    Number of systems I've worked on that had overcomplicated property file systems for getting display strings: many.

    Number of systems I've worked on that were ever actually used in any language other than English: 0.

    Be agile.

  • Cbuttius (unregistered)

    In Genesis, it says "one day". Then after that says "a second day", "a third day" and these follow the days.

    After 6 it says "The sixth day" not "a sixth day".

    and then goes on to introduce the Sabbath...

    If you don't believe in creation you have no particular reason to decide on which day of the week is first as it has no logic. But if you do believe in it, then the day biblically referred to as the first day is Sunday.

    Of course we know these weren't solar days because the solar system was not created until the 4th day, however G-d allocated Saturday to represent the Sabbath and thus Sunday to represent the first day.

  • Sunday is the last day (unregistered)

    The only thing you actually achieve with this is to break localization. Which is all good if you want to force Sunday as the first day on people...

  • shoofle (unregistered)

    ♫ Day enum and me wan' go home. ♪

  • Meep (unregistered) in reply to NitPiC#
    NitPiC#:
    Maurits:
    It fascinates me that so many cultures have a seven-day week, especially since there is no seven-day trigger for it in Nature (as far as I am aware.) The 28-ish day month has a couple of likely causes, and the year is also easily explainable. But the week? Where did that come from?
    Yeah, that is a bit curious. I assume you've already read the wiki on the subject

    7*4 == 28 is one possible link, but as Wiki points out, that is fairly week (yeah, pun intended, sorry).

    Since it is referenced right in the first chapter of the Bible, the idea must have been around and firmly entrenched for ages, long before people started committing ancient myths to written documents.

    Curious stuff indeed.

    I suspect that it's twofold: when various tribes began farming, the regular work becomes very cyclical and time off every seven days is just about right. Various revolutionaries have experimented with longer and shorter weeks, and it never lasts.

    And seven itself is an appealing number to religious mystics just figuring out math; because the number is mystical proves that the deity is actually granting the time off, which also makes the deity far more appealing.

  • Cbuttius (unregistered)

    Using a number to represent the day of the week though is normally only an internal thing because numbers work better than names for this purpose when it comes to computing.

    I mentioned that as someone who keeps the Sabbath on a Saturday, I think Saturday should be day 0 so Sunday is day 1. So those who like Monday to be day 1 are happy because it is, while Sunday is day 0, as there isn't a day 7 anyway.

    It does make some sort of sense to keep Saturday and Sunday together at one of the two ends, and certainly to keep Monday to Friday as a continuous range.

    International issues are more likely to happen in working out what is the "next working day" or "trading day" assuming that in every country they work Mondays to Fridays but that would be in the business logic.

Leave a comment on “Denumerating the DayOfWeekEnum”

Log In or post as a guest

Replying to comment #385756:

« Return to Article