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

  • xmunk@sh.itjust.works
    link
    fedilink
    arrow-up
    51
    ·
    edit-2
    1 year ago

    As a Real Programmer™ I have developed such a deep fear of anything time and date related that I would fully endorse dispatching an API call to the tz_database instead of attempting any fucking part of this.

    Kids, it’s fine to meme about silly stuff… but date and time is deadly serious, regardless of how careful you think you’re being you are wrong.

    Do you know how many timezones there are in Indiana? No? Look it up and scream in horror.

    • sunbeam60@lemmy.one
      link
      fedilink
      arrow-up
      21
      ·
      1 year ago

      What if I told you that weekend days are locale dependent?!

      Time and date is the black hole where optimistic programmers go to die. Nothing is simply with localisation and if you think it is, you mustn’t have worked enough with it.

      Source: Run a system that schedules millions of interactions across the world and deeply depend on this. The amount of code to manage and/or call out to external services to give us information about time zones, summer time, locale specific settings, day names, calendar systems, week numbers etc etc.

    • AndyLikesCandy@reddthat.com
      link
      fedilink
      arrow-up
      6
      ·
      1 year ago

      Here’s a fun thought experiment: What gregorian year and date will the spacian date value of zero correlate to? Trick question.

      The atomic clock on the moon and every other celestial body colonized will simply start at zero, and thanks to relativity it will not actually be the same rate of time passing as on earth.

      Enjoy your nightmares.

    • Kogasa@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      1 year ago

      IMO every datetime should be in utc, and variables for datetimes should either be suffixed “Utc” or have a type indicating their time zone (DateTimeOffset or UtcDateTime etc). Conversion to local time happens at the last possible second (e.g. in the view model or an outbound http request parameter). Of course that doesn’t solve the problem of interoperating with other morons programmers who don’t follow these rules, but it keeps things a lot neater locally.

      Scheduling based on regional time conventions (holidays, weekends, etc) is just not great though.

      • usrtrv@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        Throwing UTC everywhere doesn’t solve comparisons around leap seconds. I’m sure they’re other issues with this method, but this is kinda the point of “just use a library”. Then it’s someone else’s problem.

        • Kogasa@programming.dev
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

          I’m a .NET dev, I don’t have a concept of “just use a library.” Everything is a library. I don’t mean “using int for datetimes is ok as long as you label it utc,” I just mean “don’t deal with time zones.”

        • v9CYKjLeia10dZpz88iU@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          1 year ago

          Unix is the easiest format I’ve used. It’s easy to parse, it’s consistent, there’s not usually competing unix like formats, it converts perfectly to other time formats, most file explorers can immediately sort it correctly, and it’s clearly the date from which the universe spawned into existence.

  • eluvatar@programming.dev
    link
    fedilink
    arrow-up
    46
    arrow-down
    1
    ·
    1 year 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
      11
      ·
      1 year 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@infosec.pub
        link
        fedilink
        arrow-up
        6
        ·
        1 year 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
        ·
        1 year 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

  • space@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    42
    arrow-down
    1
    ·
    1 year 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.

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

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

  • MagicShel@programming.dev
    link
    fedilink
    arrow-up
    18
    ·
    1 year 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
    17
    arrow-down
    2
    ·
    1 year 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