Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Displaying AM / PM times

 [1/10] from: john::thousand-hills::net at: 26-Aug-2001 8:48


In reading and displaying lines of data from a text array with this script: data: read/lines %route.txt d7: pick data 7 s7: pick data 7 replace/all s7 tab "</CENTER></TD><TD><CENTER>" s7: rejoin [ "<tr bordercolor=lightblue border=1 bgcolor=white><TD> <CENTER>" s7 "</CENTER></TD></tr>" ] either d7 [ print s7 ] [ "" ] How can I turn the printed elements in that line to reflect a change from " 04:30 " to read " 04:30AM " or " 16:30 " to read " 04:30PM " ? ..JOHN

 [2/10] from: allenk:powerup:au at: 27-Aug-2001 8:48


Hi John, I believe Carl is the origin of this one.. nice-time: func [time /local am][ time/3: 0 am: pick [" AM" " PM"] time < 12:00 if time >= 12:00 [time: time - 12:00] if time/1 = 0 [time/1: 12] rejoin [time am] ]
>> nice-time 16:30
== "4:30 PM"
>> nice-time 4:30
== "4:30 AM" Cheers, Allen K ----- Original Message ----- From: "john" <[john--thousand-hills--net]> To: <[rebol-list--rebol--com]> Cc: <[Time--mindspring--com]>

 [3/10] from: john:thousand-hills at: 26-Aug-2001 21:35


Allen: The code works fine, as lond as I enter it from the console or point it to a specific piece of data. It doesn't work ( I am doing something that confuses it) when I place it in a table trying to print elements of an array line. The data in the line is correct in format, at least Excel converts it ok. ;Here are some specific script that refers to one line of data from the array including this row: ;" 4:56 5:01 5:07 5:17 5:27 5:30 5:35 5:43 5:52 5:57 6:05" d7: pick data 7 either d7 [ s7: pick data 7 replace/all s7 tab "</CENTER></TD><TD WIDTH=6%><CENTER>" s7: rejoin [ "<tr bordercolor=lightblue border=1 bgcolor=white><TD WIDTH=6%><CENTER>" s7 "</CENTER></TD></tr>" ] ] [ "" ] either d7 [ print s7 ] [ "" ] ;I can send the file and data if you want them. John At 08:48 AM 8/27/2001 +1000, you wrote:

 [4/10] from: joel:neely:fedex at: 26-Aug-2001 17:08


