Microsoft Small Basic

Program Listing:
Embed this in your website
' EarthClock02.smallbasic - Earth Clock v0.2 (C) 2011 Nonki Takahashi

' History:
' v0.1 2011/04/25 Created from Alarm v0.1 MDW647 (85 lines PPH681)
' v0.2 2011/04/26 Hour numerals and shadow added (210 lines PPH681-0)

' Main
rLocal = 9 ' local time for Japan
sVersion = "v0.2"
GraphicsWindow.Title = "Earth Clock " + sVersion
Clock_Init()
Alarm_Init()
Earth_Init()
Earth_DrawHourNumerals()
While "True"
  If bTick Then
    Clock_ShowDigitalTime()
    Clock_ShowDigitalDate()
    Earth_Rotate()
    Alarm_Ring()
    bTick = "False"
  Else
    Program.Delay(500)
  EndIf
EndWhile

' Alarm | Initialization
Sub Alarm_Init
  iAX = 18  ' character size for alarm
  iAY = 26
  iAL0 = 11 ' alarm y position in line
  GraphicsWindow.FontSize = iAY
  GraphicsWindow.BrushColor = sFGColor
  GraphicsWindow.BackgroundColor = sBGColor
  GraphicsWindow.DrawText(iAX * 3, iAY * iAL0, "ALARM:")
  GraphicsWindow.DrawText(iAX * 21, iAY * iAL0, ":")
  GraphicsWindow.BrushColor = "Black"
  oHour = Controls.AddTextBox(iAX * 11, iAY * iAL0)
  oMin = Controls.AddTextBox(iAX * 23, iAY * iAL0)
  GraphicsWindow.Height = iAY * (iAL0 + 2.5)
EndSub

' Alarm | Ring
Sub Alarm_Ring
  iAHour = Controls.GetTextBoxText(oHour)
  iAMin = Controls.GetTextBoxText(oMin)
  If (iHour = iAHour) And (iMin = iAMin) Then
    Sound.PlayChimesAndWait()
  EndIf
EndSub

' Clock | Initialization
Sub Clock_Init
  uFolder = "http://homepage2.nifty.com/nobukit/smallbasic.files/"
  iTX = 6 * 6 ' character size for time
  iTY = 10 * 6
  iTX0 = 260
  iHX = 9   ' character size for hour numerals
  iHY = 13
  sBGColor = "Black"  ' background color
  sFGColor = "White"  ' foreground color
  Timer.Interval = 1000
  Timer.Tick = Clock_OnTick
  bTick = "False"
  iDaySec = 24 * 60 * 60
EndSub

' Clock | Event processing on tick
Sub Clock_OnTick
  iHour = Clock.Hour
  iMin = Clock.Minute
  iSec = Clock.Second
  iYear = Clock.Year
  iMonth = Clock.Month
  iDay = Clock.Day
  bTick = "True"
EndSub

' Clock | Show digital date
Sub Clock_ShowDigitalDate
  sDate = iYear + "/"
  If iMonth < 10 Then
    sDate = sDate + "0" + iMonth + "/"
  Else
    sDate = sDate + iMonth + "/"
  EndIf
  If iDay < 10 Then
    sDate = sDate + "0" + iDay
  Else
    sDate = sDate + iDay
  EndIf
  GraphicsWindow.FontSize = iAY
  GraphicsWindow.BrushColor = sBGColor
  GraphicsWindow.FillRectangle(iAX * 3 + iTX0, iAY * 5, iAX * 10, iAY * 1.1)
  GraphicsWindow.BrushColor = sFGColor
  GraphicsWindow.DrawText(iAX * 3 + iTX0, iAY * 5 - 3, sDate)
EndSub

