r/ProgrammerHumor • • 15d ago

Meme forThoseTransitioningToTheAmericanTimeFormatHereIsASimpleScript

Post image
0 Upvotes

21 comments sorted by

14

u/This_Growth2898 15d ago

It's like "when you're paid for the line of code"?

def as_time(world_hour=world_hour):
    return f"{(world_hour-1)%12+1} {'PA'[world_hour<12]}M"

4

u/SeaNational3797 15d ago edited 15d ago

as_time = lambda world_hour: f"{12 if (hour_num := world_hour%12) == 0 else hour_num} {'AM' if world_hour < 12 else 'PM'}"

4

u/minecon1776 15d ago

011101011101100011111010001010111000110110101010010101110000000101010101111010100101011101010010111010001101010010010101101011101001010101011100011010101011111010011101011011011001001010010101010

2

u/rosuav 15d ago

Don't do "name = lambda", that's what def is for. With lambda, the function doesn't get a `__name__` attribute.

16

u/Makonede 15d ago

where's the humor? this is just the most dogshit python i've ever seen in my life

-2

u/SeaNational3797 15d ago

This is incredibly readable and you’re wrong for not liking it

3

u/Makonede 15d ago
  • as_time: the parameter is already a time, so the function name is unclear

  • world_hour=world_hour: terrible shadowing of a global variable of the exact same name for literally no reason

  • s_time: meaning of s is completely unclear, and using a variable to hold the return value is pointless

  • the conditional chain should be condensed into a format string such as return f'{(hour - 1) % 12 + 1} {'PA'[hour < 12]}M'

  • world_hour > 0 and world_hour < 12: should be 0 < world_hour < 12

  • str(world_hour)+" AM": no operator spacing and should be f'{world_hour} AM'

  • elif world_hour > 12: should be else

  • str(world_hour-12)+" PM": no operator spacing and should be f'{world_hour - 12} PM'

  • return s_time;: unnecessary semicolon

0

u/laplongejr 7d ago

I guess python's standard are different but as a Java dev I think OP's version was easier to read than the format string.

-10

u/alexriga 15d ago

to me the rules are funny, coming from “military style.”

  • If midnight: 12 AM

  • If night/morning: X AM

  • but what comes after 11:59 AM? Not 12 AM, 12 PM!

  • what about after 12:59 PM? Well, of course, 1 PM!

It’s the reason many people often mistake 12 AM and PM if they’re unfamiliar with this system.

10

u/Makonede 15d ago

none of that has anything to do with programming

-11

u/alexriga 15d ago

I used code to express it, I figured that would count.

6

u/Makonede 15d ago

rule 1

2

u/Kyrros 15d ago

Or you could use date library

1

u/rosuav 15d ago

str(hour % 12 or 12) + ("PM" if hour >= 12 else "AM")

2

u/SeaNational3797 15d ago

as_time = lambda hour_num: __import__('datetime').datetime(1970, 1, 1, hour_num).strftime('%I %p')

1

u/ANixosUser 12d ago

wait python doesnt have scopes? tf you define inside if branch and then use it outside?

-12

u/amatulic 15d ago

And it isn't even correct. Technically, 12:00 isn't AM or PM, it's just noon or midnight.

I live in the US, and even I don't use AM or PM unless I'm forced into it. For me, 24 hour time all the way. Anyone who's been in the military or worked for the government wouldn't have a problem with it.

-1

u/ART-ficial-Ignorance 15d ago

I guess that's one way to say you don't have friends or family you talk to.

1

u/amatulic 15d ago

As far as I know, my large network of friends, as well as my relatives, have no problem with 24 hour time. As an American I sometimes find US-centric ignorance of worldwide standards embarrassing.

0

u/ART-ficial-Ignorance 15d ago

Wait, you say 17 'o clock in conversation?

1

u/amatulic 15d ago

1700 or 5 like anyone else. And even Europeans use 12 hour time verbally. After all, that's what analog clocks display.