This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Find date of second thursday of month

Help for those learning Tcl or writing their own scripts.
J
Jagg
Halfop
Posts: 53
Joined: Sat Jan 24, 2004 11:32 am

Post by Jagg »

@caesar i think that is not that simple... already tried this of course.

but

Code: Select all

if {[catch {set datevalue [clock scan $date]}]} {return e8}
clock scan only work with english monthnames!?
see

Code: Select all

% set datevalue [clock scan 8-march-2011]
1299538800
% set datevalue [clock scan 8-märz-2011]
unable to convert date-time string "8-märz-2011"
and

Code: Select all

if {![string equal -nocase [clock format $datevalue -format %B] $monthname]}
gives me always "0" on none english monthnames like e.g. march ("märz" isn't equal to "march") because above code returns the german monthname

Code: Select all

% clock format 1299538800 -format %B
März
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

I think this problem is likely to be as a result of server/eggdrop language settings rather than timezone, since a timezone will often encompass several countries/languages.

The best solution would be to think about a rewrite of the script to deal with day and month numbers, mapping the numbers to names or names to numbers wherever required. I'll give it some thought but it would obviously be a pretty major rewrite.
I must have had nothing to do
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

Jagg, please give the following script a thorough testing on your system. Input to the command !finddate is the same (English). Output is English.

[13:57] <@arfer> !finddate sunday december 2010 5
[13:57] <osmosis> cannot find 5 Sunday's in December 2010
[13:57] <@arfer> !finddate wednesday december 2010 5
[13:57] <osmosis> Wednesday 29 December 2010

I have done brief tests with both Tcl 8.4/Eggdrop and Tcl 8.5/Windrop but I do not have access to alternative server language settings.

Hopefully it will now work for any internal language settings. Ensure you restart rather than rehash because of the many changes to the code.

Code: Select all

set vFinddateVersion 11.03.08.14.01

bind PUB - !finddate pPubFinddate

proc pPubFinddate {nick uhost hand chan text} {
    set vars [split $text]
    if {[llength $vars] == 4} {
        set day [lindex $vars 0]; set month [lindex $vars 1]; set year [lindex $vars 2]; set number [lindex $vars 3]
        switch -- [set date [pParseFinddate $day $month $year $number]] {
            e1 {putserv "PRIVMSG $chan :invalid or misspelt full day name"}
            e2 {putserv "PRIVMSG $chan :invalid or misspelt full month name"}
            e3 {putserv "PRIVMSG $chan :year value was not an integer"}
            e4 {putserv "PRIVMSG $chan :year value was not the 4 digits expected"}
            e5 {putserv "PRIVMSG $chan :year value was outside the range 1971 through 2037"}
            e6 {putserv "PRIVMSG $chan :number value was not an integer"}
            e7 {putserv "PRIVMSG $chan :number value was outside the range 1 through 5"}
            e8 {putserv "PRIVMSG $chan :cannot find $number [string totitle ${day}'s] in [string totitle $month] $year"}
            default {putserv "PRIVMSG $chan :$date"}
        }
    } else {putserv "PRIVMSG $chan :correct syntax is !finddate <day> <month> <year> <number>"}
    return 0
}

proc pParseFinddate {day month year number} {
    set dayname [string tolower $day]
    set monthname [string tolower $month]
    if {[lsearch -exact {sunday monday tuesday wednesday thursday friday saturday} $dayname] == -1} {return e1}
    if {[lsearch -exact {january february march april may june july august september october november december} $monthname] == -1} {return e2}
    if {![string is integer -strict $year]} {return e3}
    if {[string length $year] != 4} {return e4}
    if {($year < 1971) || ($year > 2037)} {return e5}
    if {![string is integer -strict $number]} {return e6}
    if {($number < 1) || ($number > 5)} {return e7}
    set daynumber [pMappingFinddate $dayname 1]
    set monthnumber [pMappingFinddate $monthname 2]
    set target 1
    for {set loop 1} {$loop <= 32} {incr loop} {
        set date "$year-$monthnumber-[format %02i $loop]"
        if {[catch {set datevalue [clock scan $date]}]} {return e8}
        if {![string equal [clock format $datevalue -format %m] $monthnumber]} {return e8}
        if {[string equal [clock format $datevalue -format %u] $daynumber]} {
            if {[string equal $target $number]} {
                set rday [pMappingFinddate [clock format $datevalue -format %u] 3]
                set rdate [clock format $datevalue -format %d]
                set rmonth [pMappingFinddate [clock format $datevalue -format %m] 4]
                set ryear [clock format $datevalue -format %Y]
                return "$rday $rdate $rmonth $ryear"
            } else {incr target}
        }
    }
    return 0
}

proc pMappingFinddate {input type} {
    switch -- $type {
        1 {set output [string map -nocase {monday 1 tuesday 2 wednesday 3 thursday 4 friday 5 saturday 6 sunday 7} $input]}
        2 {set output [string map -nocase {january 01 february 02 march 03 april 04 may 05 june 06 july 07 august 08 september 09 october 10 november 11 december 12} $input]}
        3 {set output [string map {1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday 7 Sunday} $input]}
        4 {set output [string map {01 January 02 February 03 March 04 April 05 May 06 June 07 July 08 August 09 September 10 October 11 November 12 December} $input]}
        default {}
    }
    return $output
}

putlog "finddate.tcl version $vFinddateVersion loaded"

# eof
I must have had nothing to do
J
Jagg
Halfop
Posts: 53
Joined: Sat Jan 24, 2004 11:32 am

Post by Jagg »

@arfer
...looks really good here now on my CET server! THANKS!!!
Post Reply