' Clock | Show digital time
Sub Clock_ShowDigitalTime
  If iHour < 10 Then
    sTime = Text.Append("0", iHour) + ":"
  Else
    sTime = iHour + ":"
  EndIf
  If iMin < 10 Then
    sTime = sTime + "0" + iMin + ":"
  Else
    sTime = sTime + iMin + ":"
  EndIf
  If iSec < 10 Then
    sTime = sTime + "0" + iSec
  Else
    sTime = sTime + iSec
  EndIf
  GraphicsWindow.FontSize = iTY
  GraphicsWindow.BrushColor = sBGColor
  GraphicsWindow.FillRectangle(iTX * 1.4 + iTX0, iTY, iTX * 8, iTY * 1.1)
  GraphicsWindow.BrushColor = sFGColor
  GraphicsWindow.DrawText(iTX * 1.4 + iTX0, iTY - 3, sTime)
EndSub

' Earth | Draw hour numerals
Sub Earth_DrawHourNumerals
  GraphicsWindow.BrushColor = sFGColor
  GraphicsWindow.FontSize = iHY
  For iHour = 0 To 22 Step 2
    iX = iXC - Math.Round(iR * 1.1 * Math.Sin(iHour * 2 * 3.14159 / 24) + iHX / 2)
    iY = iYC - Math.Round(iR * 1.1 * Math.Cos(iHour * 2 * 3.14159 / 24) + iHY / 2)
    GraphicsWindow.DrawText(iX, iY, iHour)
  EndFor
EndSub

' Earth | Initialization
Sub Earth_Init
  ' time table initialization
  sCity[0] = "GMT"  ' Greenwich Mean Time
  rDelta[0] = 0
  sCity[1] = "Paris"
  rDelta[1] = 1
  sCity[2] = "Cairo"
  rDelta[2] = 2
  sCity[3] = "Moscow"
  rDelta[3] = 3
  sCity[4] = "Dubai"
  rDelta[4] = 4
  sCity[5] = "Karachi"
  rDelta[5] = 5
  sCity[6] = "Dhaka"
  rDelta[6] = 6
  sCity[7] = "Bangkok"
  rDelta[7] = 7
  sCity[8] = "Hong Kong"
  rDelta[8] = 8
  sCity[9] = "Tokyo"
  rDelta[9] = 9
  sCity[10] = "Sydney"
  rDelta[10] = 10
  sCity[11] = "Noumea"
  rDelta[11] = 11
  sCity[12] = "Wellington"
  rDelta[12] = 12
  sCity[13] = "Midway Island"
  rDelta[13] = -11
  sCity[14] = "Honolulu"
  rDelta[14] = -10
  sCity[16] = "Los Angeles"
  rDelta[16] = -8
  sCity[17] = "Denver"
  rDelta[17] = -7
  sCity[18] = "Chicago"
  rDelta[18] = -6
  sCity[19] = "New York"
  rDelta[19] = -5
  sCity[20] = "Caracas"
  rDelta[20] = -4
  sCity[21] = "Rio de Janeiro"
  rDelta[21] = -3
  sCity[23] = "Azores"
  rDelta[23] = -1
  iCities = 24
  ' show city
  iCity = 9
  GraphicsWindow.BrushColor = sFGColor
  If rDelta[iCity] > 0 Then
    sSign = "+"
  Else
    sSign = ""
  EndIf
  GraphicsWindow.DrawText(iAX * 3 + iTX0, iAY * 7, sCity[iCity] + " (" + sSign + rDelta[iCity] + ")")
  ' add north hemesphere as shape
  uNorth = uFolder + "North.png"
  oNorth = Shapes.AddImage(uNorth) ' north hemesphere
  iR = 100  ' radius for the Earth
  iXC = 50 + iR ' center of the Earth
  iYC = 40 + iR
  Shapes.Move(oNorth, iXC - iR, iYC - iR)
  ' add shade as shape
  iMonth = Clock.Month
  uShadow = uFolder + "Shadow" + iMonth + ".png"
  oShadow = Shapes.AddImage(uShadow) ' shadow
  Shapes.SetOpacity(oShadow, 60)
  Shapes.Move(oShadow, iXC - iR, iYC - iR)
EndSub

' Earth | Rotate
Sub Earth_Rotate
  rDegree = (((iHour - rLocal) * 60 + iMin) * 60 + iSec) * 360 / iDaySec
  Shapes.Rotate(oNorth, -1 * rDegree)
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.