How to count weekend days between two dates

February 6th, 2012 No comments

This VB.NET method returns the total number of weekend days between two dates.

Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Public Function CountWeekendDays(ByVal FromDate As Date, ByVal ToDate As Date)
Dim TotalDaysInt As Integer = ToDate.Date.Subtract(FromDate.Date).Days
Dim WeeksInt As Integer = Math.Floor(TotalDaysInt / 7)
Dim WeekendDaysInt As Integer = WeeksInt * 2
If TotalDaysInt Mod 7 <> 0 Then
If FromDate.DayOfWeek = DayOfWeek.Sunday Then
WeekendDaysInt += 1
Else
Dim SaturdayOffsetInt = Math.Max((TotalDaysInt Mod 7) - (DayOfWeek.Saturday - FromDate.DayOfWeek), 0)
WeekendDaysInt += Math.Min(SaturdayOffsetInt, 2)
End If
End If
Return WeekendDaysInt
End Function

It works by multiplying the number of full weeks by 2, then it takes the leftover days and calculates how many of those days overlap the weekend.

For example:
Sunday = 0, Monday = 1, Tuesday = 2 …(and so on)

Let’s say FromDate is Wednesday, and the total number of days to ToDate is 18:

FromDate = Wednesday = 3
TotalDays = 18

TotalWeeks = Math.Floor(18 / 7) = 2
LeftoverDays = (TotalDays Mod 7) = (TodalDays – (TotalWeeks * 7)) = 4
DaysUntilSaturday = (Saturday – CheckInDate) = (6 – 3) = 3
DaysThatOverlapSaturday = (LeftoverDays – DaysUntilSaturday) = (4 – 3) = 1

Saturday is overlapped by 1 day, so we add 1 to the total number of weekend days.

WeekendDays = ((TotalWeeks * 2) + DaysThatOverlapSaturday) = (4 + 1) = 5

Of course if DaysThatOverlapSaturday < 1, don’t add anything.

If TotalDays = 19, then there would be 2 days that overlap Saturday. Since we know Sunday is the next day, we add both days.
If TotalDays = 20, then there would be 3 days that overlap Saturday, but that third day is Monday, so we only add 2.
If TotalDays = 21, then that’s 3 full weeks, and we know that 3 full weeks will always contain 6 weekend nights, so there’s nothing more to do.

The only other case is if the total number of days aren’t divisible by 7 and the Check-In date is Sunday. Since we know the remaining number of days are less then 7, we only have to add 1 for that Sunday.

Categories: VB.NET Tags:

Go board with canvas tag

July 7th, 2011 4 comments

Here’s my first attempt at a Go board with javascript and the canvas tag.

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html>
<head>
    <title>Simple Go board with canvas tag</title>
    <script language="JavaScript" type="text/javascript">
