Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created June 24, 2011 10:06
Show Gist options
  • Save jed/1044533 to your computer and use it in GitHub Desktop.
Save jed/1044533 to your computer and use it in GitHub Desktop.
ES5-ish shim for Date.prototype.toISOString
// thanks to @fgnass and @subzey for their awesome golf skills
// annotation by @fgnass
function(a){
a=this;
return (
1e3 // Insert a leading zero as padding for months < 10
-~a.getUTCMonth() // Months start at 0, so increment it by one
*10 // Insert a trailing zero as padding for days < 10
+a.toUTCString() // Can be "1 Jan 1970 00:00:00 GMT" or "Thu, 01 Jan 1970 00:00:00 GMT"
+1e3+a/1 // Append the millis, add 1000 to handle timestamps <= 999
// The resulting String for new Date(0) will be:
// "-1010 Thu, 01 Jan 1970 00:00:00 GMT1000" or
// "-10101 Jan 1970 00:00:00 GMT1000" (IE)
).replace(
// The two digits after the leading '-1' contain the month
// The next two digits (at whatever location) contain the day
// The last three chars are the milliseconds
/1(..).*?(\d\d)\D+(\d+).(\S+).*(...)/,
'$3-$1-$2T$4.$5Z')
}
function(a){a=this;return(1e3-~a.getUTCMonth()*10+a.toUTCString()+1e3+a/1).replace(/1(..).*?(\d\d)\D+(\d+).(\S+).*(...)/,'$3-$1-$2T$4.$5Z')}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "toISOString",
"description": "ES5-ish shim for Date.prototype.toISOString",
"contributors": ["@subzey", "@fgnass"],
"keywords": [
"ES5",
"shim",
"Date",
"toISOString"
]
}
<!DOCTYPE html>
<title>Date.prototype.toISOString</title>
<div>Expected value: <b id="expected"></b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var toISOString = function(a){a=this;return(1e3-~a.getUTCMonth()*10+a.toUTCString()+1e3+a/1).replace(/1(..).*?(\d\d)\D+(\d+).(\S+).*(...)/,'$3-$1-$2T$4.$5Z')}
, now = new Date
document.getElementById( "expected" ).innerHTML = Date.prototype.toISOString.call(now)
document.getElementById( "ret" ).innerHTML = toISOString.call(now)
</script>
@jdalton
Copy link

jdalton commented Aug 24, 2011

I am not at all aggressive. I have not used any but polite language

Not sure how else to read these...



... maybe something is lost in the translation.

The people here are already informed about the specs. They know the quirks of the language (otherwise they would not be as proficient golfing down the codes to the limit)

I don't believe this is universally the case as I have helped explain spec'ed methods to more than a couple devs who participate in code golf.
In fact some of the quirks, I recently commented on, the browser vendors themselves were not aware of until last week when I reported the issues.

though you might want to try to promote it in a less self-righteous manner,

Thanks for another not-so-subtle ad hominem attack.

@atk
Copy link

atk commented Aug 24, 2011

If you cite someone, be sure, to include the (context)[https://gist.github.com/1039813]. While my comments may in some cases be less than couteous, they certainly are not threatening, hurtful or intimidating. For example: "barking up the wrong tree" is just an english idiom for "missing the point" and not literally meant to degrade you to a canine being. And you are welcome about the hint about the way you appear to be promoting your concern: nobody likes a "I'm holier-than-you"-lecture - especially if they intend to sin anyway. Please keep this in mind when you try to teach others about standards.

@jdalton
Copy link

jdalton commented Aug 24, 2011

If you cite someone, be sure, to include the (context).

I actually did in the comment before last (the "aggressively negative manner" link).

While my comments may in some cases be less than couteous, they certainly are not threatening, hurtful or intimidating

That's great because I didn't describe them as "threatening, hurtful or intimidating", but as "an aggressively negative manner" which your comments were/are.

And you are welcome about the hint about the way you appear to be promoting your concern: nobody likes a "I'm holier-than-you"-lecture - especially if they intend to sin anyway. Please keep this in mind

I don't think reporting potential problems and bugs or defending my motives/feedback (in response to you labeling it as an unjustified nuisance) makes me "holier-than-you" ;)

@mathiasbynens
Copy link

NO.

@jed
Copy link
Author

jed commented Aug 24, 2011

@jdalton, given the reception you're getting here, i get the sense you may be best off taking your ES5 abstinence agenda elsewhere.

Copy link

ghost commented Aug 24, 2011

@jed First, I believe that @jdalton has raised a perfectly valid point: shims that claim to be ECMAScript 5 compliant should be ECMAScript 5 compliant, without deviating from the specification...otherwise, they cannot legitimately be considered ES 5 shims (unless, of course, it is impossible to implement the functionality in ES 3, which is not the case here). Secondly, I humbly request that you refrain from egregiously assuming others' intentions in the future. John-David was merely attempting to provide a constructive critique to assist the community; if you cannot tolerate that, you shouldn't be on GitHub.

@jed
Copy link
Author

jed commented Aug 24, 2011

given that

  1. this shim makes no "claim to be ECMAScript 5 compliant",
  2. ES5 conformance is less important than byte count for this project,
  3. 140bytes gist comments are for golfing, not hand-wringing, and
  4. this has been explained repeatedly without success,

i'm going to start moderating comments accordingly. please stay on topic.

Copy link

ghost commented Aug 24, 2011

@jed Thank you for clarifying your position. All your points are valid, with the exception of the first: the description of this Gist reads, "ES5 shim for Date.prototype.toISOString." This snippet does not aspire to be an ES 5 shim, as you have made clear; thus, I respectfully suggest updating the title accordingly.

Edit: Thank you for updating the title.

@fgnass
Copy link

fgnass commented Aug 25, 2011

I wouldn't mind calling this function "ES5 compliant" as the function itself behaves as expected in all tested browsers.

We should add a big warning though, that explains that there are better (read more future-proof) ways to implement this, but which unfortunately don't fit into 140 bytes.

Perhaps @jed could create another Gist with his original implementation and link to it, so that others can try to golf enough bytes out of it. Even if that might be impossible, it would still be a good documentation resource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment