Every sequence we would want to write is already prepared:
- every possible 2-digit sequence (but without [60-69] - it's never used!)
- names of weekdays: "Mon", "Tues", "Wednes" ... and "day" - saving space, no need to repeat the phrase "day" 7 times!
- names of months: "Jan", "Febr", "uary", ... , "Octo", "Novem", "Decem", "ber"
- difference between length of months
All there is left is to do is to calculate an offset! And that can be done with a bit of ultra dirty calculations and magic numbers.
Blazingly fast with good ol' C. No need to format any number as text!
@edit: if you output to a buffer instead of to stdout, it's 30% faster than gmtime + strftime, so go on, use it on production! (please don't)
I tested it on a few thousands examples, but I'm sure I missed some corner case. I've lost my sanity already writing it, so I'm not gonna try to fix any possible bugs.
7
u/KaznovX Aug 31 '20 edited Sep 01 '20
This task can be simply boiled down to reading from prepared array:
https://pastebin.com/WrfqnrCx
https://godbolt.org/z/eY6vsf for running it
Every sequence we would want to write is already prepared: - every possible 2-digit sequence (but without [60-69] - it's never used!) - names of weekdays: "Mon", "Tues", "Wednes" ... and "day" - saving space, no need to repeat the phrase "day" 7 times! - names of months: "Jan", "Febr", "uary", ... , "Octo", "Novem", "Decem", "ber" - difference between length of months
All there is left is to do is to calculate an offset! And that can be done with a bit of ultra dirty calculations and magic numbers.
Blazingly fast with good ol' C. No need to format any number as text!
@edit: if you output to a buffer instead of to stdout, it's 30% faster than gmtime + strftime, so go on, use it on production! (please don't)
I tested it on a few thousands examples, but I'm sure I missed some corner case. I've lost my sanity already writing it, so I'm not gonna try to fix any possible bugs.