How to get a formatted date and time string from `now`? - rebol

I'm using "Red Programming Language" version "0.6.4" on Windows and making a command line application.
I don't know much Red language and I don't understand many things. I did go over "work in progress" docs at (https://doc.red-lang.org/en/) before asking here.
I need to get a date and time string formatted as yyyymmdd_hhmm.
I've started with code like this:
Red []
dt: to string! now/year
print dt
which gives me 2019 but I need the other things month, day and time to obtain something like 20190608_2146
I tried also:
Red []
dt: to string! now/precise
print dt
which gives me 8-Jun-2019/21:47:51.299-07:00 but again what I needed was 20190608_2147
Question:
How to modify the code above to obtain something like 20190608_2147 from now?
Thank you.

I have written a script for Rebol and Red called 'Form Date' that will format dates/times in a similar fashion to STRFTIME. The Red version is here.
do %form-date.red
probe form-date now "%Y%m%d_%H%M"
print first spec-of :form-date
Within the script are individual snippets of code used for formatting the various components of a date! value.
You don't need the script for your specific example though, you can extract and join the various components thus:
date: now
rejoin [ ; reduce-join
form date/year
pad/left/with date/month 2 #"0"
pad/left/with date/day 2 #"0"
"_"
pad/left/with date/hour 2 #"0"
pad/left/with date/minute 2 #"0"
]

As the above solution has some problems under Rebol2 here a variation that works with Rebol and Red the same way
date: now
rejoin [
date/year
next form 100 + date/month
next form 100 + date/day
"_"
next form 100 + date/time/hour
next form 100 + date/time/minute
]

Here is another way:
rejoin [
now/year
either 10 > x: (now/month) [join "0" x][x]
either 10 > x: (now/day) [join "0" x][x]
"_"
either 10 > x: first (now/time) [join "0" x][x]
either 10 > x: second (now/time) [join "0" x][x]
]
Red has pad, so rgchris' answer up there is good. Yet, there is no need for date: now as rgchris has done:
rejoin [
now/year
pad/left/with now/month 2 #"0"
pad/left/with now/day 2 #"0"
"_"
pad/left/with first (now/time) 2 #"0"
pad/left/with second (now/time) 2 #"0"
]

Related

replace occurance of character between 2 string using mawk only

replace occurance of character in string using mawk
Hi Guys, need this solution using mawk, please help.
I have 2 strings STR_IN & STR_CMPR.
Want to repalce all character of STR_CMPR in STR_IN
main thing is, if STR_IN have some character twice & in STR_CMPR same character is only once then from STR_IN only one character should be replce.
can someone help using mawk only, no other method please.
if it can be achieve using gsub & regex or match & regex then its best. I dont want to run through each character using some loop.
Below are 3 examples with expected output.
eg 1 :
STR_IN="AABBCCDD";
STR_CMPR="DBAC";
if using gsub;
gsub(STR_CMPR, "", STR_IN);
result should be, STR_IN = ABCD;
if using match,
result : STR_IN_MATCH_CNT = 4 out of 8;
eg 2 :
STR_IN="DBAC";
STR_CMPR="AABBCCDD";
if using gsub;
gsub(STR_CMPR, "", STR_IN);
result should be, STR_IN = blank;
if using match;
result : STR_IN_MATCH_CNT = 4 out of 4;
eg 3 :
STR_IN="DDBBAC";
STR_CMPR="AABCCD";
if using gsub,
gsub(STR_CMPR, "", STR_IN);
result should be, STR_IN = DB;
if using match,
result : STR_IN_MATCH_CNT = 4 out of 6;
There is no way out of this, a for loop is needed, the quickest is:
STR_IN_MATCH_CNT=length(STR_IN)
for(i=1;i<=length(STR_CMPR);++i) sub(substr(STR_CMPR,i,1),"",STR_IN)
STR_IN_MATCH_CNT-=length(STR_IN)

Date substraction why don't I get hh:mm:ss in Rebol / Red?

1°) I get only the number of days, how to also get the hh:mm:ss ?
diff: (now - (10-Nov-2017/15:00:00))
6
2°) What's the most elegant way to get the number of minutes ?
1°) I get only the number of days, how to also get the hh:mm:ss ?
Use difference function for that:
>> difference now 10-Nov-2017/15:00:00
== 145:19:24
2°) What's the most elegant way to get the number of minutes ?
The same way as for any other date value:
>> d: difference now 10-Nov-2017/15:00:00
>> d/minute
== 21
Alternatively, you can use pick to avoid setting the intermediary date to a word:
>> pick (difference now 10-Nov-2017/15:00:00) 2 ; Red and Rebol2/3
== 21
>> pick (difference now 10-Nov-2017/15:00:00) 'minute ; Rebol3, not yet implemented in Red
== 21

Rebol calc-workdays not working on Red

