Denumerating the DayOfWeekEnum

« Return to Article
  • geoffrey 2012-07-26 14:36
    This was obviously done to make the code portable across platforms. This is a crucial requirement of an enterprise system.
  • Chuck 2012-07-26 14:42
    Not Frist.
  • snoofle 2012-07-26 14:52
    ...or to anticipate for when we add an 8th day of the week.
  • paquetted 2012-07-26 15:01
    This is not portable in other languages/countries where the first day of the week may not be sunday..
  • Chronomium 2012-07-26 15:03
    (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 2012-07-26 15:08
    Ummm... WOW! I believe what we have here is a
    new IdiotSavant<Idiot>();
  • C-Derb 2012-07-26 15:11
    Safe to assume DRX and OBL "commonly" have performance issues?
  • M 2012-07-26 15:12
    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 2012-07-26 15:14
    Obviously, this code was offshored to Turkmenistan:

    http://en.wikipedia.org/wiki/Renaming_of_Turkmen_months_and_days_of_week,_2002
  • Some Jerk 2012-07-26 15:16
    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 2012-07-26 15:17
    TRWTF is the hashtable. That data should be stored in xml.
  • Some Jerk 2012-07-26 15:21
    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 2012-07-26 15:24
    Where's Blernsday?
  • KattMan 2012-07-26 15:31
    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 2012-07-26 15:33
    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 2012-07-26 15:49
    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 2012-07-26 15:51
    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?
  • KattMan 2012-07-26 16:01
    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 2012-07-26 16:04
    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.
  • Coyne 2012-07-26 16:19
    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 2012-07-26 16:22
    The real WTF is using a hash table when everyone knows a list is faster for collections of 10 or less, right?
  • C-Derb 2012-07-26 16:39
    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 2012-07-26 17:23
    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 2012-07-26 17:30
    The real fail is using C#.
  • Rob 2012-07-26 17:51
    TRWTF is calling ToString() on a string.
  • James 2012-07-26 18:34
    It's not a string, it's an object. The HashTable is not generic.
  • Bill 2012-07-26 18:55
    Does C# have arrays?
  • Rob 2012-07-26 18:58
    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 2012-07-26 20:47
    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# 2012-07-26 21:06
    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 2012-07-26 22:04
    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.
  • Maurits 2012-07-26 23:00
    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# 2012-07-27 00:37
    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. 2012-07-27 00:47
    For key, value pairs collections under 10 items it is recommended to use Dictionary.
  • Errr.... 2012-07-27 01:50
    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 2012-07-27 04:04
    The real WTF is Java.
  • Jason 2012-07-27 04:12
    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 2012-07-27 04:13
    string dayOfWeekStr = String.Format("{0}", today.DayOfWeek);

    This is the best use of String.Format() I've ever seen !
  • barleyguy 2012-07-27 04:15
    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 2012-07-27 04:21
    Y U No(I had to do it) know that 7days week is derrived from 7 days moon phases?
  • English Man 2012-07-27 04:45
    PHP Vigilante:
    The real WTF is Java.
    It's a good job he didn't use it then.
  • Cbuttius 2012-07-27 05:19
    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 2012-07-27 06:00
    PHP Vigilante:
    The real WTF is not knowing Java.

    TFTFY
  • Luc 2012-07-27 06:14
    TRWTF is that the week starts on Monday :-p
  • lmm 2012-07-27 06:22
    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 2012-07-27 06:35
    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 2012-07-27 06:52
    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 2012-07-27 07:04
    ♫ Day enum and me wan' go home. ♪
  • Meep 2012-07-27 07:15
    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 2012-07-27 07:19
    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.

  • data nerd 2012-07-27 09:06
    He forgot FILENOTFOUND
  • PedanticCurmudgeon 2012-07-27 09:26
    NitPiC#:
    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.
    From the article:
    The association of the days of the week with the Sun, the Moon, and the five planets visible to the naked eye dates to the Roman era (2nd century).
  • n9ds 2012-07-27 09:27
    7 Days/Week is easy to explain, take the number of days in the year, figure on 50 weeks of useful work (which is a nice round number), then add 2 for vacation, and lo and behold you get 7 ish. Although I'm not sure what they'd do in places where they get more than 2 weeks of vacation. Maybe that's where the exchange rate comes into play.
  • DWalker59 2012-07-27 11:03
    Some Jerk:
    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!?


    Its'? That's a new variation.
  • Nagesh 2012-07-27 11:17
    Is good to see WTF that not PHP. PHP gets too bad rap here, but these codesd show he can make WTF with C++ too.
  • radarbob 2012-07-27 11:45
    Spewin Coffee:
    The real fail is using C#.

    Fishing with Bill Dance!
  • radarbob 2012-07-27 11:48
    PHP Vigilante:
    The real WTF is Java.

    Fishing with Bill Dance!
  • radarbob 2012-07-27 11:51
    A. Nonymous:
    PHP Vigilante:
    The real WTF is not knowing Java.

    TFTFY

    Fishing with Bill Dance!
  • radarbob 2012-07-27 12:14
    It all makes sense... Resume fodder. To wit:


    Engineered highly efficient hash table used throughout the code base, significantly reducing processing time. So concise and well written as to be self documenting. Consistent; the hash table uses strings exclusively, giving a simplified API. I also leverage String's inherent trait of immutability thereby reducing not only error potential, but conserving memory in a 1.5M LOC system where every byte counts.

    Please send the codz upon request.

    Fishing with Crystal Meth
  • foxyshadis 2012-07-27 12:18
    PedanticCurmudgeon:
    NitPiC#:
    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.
    From the article:
    The association of the days of the week with the Sun, the Moon, and the five planets visible to the naked eye dates to the Roman era (2nd century).


    That's from at least a millennium or two after the 7-day week became common, though, so it obviously has nothing to do with the origins.

    Personally, I think it's most likely that an early prophet of some forgotten religion (what we'd call a cult leader now), maybe prior to Judaism entirely, came up with the absolute 7-day cycle and it stuck and spread. Similar arbitrary customs happen all the time, because sometimes it doesn't matter what you use as long as everyone agrees.
  • PedanticCurmudgeon 2012-07-27 12:48
    foxyshadis:
    PedanticCurmudgeon:
    From the article:
    The association of the days of the week with the Sun, the Moon, and the five planets visible to the naked eye dates to the Roman era (2nd century).


    That's from at least a millennium or two after the 7-day week became common, though, so it obviously has nothing to do with the origins.
    Unless the association goes back further than Wikipedia cares to admit, which it does.
  • Nagesh 2012-07-27 13:12
    7 day week borrowed from ancient hindu calendar systems.
    Jesus Christ visited India and stay with several folks over here and learn magics tricks.
    Between the age of 13 - 28.

    None of this visit documented in Bible, but we Hindu know better.
  • n9ds 2012-07-27 17:10
    Nagesh:
    7 day week borrowed from ancient hindu calendar systems.
    Jesus Christ visited India and stay with several folks over here and learn magics tricks.
    Between the age of 13 - 28.

    None of this visit documented in Bible, but we Hindu know better.


    No, but something like it is documented here: http://www.chrismoore.com/lamb.html where we learn the origins of the Jewish tradition of Chinese food on Christmas Day.
  • Nagesh 2012-07-30 04:49
    n9ds:
    Nagesh:
    7 day week borrowed from ancient hindu calendar systems.
    Jesus Christ visited India and stay with several folks over here and learn magics tricks.
    Between the age of 13 - 28.

    None of this visit documented in Bible, but we Hindu know better.


    No, but something like it is documented here: http://www.chrismoore.com/lamb.html where we learn the origins of the Jewish tradition of Chinese food on Christmas Day.


    pla·gia·rism/ˈplājəˌrizəm/
    Noun:
    The practice of taking someone else's work or ideas and passing them off as one's own.



    It is copying, but original Hindu mystic rely on word of mouth to tell story and did not have reach of Internet.
  • Some Jerk 2012-07-30 08:27
    DWalker59:
    Some Jerk:
    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!?


    Its'? That's a new variation.

    Now THAT is a WTF!
  • QJo 2012-07-30 08:38
    shoofle:
    ♫ Day enum and me wan' go home. ♪

    +1

    "Day 0. Da-a-a-ay 0."
  • Coyne 2012-07-30 09:40
    C-Derb:
    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.


    Maybe he's paid by the millisecond of runtime?
  • JJ 2012-07-30 17:26
    AGray:
    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();
    }


    [...]

    ...I have great faith in the C# one, since I use C# on a day-to-day basis.

    Yikes, I think your faith is about to be shattered. Calling ToString() on an enum member returns the name of the member, not its value, so you'll be returning "Friday," "Tuesday," etc., whereas the WTF code returns "5," "2," etc.
  • Cbuttius 2012-07-31 12:48
    Yom Rishon, 1st day
    Yom Sheni 2nd day
    Yom Shlishi 3rd day
    Yom R'vii 4th day
    Yom Chamishi 5th day
    Yom Shishi 6th day

    and you shouldn't be using your computer on Shabbat anyway.
  • Inflexible 2012-08-05 07:00
    I would like to note that your solution doesn't allow us to add FileNotFound when we need to.
  • Ceiling Cat 2012-08-06 06:37
    I have no understanding for this nonsense.
    Sunday is supposed to be day "7". It says so in the Bible.