summaryrefslogtreecommitdiffstats
path: root/library
diff options
context:
space:
mode:
authorgahr <gahr@gahr.ch>2016-02-17 11:03:48 (GMT)
committergahr <gahr@gahr.ch>2016-02-17 11:03:48 (GMT)
commit03dd6c13603af9d6792030412f52d942fc2eaefd (patch)
tree36dc308e5e2879fff93be3333f2225732a0387fd /library
parentf7ea5b6cc57fafcbcb3377e83fdbc4d999d123d4 (diff)
downloadtcl-03dd6c13603af9d6792030412f52d942fc2eaefd.zip
tcl-03dd6c13603af9d6792030412f52d942fc2eaefd.tar.gz
tcl-03dd6c13603af9d6792030412f52d942fc2eaefd.tar.bz2
[5f71353740] Support the "weekdays" unit in [clock add]
Diffstat (limited to 'library')
-rwxr-xr-xlibrary/clock.tcl56
1 files changed, 52 insertions, 4 deletions
diff --git a/library/clock.tcl b/library/clock.tcl
index 8e4b657..231f1ae 100755
--- a/library/clock.tcl
+++ b/library/clock.tcl
@@ -4248,7 +4248,7 @@ proc ::tcl::clock::add { clockval args } {
?-gmt boolean? ?-locale LOCALE? ?-timezone ZONE?\""
}
if { [catch { expr {wide($clockval)} } result] } {
- return -code error $result
+ return -code error "expected integer but got \"$clockval\""
}
set offsets {}
@@ -4287,9 +4287,6 @@ proc ::tcl::clock::add { clockval args } {
-errorcode [list CLOCK gmtWithTimezone] \
"cannot use -gmt and -timezone in same call"
}
- if { [catch { expr { wide($clockval) } } result] } {
- return -code error "expected integer but got \"$clockval\""
- }
if { ![string is boolean -strict $gmt] } {
return -code error "expected boolean value but got \"$gmt\""
} elseif { $gmt } {
@@ -4326,6 +4323,11 @@ proc ::tcl::clock::add { clockval args } {
$changeover]
}
+ weekdays - weekday {
+ set clockval [AddWeekDays $quantity $clockval $timezone \
+ $changeover]
+ }
+
hours - hour {
set clockval [expr { 3600 * $quantity + $clockval }]
}
@@ -4425,6 +4427,52 @@ proc ::tcl::clock::AddMonths { months clockval timezone changeover } {
#----------------------------------------------------------------------
#
+# AddWeekDays --
+#
+# Add a given number of week days (skipping Saturdays and Sundays)
+# to a given clock value in a given time zone.
+#
+# Parameters:
+# days - Number of days to add (may be negative)
+# clockval - Seconds since the epoch before the operation
+# timezone - Time zone in which the operation is to be performed
+# changeover - Julian Day on which the Gregorian calendar was adopted
+# in the target locale.
+#
+# Results:
+# Returns the new clock value as a number of seconds since the epoch.
+#
+# Side effects:
+# None.
+#
+#----------------------------------------------------------------------
+
+proc ::tcl::clock::AddWeekDays { days clockval timezone changeover } {
+
+ set day [format $clockval -format %u]
+
+ set weeks [expr {$days / 5}]
+ set rdays [expr {$days % 5}]
+ set toAdd [expr {7 * $weeks + $rdays}]
+ set resDay [expr {$day + ($toAdd % 7)}]
+
+ # Adjust if we start from a weekend
+ if {$day > 5} {
+ set adj [expr {5 - $day}]
+ incr toAdd $adj
+ incr resDay $adj
+ }
+
+ # Adjust if we end up on a weekend
+ if {$resDay > 5} {
+ incr toAdd 2
+ }
+
+ AddDays $toAdd $clockval $timezone $changeover
+}
+
+#----------------------------------------------------------------------
+#
# AddDays --
#
# Add a given number of days to a given clock value in a given time