Hi, John, john wrote:
> The code works fine, as lond as I enter it from the console or > point it to a specific piece of data.
<<quoted lines omitted: 7>>
> 4:56 5:01 5:07 5:17 5:27 5:30 5:35 > 5:43 5:52 5:57 6:05"
If I read your email correctly, you're dealing with a "line of data" that is a string. In order to manipulate its content as TIME! values, you have to break it apart and convert the pieces to the appropriate type. Here are some test data...
>> stringline: "4:56 5:01 5:07 5:17 5:27 5:30 5:35"
== {4:56 5:01 5:07 5:17 5:27 5:30 5:35} ...and here's the function that Allen recommended...
>> nice-time: func [time /local am][
[ time/3: 0 [ am: pick [" AM" " PM"] time < 12:00 [ if time >= 12:00 [time: time - 12:00] [ if time/1 = 0 [time/1: 12] [ rejoin [time am] [ ] ...and here's one way to get them together:
>> foreach val parse stringline none [
[ print nice-time to-time val [ ] 4:56 AM 5:01 AM 5:07 AM 5:17 AM 5:27 AM 5:30 AM 5:35 AM The PARSE STRINGLINE NONE breaks the line into a block of strings (breaking on whitespace). The FOREACH steps VAL through those strings, and the body of the loop converts each one to a TIME! value which is pretty-printed. HTH! -jn- -- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

 [5/10] from: sqlab:gmx at: 27-Aug-2001 9:40


Hi I use 12 hours schemes very rarely, but I see nice-time giving two times per day the same time. 12:00 ==> 12:00 PM 24:00 ==> 12:00 PM . So I would use if time/1 = 0 [time/1: 12 am: " NOON"] .) What's the correct usage in your place? AR

 [6/10] from: joel:neely:fedex at: 27-Aug-2001 2:54


[sqlab--gmx--net] wrote:
> I use 12 hours schemes very rarely, but I see nice-time giving > two times per day the same time. > > 12:00 ==> 12:00 PM > 24:00 ==> 12:00 PM >
J. Random Manonthestreet will not likely be comfortable with identifying which of 12:00 AM and 12:00 PM are "noon" and midnight , but will have to stop and think about it. Also, 24:00 is not a valid 24-hour time *of day* (00:00..23:59), although it is a completely valid time *interval*. REBOL blurs the distinction between those two meanings of the word "time", which allows conversations like the following:
>> foo: 37:48 == 37:48 >> print foo 37:48
<<quoted lines omitted: 3>>
>> now/time == 2:25:20 >> now/time + foo == 40:13:24
Evaluating DATE! + TIME! appears to normalize the time to time-of-day, while calculating TIME! + TIME! appears not to do so. (Good reasons not to use 12-hour time... ;-)
> So I would use > if time/1 = 0 [time/1: 12 am: " NOON"] > .) >
Not quite! The full set of exceptions and boundaries is: 00:00 => 12:00 MIDNIGHT 00:01 => 12:01 AM ... ... 00:59 => 12:59 AM 01:00 => 01:00 AM ... ... 11:59 => 11:59 AM 12:00 => 12:00 NOON 12:01 => 12:01 PM 12:59 => 12:59 PM 13:00 => 01:00 PM ... ... 23:29 => 11:59 PM
> > I believe Carl is the origin of this one.. > >
<<quoted lines omitted: 6>>
> > ] > >
Incorporating the above tweaks, we get a "human time-of-day" function: humantod: func [t [time!] /local h suf] [ t/3: 0 suf: pick ["AM" "PM"] 12 > h: t/1: t/1 // 24 if h >= 12 [t: t - 12:00] if t/1 = 0 [ t/1: 12 if t = 12:00 [ suf: pick ["NOON" "MIDN"] 12 = h ] ] rejoin [t " " suf] ] which can be tested as follows:
>> do func [/local it tt ht] [
[ for it 0 86400 30 * 60 [ [ ht: humantod tt: to-time it [ print [ht tt it] [ ] [ ] 12:00 MIDN 0:00 0 12:30 AM 0:30 1800 1:00 AM 1:00 3600 1:30 AM 1:30 5400 [snip] 8:30 AM 8:30 30600 9:00 AM 9:00 32400 9:30 AM 9:30 34200 10:00 AM 10:00 36000 10:30 AM 10:30 37800 11:00 AM 11:00 39600 11:30 AM 11:30 41400 12:00 NOON 12:00 43200 12:30 PM 12:30 45000 1:00 PM 13:00 46800 1:30 PM 13:30 48600 [snip] 9:30 PM 21:30 77400 10:00 PM 22:00 79200 10:30 PM 22:30 81000 11:00 PM 23:00 82800 11:30 PM 23:30 84600 12:00 MIDN 24:00 86400 HTH! -jn- -- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

 [7/10] from: sanghabum:aol at: 27-Aug-2001 13:53


[joel--neely--fedex--com]:
> Also, 24:00 is not a valid 24-hour time *of day* (00:00..23:59), > although it is a completely valid time *interval*. REBOL blurs > the distinction between those two meanings of the word "time", > which allows conversations like the following: >
A minor, even pedantic, correction here. ISO 8601 does allow 24:00:00, but no other "24" value--the minutes and seconds must be zero. It allows you to distinguish: 00:00:00 29-Aug-2001 and 24:00:00 29-Aug-2001 which is the "same as" 00:00:00 30-Aug-2001, except it's the last moment of the previous day, rather than the first moment of the next day). Whether this is a useful distinction or not, depends on your application and your user's expectations. The ISO standards do not define formats for AM/PM, and general confusion (differing local conventions ) reigns as to the meaning of 12:00AM and 12:00PM. (Another goodreason not to use 12-hour time... ;-) You can use "noon" and "midnight" to replace these, but remember that midnight is ambiguous---is "midnight 31Aug-2001" in the mind of the reader the "last moment of August" or the "first moment of its last day"? --Colin

 [8/10] from: john:thousand-hills at: 30-Aug-2001 20:35


I have a REBOL file I am using that presents bus schedules in a format readable for the blind. The data is dumped into an ASCII array, I read it by line and would like to modify the line to include AM or PM before REBOL places the elements into the HTML table. I have sniped the code to reflect what is done with one single line.: This is what the line looks like and it is tab delimited: 7:14 7:17 7:23 7:26 7:32 7:40 7:43 7:47 7:54 8:03 8:08 8:16 8:26 How can I change the data in d7 to read AM/PM before it goes to the table, I need to retain the tabs? Here is the code: Time conversion works from console ntime: func [time /local am][ time/3: 0 am: pick [" AM" " PM"] time < 12:00 if time >= 12:00 [time: time - 12:00] if time/1 = 0 [time/1: 12] rejoin [time am] ] data: read/lines %route.txt d7: pick data 7 either d7 [ s7: pick data 7 replace/all s7 tab "</CENTER></TD><TD WIDTH=6%><CENTER>" s7: rejoin [ "<tr bordercolor=lightblue border=1 bgcolor=white><TD WIDTH=6%><CENTER>" s7 "</CENTER></TD></tr>" ] ] [ "" ] john

 [9/10] from: joel:neely:fedex at: 30-Aug-2001 17:45


Hi, John, john wrote:
> I have a REBOL file I am using that presents bus schedules in a format > readable for the blind.
<<quoted lines omitted: 6>>
> How can I change the data in d7 to read AM/PM before it goes to the > table, I need to retain the tabs?
The key is to turn the tab-delimited line into a block of strings, each of which can be converted to a TIME! value for submission to the time formatting function. Using your definition of "nice time" formatting, I whomped up the following demo (pretending that D7 was read from a file) in a source file named timeform.r ... REBOL [] d7: {8:17^-10:26^-12:40^-14:47^-16:03} nicetime: func [time [time!] /local am] [ time/3: 0 am: pick [" AM" " PM"] time < 12:00 if time >= 12:00 [time: time - 12:00] if time/1 = 0 [time/1: 12] rejoin [time am] ] makerow: func [s [string!] /local row] [ row: copy {<tr bordercolor="lightblue" border="1" bgcolor="white">} foreach t parse s none [ append row rejoin [ "^/" <td width="6%" align="center"> nicetime to-time t </td> ] ] rejoin [row "^/" </tr>] ] makerow d7 ...which produces the following output...
>> print do %timeform.r
<tr bordercolor="lightblue" border="1" bgcolor="white"> <td width="6%" align="center">8:17 AM</td> <td width="6%" align="center">10:26 AM</td> <td width="6%" align="center">12:40 PM</td> <td width="6%" align="center">2:47 PM</td> <td width="6%" align="center">4:03 PM</td> </tr>
>>
HTH! -jn- -- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

 [10/10] from: al:bri:xtra at: 31-Aug-2001 17:19


>> Line7: "7:14 7:17 7:23 7:26 7:32 7:40 7:43 7:47 7:54 8:03 8:08 8:16 8:26"
== {7:14 7:17 7:23 7:26 7:32 7:40 7:43 7:47 7:54 8:03 8:08 8:16 8:26}
>> d7: load line7
== [7:14 7:17 7:23 7:26 7:32 7:40 7:43 7:47 7:54 8:03 8:08 8:16 8:26 ]
>> map d7 :ntime
== ["7:14 AM" "7:17 AM" "7:23 AM" "7:26 AM" "7:32 AM" "7:40 AM" "7:43 AM" 7:47 AM "7:54 AM" "8:03 AM" "8:08 AM" "8:16 AM" "8 :26 A...
>> source map
map: func [ {Maps or applies the function to all elements of the series.} Arg1 [any-function! series!] Arg2 [any-function! series!] /Only "Inserts the result of the function as a series." /local Result Results Function Series][ any [ all [ any-function? :Arg1 series? :Arg2 (Function: :Arg1 Series: :Arg2 true) ] all [ any-function? :Arg2 series? :Arg1 (Function: :Arg2 Series: :Arg1 true) ] throw make error! reduce [ 'script 'cannot-use rejoin [ {"} mold 'Map " " mold type? :Arg1 {"} ] rejoin [ {"} mold type? :Arg2 {"} ] ] ] Results: make Series length? Series do reduce [ 'foreach Arguments :Function 'Series compose [ if not unset? set/any 'Result Function (Arguments :Function) [ either only [ insert/only tail Results :Result ] [ insert tail Results :Result ] ] ] ] Results ] I hope that helps! Andrew Martin ICQ: 26227169 http://zen.scripterz.org

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted