World: r3wp
[Core] Discuss core issues
older newer | first last |
Gregg 25-Oct-2008 [11169x4] | 'd is the param name. |
REBOL.org has a couple ISO date formatting funcs, though I think a lot of us roll our own, sometimes ad hoc. It depends, too, on how flexible--or accepting of various inputs--you want it to be. | |
as-utc: func [date] [ if all [date/zone 0:00 <> date/zone] [ date: add date negate date/zone ] date/zone: none if none? date/time [date/time: 0:0:0] date ] to-ISO8601-date: func [ "Converts a date! value to an ISO 8601 format string." date [date!] "The date to format" /T {Use T to delimit time value, rather than a space} /no-zone "Don't include the timezone" /local pad z ][ pad: func [val /to len] [ val: form val head insert/dup val #"0" ((any [len 2]) - length? val) ] rejoin [ pad/to date/year 4 "-" pad date/month "-" pad date/day either T ["T"] [" "] either none? t: date/time ["00:00:00Z"] [ ;<< reusing 'T here! rejoin [ pad t/hour ":" pad t/minute ":" pad round t/second either no-zone [""] [ either 0:00 = z: date/zone ["Z"] [ ;<< setting 'z here! rejoin [ pick ["+" "-"] z/hour > 0 pad abs z/hour pad abs z/minute ] ] ] ] ] ] ] | |
>> to-ISO8601-date/T as-utc now == "2008-10-25T18:41:13Z" >> to-ISO8601-date/T as-utc now/date == "2008-10-25T00:00:00Z" | |
Graham 25-Oct-2008 [11173x2] | format I have is SS.FZ |
so pad here will not pad 5.2Z to 05.2Z | |
Gregg 25-Oct-2008 [11175x2] | I looked at some of my stuff but, for some reason, I don't seem to have one that does the 0.0 format for seconds. Even my FORMAT func doesn't work for that, though it would shorten the rejoins a bit. e.g. form-as-utc: func [date] [ format as-utc date "yyyy-mm-dd\Thhh:mm:ss\Z" ] Just have to change that last part for the seconds. But I can't remember if I've published FORMAT. |
Yeah, I've seen that format as a standard, which is why I don't know why I don't have that. Must not have needed it. :-\ | |
Graham 25-Oct-2008 [11177x5] | I ended up just by factoring out my formatting to |
format-10: func [ d [integer! decimal!] ][ either d < 10 [ join "0" d ] [ form d ] ] form-utc: func [ d [date!] /local ][ ; convert to GMT d: d - d/5 rejoin [ d/year "-" format-10 d/month "-" format-10 d/day "T" format-10 d/time/1 ":" format-10 d/time/2 ":" format-10 round/to d/time/3 .1 "Z" ] ] | |
I can understand that one :) | |
why is your as-utc so complicated ? What case am I missing? | |
can date/zone be none? | |
Gregg 25-Oct-2008 [11182x2] | It can in cases where I use it. e.g., cascading calls that may mod the date to UTC more than once. |
Partly legacy as well. I can't remember if REBOL used to set the zone to none, rather than 0:00, or if that was something I did originally. | |
Graham 25-Oct-2008 [11184] | better to be safe than sorry! |
Oldes 25-Oct-2008 [11185] | Rebol zone can be none in some cases. |
Graham 25-Oct-2008 [11186] | do you know which? |
Oldes 25-Oct-2008 [11187x2] | I'm probably wrong.. it looks it should not be none. It returns 0:0 instead of none in all cases (event if there is no zone) >> d: 1-1-2006/1:0:0+0:0 == 1-Jan-2006/1:00 >> d/zone == 0:00 >> d: 1-1-2006 d/zone == 0:00 |
>> d: now d/zone: none d/zone == 0:00 | |
Graham 25-Oct-2008 [11189] | the problem with dates is that if zone is 0, then it does not display. I wasn't aware of any 'none issue. |
Chris 25-Oct-2008 [11190] | do http://www.rebol.org/download-a-script.r?script-name=form-date.r form-date now "%Y-%m-%dT%H:%M:%SZ%Z" form-date/gmt now "%Y-%m-%dT%H:%M:%s" |
Graham 25-Oct-2008 [11191x4] | didn't know about that one ... but |
>> form-date/gmt now "%Y-%m-%dT%H:%M:%s" == "2008-10-25T20:27:11.000000" | |
whereas I need | |
2008-10-25T20:27:11.0Z | |
Chris 25-Oct-2008 [11195x3] | Yep, you'd have to modify Brian's 'pad-precise... |
do http://www.rebol.org/download-a-script.r?script-name=form-date.r pad-precise: func [s [number!]][ skip tail rejoin ["0" round/to s 0.1] -4] form-date now "%Y-%m-%dT%H:%M:%SZ%Z" form-date/gmt now "%Y-%m-%dT%H:%M:%s" | |
(a hack -- 'pad-precise shouldn't be global) | |
Graham 25-Oct-2008 [11198x5] | anyone have a nice word that does this |
string: copy/part string n | |
maybe 'cut ? | |
more traditionally 'left | |
I do this a lot to make sure there is no text overflow in database fields | |
Chris 25-Oct-2008 [11203] | 'truncate, or just 'clip ? |
btiffin 26-Oct-2008 [11204] | Go controversial; use 'only ;) |
Gregg 26-Oct-2008 [11205] | 'left is very BASIC-like. One of the first things I did in REBOL was some string funcs like that. e.g. left: func [s len][copy/part s len] right: func [s len] [copy skip tail s negate len] mid: func [s start len][copy/part at s start len] |
Graham 26-Oct-2008 [11206] | Actually what I would like to do is modify in-situ cut: func [ 'c n ][ set c copy/part get c n ] but this only works for standalone series and not when series are part of an object |
Anton 27-Oct-2008 [11207x2] | Can't you just use a word ? |
>> cut: func [ w n ][ set w copy/part get w n] >> o: context [s: "hello"] >> cut in o 's 3 == "hel" >> o/s == "hel" >> t: "bonjour" == "bonjour" >> cut 't 3 == "bon" | |
Graham 27-Oct-2008 [11209] | Ok |
Anton 27-Oct-2008 [11210x4] | Navigating my public directory: >> change-dir %"~niclasen/" ** Access Error: Bad file path: ~niclasen/ ** Where: clean-path ** Near: get-modes target 'full-path |
The ~niclasen directory was created automatically by path-thru (via load-thru). | |
CLEAN-PATH doesn't like the leading tilde '~'. | |
It's not in RAMBO. R3 doesn't have this problem, but it seems that clean-path doesn't do anything with tildes. (Maybe R3 clean-path is not using the operating system to do path expansion.) | |
ChristianE 27-Oct-2008 [11214] | If by working in situ you mean you'd like to modify the series directly instead of copying parts of it, maybe you can use cut-left: func [series length] [clear skip series length series] cut-right: func [series length] [remove/part series length] cut-middle: func [series start length] [cut-right series start - 1 cut-left series length] |
BrianH 27-Oct-2008 [11215] | Anton, the R3 clean-path is a mezzanine which only applies REBOL expansion rules, not OS-specific ones. |
Anton 27-Oct-2008 [11216] | Aha, I see, clean-path is a much longer function, now that it's not using get-modes. |
BrianH 27-Oct-2008 [11217x2] | Tilde expansion could be added using system/options/home if you like. |
~ is a valid character for filenames on some platforms though. | |
older newer | first last |