Yeah I know, just wanted to avoid branching as a proof of concept.
The approach makes sense, you have a 1-based display format (1-12) do you need to convert it into 0-based index format compatible with modulo logic (0-11), perform the 24->12 and lastly convert back to display format. We're are just doing all 3 as one expression.
In the real world I probably wouldn't do any of this. DateTime logic has been implemented and fine tuned countless of times, usually there is no reason to reinvest the wheel, or at least not the whole thing :D
Sure depends on platform and context but we are taking about Swift so I don't think microcontrollers are involved.
For almost anything else, it's usually better to prioritize flexibility and future proofing then just go all in on optimization. I would even go so far as to use unix time or something and then use DateTime format string to shape it into desired form. Functional, reliable (as the library is already tested) and can be easily modified just by changing the format string. If you're formating time by manually twiddling with components, you should probably rethink your approach.
no but it's something to seriously consider, of course since it's in swift I doubt it is some sort of embedded system but it's not something to downvote into oblivion for
well… this varies from country to country. In Japan it’s not entirely uncommon to see hours “5p-2a” written as “17:00-26:00”. Not exactly more than 24 hours but certainly written as more than 24 hours.
Naw I'm sure I'm smarter than the Microsoft guys or the nodatime guys. I'll just whip a datetime library up on my own in a few hours. How hard can it be? Just make the underlying type a Unix timestamp and everything else is just display logic! /s
Interesting. Codewise 26 % 12 = 2 still works out whereas OP's code and the hour - 12 algorithms don't.
Although I usually use hour > 11 ? 'p' : 'a' to determine the meridian label. Guess I need to update this to hour > 11 && < 24 ? 'p' : 'a'. Grr handling datetimes!
Yeah I assumed the post's code was not necessarily display code, as opposed to your example, but I cannot find a realistic example where that would be the case. However 24h timestamps still require less processing than 12h timestamps, even for Japanese time display.
It generally does a lot better with plain text such as social media posts -- my assumption is a combination of the indentation and the fact it's probably trained almost entirely on non-monospace fonts makes it trip up a lot with code. Unfortunately, we can't get anything better for the bot; the only API that's overall considered better is Google's, and it's much more expensive. I'm not bothered, though, I enjoy typing out big code posts even if I do occasionally mess up like here.
Even with more reliable OCR, we'd need to format and double-check it to make sure it didn't mess anything up. OCR technology is good and getting better, but it's not yet reliable enough to be providing for a service without its attempts getting vetted by a human. ;)
It's funny I recently did similar logic ona PLC, I ended up doing a check after pulling the time if it was greater than 12, setting a book tag to note that it was pm, and then subtracting 12.
Ok, in this case there is no 1am. Only times from 1pm-12am. They are all in 24 hour instead of 12 hour (so 13 - 24). Doing notificationHour- = 12 would be an excellent solution for this problem.
I want to live in a world missing the first 12 hours! I'm usual tired and hungry during those anyway. Also there is no 24th hour, 23:59 rolls over to 00:00 (so 13- 23)
130
u/bunnyjuumps Nov 18 '21
return notificationHour > 12 ? notificationHour - 12 : notificationHour
5
u/Legendary-69420 Nov 19 '21
Oh so swift is so similar to python? I know what I might do next
48
u/--var Nov 19 '21
There are only 24 hours in a day, so there is no 24th hour. 23:59 -> 00:00
In javascript you can just go
hour % 12 || 12, but this just goes to show 24 hour timestamps make more sense and require less processing.17
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 18 '21 edited Nov 18 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
4
u/Knuffya Nov 19 '21
This is shit on so many levels
3
u/Spynder shameless stealing content to r/YandereTechnique Nov 19 '21
r/YandereTechnique
2
u/DrinkToShrink Nov 19 '21
That's stupid! Why not use a switch-case statement?
1
u/jrich44360 Nov 19 '21
If (notificationHour > 12) { notificationHour = (notificationHour -12) } Or If (notificationHour > 12) { notificationHour = (notificationHour%12) }
1
u/proEndreeper Nov 19 '21
It's funny I recently did similar logic ona PLC, I ended up doing a check after pulling the time if it was greater than 12, setting a book tag to note that it was pm, and then subtracting 12.
1
u/Svobpata Nov 25 '21
Layering bad code on top of bad code: why didn’t they just use a dictionary of 24:12 mappings?
Real solution: use Foundation’s Date, DateFormatter and Calendar
0
u/mplaczek99 Nov 19 '21
notifacationHour -= 12;
0
u/[deleted] Nov 18 '21
[deleted] — view deleted comment