<!-- //
    var Go = {
        nextMove : 'black'
    };
    Go.board = {
        size : 19, // breaks if > 25
        margin : 30,
        bgColor : '#FFA54F'
    }
    Go.apply = function(o, c){
        if(o && c && typeof c == 'object'){
            for(var prop in c){
                o[prop] = c[prop];
            }
        }
        return o;
    };
    Go.apply(Go.board, {
        xLabel : 'abcdefghjklmnopqrstuvwxyz',
        map : new Array(),
        stoneWidth : 0,
        stoneHeight : 0,
        getStarPoints : function(){
            // I'd like an algorithm for this
            var result = new Array();
            switch (this.size){
                case 19:
                    result.push([4,4],[10,4],[16,4],[4,10],[10,10],[16,10],[4,16],[10,16],[16,16]);
                    break;
                case 13:
                    result.push([4,4],[10,4],[7,7],[4,10],[10,10]);
                    break;
                case 9:
                    result.push([3,3],[7,3],[5,5],[3,7],[7,7]);
                    break;
            }
            return result;
        },
        getCoords : function(x,y){
            return this.map[x][y];
        },
        buildMap : function(){
            var m = (this.margin||0);
            var hLineGap = (Go.canvas.height-(m*2))/(this.size-1);
            var vLineGap = (Go.canvas.width-(m*2))/(this.size-1);
            var x, y, lat, lon;
            // a convenient moment to determine stone size.
            this.stoneWidth = vLineGap/2;
            this.stoneHeight = hLineGap/2;
            for (lon=0; lon<this.size; lon++){
                x = (lon*vLineGap)+m;
                this.map[lon+1] = [];
                for (lat=0; lat<this.size; lat++){
                    y = (lat*hLineGap)+m;
                    this.map[lon+1][lat+1] = [x,y];
                }
            }
        },
        drawBackground : function(){
            var c = Go.cxt;
            c.fillStyle = this.bgColor;
            c.fillRect(0,0,Go.canvas.width, Go.canvas.height);
        },
        drawStone : function(x,y,color){
            var coords = this.getCoords(x,y);
            if (undefined==coords){return false;}
            var c = Go.cxt;
            if (!color){
                color = Go.nextMove;
                Go.nextMove = (color=='black')?'white':'black';
            }
            c.beginPath();
            c.arc(coords[0],coords[1],this.stoneWidth,0,Math.PI*2,true);
            c.fillStyle = color;
            c.fill();
            c.stroke();
        },
        drawStar : function(x,y){
            var coords = this.getCoords(x,y);
            if (undefined==coords){return false;}
            var c = Go.cxt;
            c.beginPath();
            c.arc(coords[0],coords[1],4,0,Math.PI*2,true);
            c.fillStyle = 'black';
            c.fill();
            c.stroke();
        },
        drawStars : function(){
            var stars = this.getStarPoints();
            for (var i=0; i<stars.length; i++){
                star = stars[i];
                this.drawStar(star[0], star[1]);
            }
        },
        drawLines : function(){
            var c = Go.cxt, fc, tc;
            for (var i=1; i<=this.size; i++){
                c.beginPath();
                fc = this.getCoords(1, i);
                tc = this.getCoords(this.size, i);
                c.moveTo(fc[0], fc[1]);
                c.lineTo(tc[0], tc[1]);
                c.stroke();
            }
            for (var i=1; i<=this.size; i++){
                c.beginPath();
                fc = this.getCoords(i, 1);
                tc = this.getCoords(i, this.size);
                c.moveTo(fc[0], fc[1]);
                c.lineTo(tc[0], tc[1]);
                c.stroke();
            }
        },
        drawLabels : function(){
            var c = Go.cxt, f1, f2;
            c.textAlign = 'center';
            c.textBaseline = 'middle';
            for (var i=1; i<=this.size; i++){
                f1 = this.getCoords(i, 1);
                f2 = this.getCoords(i, this.size);
                c.strokeText(this.xLabel.charAt(i-1).toUpperCase(), f1[0], f1[1]-(this.margin/2));
                c.strokeText(this.xLabel.charAt(i-1).toUpperCase(), f2[0], f2[1]+(this.margin/2));
            }
            for (var i=1; i<=this.size; i++){
                f1 = this.getCoords(1, i);
                f2 = this.getCoords(this.size, i);
                c.strokeText(i, f1[0]-(this.margin/2), f1[1]);
                c.strokeText(i, f2[0]+(this.margin/2), f2[1]);
            }
        },
        monitorMouseMove : function(e){
            var x, y, mx, my,
            sw = Go.board.stoneWidth,
            sh = Go.board.stoneHeight;
            if(e.offsetX) {
                mx = e.offsetX;
                my = e.offsetY;
            }
            else if(e.layerX) {
                mx = e.layerX;
                my = e.layerY;
            }
            x = Math.round((mx+(sw/2))/(2*sw));
            y = Math.round((my+(sw/2))/(2*sh));
        },
        monitorMouseClick : function(e){
            // this isn't really perfected..
            var x, y, mx, my,
            sw = Go.board.stoneWidth,
            sh = Go.board.stoneHeight,
            m = Go.board.margin;
            if(e.offsetX) {
                mx = e.offsetX;
                my = e.offsetY;
            }
            else if(e.layerX) {
                mx = e.layerX;
                my = e.layerY;
            }
            x = Math.round((mx+(m/2)+(sw/2))/(2*sw));
            y = Math.round((my+(m/2)+(sw/2))/(2*sh));
            Go.board.drawStone(x,y);
        }
    });
    var start = function(){
        Go.canvas = document.getElementById('goboard');
        Go.canvas.onmousemove = Go.board.monitorMouseMove;
        Go.canvas.onclick = Go.board.monitorMouseClick;
        Go.cxt = Go.canvas.getContext('2d');
        Go.board.buildMap();
        Go.board.drawBackground();
        Go.board.drawLines();
        Go.board.drawStars();
        Go.board.drawLabels();
    }            
// -->
    </script>
</head>
<body onLoad="start();">
    <canvas id="goboard" name="goboard" width="900" height="900">
    </canvas>
</body>
</html>
Categories: Javascript, 囲碁 Go Tags:

Resetting a Mac’s hardware

