Yes, someone actually did this and I found it running on our server

  • space@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    49
    arrow-down
    1
    ·
    11 months ago

    You want to expand your business to Europe. Bam, your code is broken, in Europe the week starts on Monday.

    Than you want to expand to the middle east. Bam, broken again… Because in arab countries and Israel, the weekend is on Friday and Saturday.

    Then you want to expand to Mexico and India. Bam, broken again, their weekend is only on Sunday.

  • eluvatar@programming.dev
    link
    fedilink
    arrow-up
    45
    arrow-down
    1
    ·
    11 months ago

    Honestly the first one is the only one that works when people define the first day of the week differently. On the other hand, it does make you wonder. If Sunday is the first day of the week (as it is in many places) then how is it also part of the weekend?

    • Esca@lemmy.one
      link
      fedilink
      arrow-up
      13
      ·
      11 months ago

      But if you’re worried about locale, you can’t assume people use the string “Saturday” to describe Saturday either. That solution only works in English.

      • jonne
        link
        fedilink
        arrow-up
        5
        ·
        11 months ago

        I assume this is in a language where the first day of the week is a fixed part of the language (like JS).

      • Schrolli97@programming.dev
        link
        fedilink
        arrow-up
        3
        ·
        11 months ago

        Thats not really one weekend then though, is it? Its more like last week’s post-week weekend and this week’s pre-week weekend

  • Matombo@feddit.de
    link
    fedilink
    arrow-up
    30
    arrow-down
    1
    ·
    11 months ago

    Ok another US local units are retarded rant: it’s called weekEND! why do you start your week at sunday and not monday! Sunday is part of the weekEND!

    • Leate_Wonceslace@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      8
      arrow-down
      1
      ·
      11 months ago

      If you’re referring to an “end” of an object, it can refer to the extreme of a side of it. For example, aglets are at either end of a shoelace.

      • Matombo@feddit.de
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        11 months ago

        I’m refering to end in a temporal sense because we are talking about a time context here. There is a clear direction so going backwards brings you to the begin.

  • irdc@derp.foo
    link
    fedilink
    arrow-up
    27
    ·
    edit-2
    11 months ago
    weekend = day_of_week in (“sat”, “sun”)
    

    As a bonus this completely sidesteps the issue of what day is 0 or 1.

    • dbilitated@aussie.zone
      link
      fedilink
      arrow-up
      1
      ·
      11 months ago

      yeah I like having an array of days that are weekend days then testing if the day is in the array. can change what days are considered weekend if we go to a three day weekend and it reads really well. I hate massive if statements

    • 4am@lemm.ee
      link
      fedilink
      arrow-up
      3
      arrow-down
      2
      ·
      11 months ago

      dayOfWeek is clearly represented by 1-7 in the example, with Sunday being 1.

      So, I guess the answer is “depends on what date library you’re linking against”

        • Zagorath@aussie.zone
          link
          fedilink
          arrow-up
          12
          arrow-down
          1
          ·
          edit-2
          11 months ago

          Both Monday and Sunday are used as the first day of the week with quite some regularity. It’s a completely arbitrary standard no different to "the tenth month is the one called “October”. Or dividing a day into 24 segments which are each broken into 60 smaller segments of 60 even smaller segments. You can’t say either is “wrong” per se.

          Personally, I was brought up learning Sunday is the first day of the week, but at some point decided that was bullshit partly because it’s the week end. But also just from a practical standpoint when looking at a calendar, it’s useful to have the weekend days grouped together.

          • Cosmicomical@kbin.social
            link
            fedilink
            arrow-up
            3
            ·
            11 months ago

            Funny thing, september comes from the number 7, october from 8 and november and december from 9 and 10, as the year in ancient rome was starting around march. This problem is timeless.

            • Zagorath@aussie.zone
              link
              fedilink
              English
              arrow-up
              2
              ·
              11 months ago

              Huh. I knew about the problem (that’s why I used October as my example, rather than, say, February), but I was mistaken as to the cause. The way I had always heard it told, September–December don’t match their current place in the year because of the addition of July and August. But I just looked it up and it seems you’re right. Those months are merely renamings of Quintilis and Sextilis, and the numbering issue comes from moving the start of the year from March to January.

          • JeffKerman1999@sopuli.xyz
            link
            fedilink
            arrow-up
            18
            ·
            11 months ago

            Ah the same place that uses feet and inches, and puts the month before the day, and cannot read a 24 hour clock?

            • DroneRights [it/its]@lemm.eeOP
              link
              fedilink
              English
              arrow-up
              3
              arrow-down
              1
              ·
              11 months ago

              That’s nice, you can run weekend = dayOfWeek > 5 and then explain to the boss why clients are getting work texts on a sunday

                • DroneRights [it/its]@lemm.eeOP
                  link
                  fedilink
                  English
                  arrow-up
                  4
                  arrow-down
                  1
                  ·
                  11 months ago

                  I’ll be sure to tell my boss to throw away all the work he already paid for and start over in a different language. I’m sure he’ll be very understanding

                • Cosmicomical@kbin.social
                  link
                  fedilink
                  arrow-up
                  2
                  ·
                  11 months ago

                  Yo nodejs is just plain amazing. We should just keep improving on js and replace all other languages. Js is already on all browsers, by adopting it on the server you get huge efficiency as you can move code AND coders between backend and frontend. Of course you must make the right choices of practices and frameworks for this to be possible

            • DroneRights [it/its]@lemm.eeOP
              link
              fedilink
              English
              arrow-up
              1
              arrow-down
              2
              ·
              edit-2
              11 months ago

              That’s nice, you can run weekend = dayOfWeek > 5 and then explain to the boss why clients are getting work emails on a sunday

          • oolio@feddit.de
            link
            fedilink
            arrow-up
            5
            ·
            11 months ago

            Cron has clearly the superior numbering system, where sunday is both 0 and 7

        • kungen@feddit.nu
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          11 months ago

          Depending on how you’re counting your integers, Monday is 0, being the first day of the week.

  • MagicShel@programming.dev
    link
    fedilink
    arrow-up
    23
    ·
    11 months ago

    I’d make it a named function for clarity and testability and proceed to give zero shits how it is implemented. I would unironically write this code if it worked, but I wouldn’t inline it to reduce the cognitive load of reading it.

  • Mac@programming.dev
    link
    fedilink
    arrow-up
    18
    arrow-down
    1
    ·
    11 months ago

    Image Transcription: Meme


    [Paneled meme with a brain that gets increasingly glowing]


    [The brain is smaller than the skull]

    if dayOfWeek.name == "Sunday" || dayOfWeek.name == "Saturday"
        weekend = true
    

    [The brain is glowing in some areas]

    if dayOfWeek < 2 || dayOfWeek > 6
        weekend = true
    

    [The brain is shooting out rays of light]

    weekend = !((dayOfWeek - 1) % 6)
    

    I am a human volunteer who transcribes posts to improve accessibility on programming.dev and you could be one too! !transcribing@programming.dev

  • ISometimesAdmin@the.coolest.zone
    link
    fedilink
    arrow-up
    2
    arrow-down
    1
    ·
    11 months ago

    Depending on whether this code is in a hotpath (and considering how “elementary” it is, I figure that’s a possibility), this could very well be a significant speed improvement.

    Though I’d say that only excuses it if it’s truly an elementary function (and not one line as part of a larger function), as otherwise it’s unreadable garbage. But on its own it:

    • has a clear purpose
    • (presumably) isn’t reimplementing functionality
    • is easily tested
    • can be modified with no side effects (besides breaking your calendar, but that’s beside the point)
    • DroneRights [it/its]@lemm.eeOP
      link
      fedilink
      English
      arrow-up
      2
      arrow-down
      1
      ·
      edit-2
      11 months ago

      It’s one line as part of a larger function. Also, it doesn’t actually say weekend, it just executes some other functionality if !(day % 6). I made it more readable so that everyone here could understand what it does