Getting current time from wolfram.com using python

Hello to all,

I am looking of a python script that can fetch date & time from wolfram or any website that gives correct time.

  1. Open woflram.com website
  2. Search query "time"
  3. Search result displays the time.

The script has to contact the website with the search query, take that information and display it in DDMMYY HHMM format.

I already have a pyhton + windows batch script that take time from a website whose output is in simple HTML that can be processed easily. But the time format from that website dont have 'year' section. This is for my laptop that dont have battery. so i have to manually maintain time or automate it by fetching time from internet, which is easy.

Kindly help or give me a start (refer below).

I have read about HTML parsers in python. How can i get time from following HTML piece of code. i.e. "4:37:22 pm MYT | Wednesday, January 6, 2010"

<div id="pod_0200" class="pod "><hr class="top" /><h1><span>Result:</span></h1><div id="subpod_0200_1" class="sub"><div class="output" id="scannerresult_0200_1"><img src="http://www4c.wolframalpha.com/Calculate/MSP/MSP1431990e8ecc56h866300002h0f516223185h8h?MSPStoreType=image/gif&s=46" id="i_0200_1" alt="4:37:22 pm MYT  |  Wednesday, January 6, 2010" title="4:37:22 pm MYT  |  Wednesday, January 6, 2010..."   /></div><div class="annotpod"> 
	    </div>

Ok !, I did it myself. Here is the code.

Python (Modified from actual source: java2s.com)

# mytime2  - SYNC TIME WITH WOLFRAM ALPHA VERSION 2

import sys
import HTMLParser
import urllib

mode = sys.argv
url = "http://www.wolframalpha.com/input/?i=time"
urlText = []
TZ = "MYT"

class parseText(HTMLParser.HTMLParser):
    def handle_data(self, data):
        if data != '\n':
            urlText.append(data)


lParser = parseText()
lParser.feed(urllib.urlopen(url).read())
lParser.close()

htm_str_1 = []

for htm_str in urlText:
	htm_str = htm_str.split();	
	wrdLoc = 1
	for htm_str_2 in htm_str:		
		if htm_str_2 == TZ:
			
			# TIME
			time = htm_str[37]
			timeChk = htm_str[37].find(":")
			if timeChk != -1:
				time = time.split('"')
				time = time[1]+" "+htm_str[38].upper()
			
			#DATE
			day = htm_str[43].split(',')

			month = htm_str[42].lower()
			
			if month == "january":
				month = "01"		
			if month == "february":
				month = "02"
			if month == "march":
				month = "03"
			if month == "april":
				month = "04"
			if month == "may":
				month = "05"
			if month == "june":
				month = "06"
			if month == "july":
				month = "07"		
			if month == "august":
				month = "08"
			if month == "september":
				month = "09"
			if month == "october":
				month = "10"
			if month == "november":
				month = "11"
			if month == "december":
				month = "12"

			year = htm_str[44].split('"')			
			date = month+"/"+day[0]+"/"+year[0]

			break
		wrdLoc = wrdLoc + 1

if mode[1] == '1':
	if date != "":
		print date
	else:
		print ""

if mode[1] == '2':
	if time != "":
		print time
	else:
		print ""

Batch Script (Windows)

@echo off
REM set period=%1
mytime.py 2 > TIM.TEMP
set /p var= < TIM.TEMP
REM set mtime=%var% %period%
set mtime=%var%
echo %mtime%
time %mtime%
mytime.py 1 > DAT.TEMP
set /p var= < DAT.TEMP
set mdate=%var%
echo %mdate%
date %mdate%
echo BASIL B.C. 2009

Internet connection is necessary. Change the value for 'TZ' timezone variable accordingly. Place both of them in one folder say 'bin'. Add 'bin' path to environment variables and execute 'mytime' from command line or its shortcut. If wolframalpha changes the HTML source code style, this script wont work.

I use this script to set time & date in my laptop that has no battery.

Hi, is there a reason for not using ntp for that purpose?

Is there any ntp command for windows ?. The built in internet time sync in windows wont work if the system time difference is huge (like years) from actual time. My laptop is manufactured in 2004 so the time defaults to July 2004 if the bios battery runs out of charge.

Usually i dont care what time my laptop keeps, but the websites that requires user accounts wont login properly if the system time is wrong, i didnt know that websites really cares about my system time. Is there anyway to bypass this ?

OK , I did not realize you were hampered by the use of windows ;). I am sure there are ntp clients for windows. I would venture a google.
If the time difference is huge than ntp will not sync either. The trick is to issue a hard ntpdate at boot time and then let ntp do the timekeeping afterwards.

Meinberg NTP Software Downloads