I trying to execute this script https://www.mail-archive.com/rebol-bounce#rebol.com/msg01222.html in Red but I can't see why I got
calc-workdays now/date 3-feb-2007 [1-feb-2007]
*** Script Error: > operator is missing an argument
*** Where: do
*** Stack: print calc-workdays
found?: func [
"Returns TRUE if value is not NONE."
value
][
not none? :value
]
calc-workdays: func [
"Return number of workdays between two dates, excluding holidays"
date1 [date!] "Start date"
date2 [date!] "End date"
holidays [block!] "Block of dates to exclude (holidays, etc.)"
/non "Return number of non-work days (weekend + holidays) between 2 dates"
/local days day1 day2 diff param
][
days: copy []
set [day1 day2] sort reduce [date1 date2]
diff: day2 - day1
param: pick [[> 5 union][< 6 exclude]] either found? non [1][2]
loop diff [
day1: day1 + 1
if do param/1 day1/weekday param/2 [append days day1]
]
return length? do param/3 days holidays
]
Rebol2 seems to allow you to pass a WORD! to DO, and evaluate it. If the word happens to be a variable holding an ANY-FUNCTION!, it will be run...and if it's an infix "OP!" then it will be run as if it were not infix.
>> do quote > 1 2
== false
>> do quote < 1 2
== true
Red sort of does this also, but DO is not variadic. It can run 0 arity functions only:
>> foo: does [print "hi"]
>> do quote foo
hi
>> bar: func [x] [print x]
>> do quote bar "hi"
*** Script Error: bar is missing its x argument
The script in question was attempting to use this feature. But it could be done with ordinary COMPOSE or REDUCE. So change:
if do param/1 day1/weekday param/2 [append days day1]
To:
if do reduce [day1/weekday param/1 param/2] [append days day1]
That will build a block of code where the > or < operator is in the middle, and execute it normally without relying on this WORD!-dispatch or infix-dropping behavior of DO.
Similarly, change:
return length? do param/3 days holidays
To:
return length? do reduce [param/3 days holidays]
With those changes, and removing the found? (it's not necessary) then it appears to work.

Twitter api python - max number of objects

so I am calling the twitter api:
openurl = urllib.urlopen("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&contributor_details&include_rts=true&screen_name="+user+"&count=3600")
and it returns some long file like:
[{"entities":{"hashtags":[],"user_mentions":[],"urls":[{"url":"http:\/\/t.co\/Hd1ubDVX","indices":[115,135],"display_url":"amzn.to\/tPSKgf","expanded_url":"http:\/\/amzn.to\/tPSKgf"}]},"coordinates":null,"truncated":false,"place":null,"geo":null,"in_reply_to_user_id":null,"retweet_count":2,"favorited":false,"in_reply_to_status_id_str":null,"user":{"contributors_enabled":false,"lang":"en","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/151701304\/theme14.gif","favourites_count":0,"profile_text_color":"333333","protected":false,"location":"North America","is_translator":false,"profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/151701304\/theme14.gif","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1642783876\/idB005XNC8Z4_normal.png","name":"User Interface Books","profile_link_color":"009999","url":"http:\/\/twitter.com\/ReleasedBooks\/genres","utc_offset":-28800,"description":"All new user interface and graphic design book releases posted on their publication day","listed_count":11,"profile_background_color":"131516","statuses_count":1189,"following":false,"profile_background_tile":true,"followers_count":732,"profile_image_url":"http:\/\/a2.twimg.com\/profile_images\/1642783876\/idB005XNC8Z4_normal.png","default_profile":false,"geo_enabled":false,"created_at":"Mon Sep 20 21:28:15 +0000 2010","profile_sidebar_fill_color":"efefef","show_all_inline_media":false,"follow_request_sent":false,"notifications":false,"friends_count":1,"profile_sidebar_border_color":"eeeeee","screen_name":"User","id_str":"193056806","verified":false,"id":193056806,"default_profile_image":false,"profile_use_background_image":true,"time_zone":"Pacific Time (US & Canada)"},"possibly_sensitive":false,"in_reply_to_screen_name":null,"created_at":"Thu Nov 17 00:01:45 +0000 2011","in_reply_to_user_id_str":null,"retweeted":false,"source":"\u003Ca href=\"http:\/\/twitter.com\/ReleasedBooks\/genres\" rel=\"nofollow\"\u003EBook Releases\u003C\/a\u003E","id_str":"136957158075011072","in_reply_to_status_id":null,"id":136957158075011072,"contributors":null,"text":"Digital Media: Technological and Social Challenges of the Interactive World - by William Aspray - Scarecrow Press. http:\/\/t.co\/Hd1ubDVX"},{"entities":{"hashtags":[],"user_mentions":[],"urls":[{"url":"http:\/\/t.co\/GMCzTija","indices":[119,139],"display_u
Well,
the different objects are slit into tables and dictionaries and I want to extract the different parts but to do this I have to know how many objects the file has:
example:
[{1:info , 2:info}][{1:info , 2:info}][{1:info , 2:info}][{1:info , 2:info}]
so to extract the info from 1 in the first table I would:
[0]['1']
>>>>info
But to extract it from the last object in the table I need to know how many object the table has.
This is what my code looks like:
table_timeline = json.loads(twitter_timeline)
table_timeline_inner = table_timeline[x]
lines = 0
while lines < linesmax:
in_reply_to_user_id = table_timeline_inner['in_reply_to_status_id_str']
lines += 1
So how do I find the value of the last object in this table?
thanks
I'm not entirely sure this is what you're looking for, but to get the last item in a python list, use an index of -1. For example,
>>> alist = [{'position': 'first'}, {'position': 'second'}, {'position': 'third'}]
>>> print alist[-1]['position']
{'position': 'third'}
>>> print alist[-1]['position']
third

how to use less condition for many condition?

i mean for e.g., numbers are between 1 and 100 .i want to show messagebox for each number as text for example "One" for 1.
is it possible ?
anyone help!
I wrote a similar program a few years ago. Create a String array with One, Two..Twenty, Thirty, Forty...Hundred
Then with the input number check if <20 just print that array index.
if(num < 20)
print arr[num]
if num>20
print arr[num/10 + 19] + " " + arr[num%10]
I hope you get the drift....