March 4th, 2009 No comments

There are a lot of little hidden support features built in to a Mac that you’ll only ever find out if you dig deep or call Apple tech support directly. One quite useful procedure I recently learned of is how to reset all hardware in your mac. This may be useful if you are experiencing odd behavior that doesn’t seem software related.

Step 1: Shut down the Mac normally.
Step 2: Remove all power and peripherals. (Pull every last cord out of the back)
Step 3: Hold the power button in for 10 seconds. (I believe this is to ensure that all power is thoroughly drained from the system as there are certain electronic components that can hold on to a charge for quite some time.)
Step 4: Plug everything back in.
Step 5: Turn on the power and hold command+alt+p+r
Keep these keys held until you hear the boot up chime ring twice. There may be a slight pause between chimes.

Once this is complete, resume using the Mac normally and see if your problems have been solved. (If not, don’t despair. This is after all only one of a number of possible troubleshooting steps)

Categories: Misc Tags: ,

Fantastic Frameworks

February 16th, 2009 No comments

Perhaps I’ll write more about this later, but for now I’ll just say that I’ve been using a lot of the Zend Framework and Ext Javascript library. I enjoy writing code with these fantastic tools.
Zend Framework
Ext Javascript Library

Categories: Javascript, PHP Tags: ,

Useful Javascript – Countdown timer

January 22nd, 2008 No comments

Javascript isn’t always the easiest stuff to write. Browser issues, debugging (at least before the days of firebug), etc. Anybody who’s tried anything more than a simple alert() knows this. So I’m always happy when I come across a free pre-written library for accomplishing tasks that would have otherwise taken me more than a few minutes to do myself. Here’s one that just happened to gracefully meet my every need for a countdown timer:

Countdown Pro

Categories: Javascript Tags:

Dr. Who theme

July 24th, 2007 1 comment

I particularly like the opening theme music to the classic sci-fi television series “Dr. Who”. Here’s how it was made.

UPDATE: It’s gone! youtube took it away! super-drat.

UPDATE 2 I found it again! Here’s the original video I embedded here:

Categories: Misc Tags:

Joost – the future of TV?

May 23rd, 2007 No comments

I’ve recently discovered an interesting new application that claims to be the future of TV: www.joost.com
It’s currently in the beta stages of development and much like when Google’s Gmail first appeared, it’s by invitation only. I happened to come across a blog (by another Matt) who offered free invitations, so I asked and quickly received. (thanks Matt!) Visit his blog at http://whatismatt.com/.

I’m enjoying this program so far. The interface is rather nice, and the channel lineup is growing. Video quality is many times greater than what youtube offers. I can watch it full-screen on my 24-inch iMac and although it’s not quite HD, it’s at least as good as regular television. I imagine as broadband speeds increase, so will the available quality of the stream.

Categories: Misc Tags:

Furigana in Office 2004 for Macintosh

April 18th, 2007 2 comments

I’ve finally discovered the method for enabling furigana in Office 2004 for the Mac.

1) Browse to the following folder in Finder:
“Applications” -> “Microsoft Office 2004″ -> “Additional Tools” -> “Microsoft Language Register”

2) Inside this folder is an application called “Microsoft Language Register”. Do not open this application directly, rather drag and drop the “Microsoft Word” application itself onto the language register program. A prompt will appear allowing you to switch the editing environment to Japanese. (I know this seems a little odd, but apparently it’s the way to do it.)

3) While editing a document, highlight the Kanji you want to apply furigana to and choose Format -> Phonetic guide. Enter the text you want to appear as furigana in the “Ruby Text” field, and hit “OK”.

Categories: Mac, 日本語 Japanese Tags:

KUIS entrance ceremony speech

April 18th, 2007 No comments

On the 4th of March, I gave a speech at the Kanda University of International Studies entrance ceremony.

I’ve given public speeches before, but nothing nearly this size. The auditorium was filled with around 500 people, all in formal attire. And did I mention I gave the speech in Japanese? Just a little pressure there..
Although I did end up slipping up in a couple spots, I think the reception was quite positive overall.

The video was taken by my friend Jani with a little Kodak EasyShare v550 digital camera. Thanks Jani!

Here’s a transcript of the original speech:
Speech Transcript

Categories: 神田外語大学 KUIS Tags:

Full Bloom

April 18th, 2007 No comments

After my parents left for China, I visited Ueno again with my brother, Jani, Yuki, and Tuure. This time the blossoms were in full bloom and I got some pretty good pictures.

3163
3175
Categories: 日本 Japan Tags: