Getting current time from wolfram.com using python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting current time from wolfram.com using python
# 1  
Old 01-06-2010
Question 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"

Code:
<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>

# 2  
Old 01-10-2010
Bug Getting time from internet and updating the windows time using python

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

Python (Modified from actual source: java2s.com)

Code:
# 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)

Code:
@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.
# 3  
Old 01-10-2010
Hi, is there a reason for not using ntp for that purpose?
# 4  
Old 01-11-2010
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 ?
# 5  
Old 01-11-2010
OK , I did not realize you were hampered by the use of windows Smilie. 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.

Last edited by Scrutinizer; 01-11-2010 at 04:31 AM..
# 6  
Old 01-11-2010
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Arduino UNIX Time - Syncing Computer UNIX Time to Arduino Time with Python

Just finished a quick Python script to send the current unix time over to the Arduino from macOS, so in the absence of GPS or some other way to get the unix timestamp (epoch time) to the Arduino, I can get my macOS and Arduino UNO synced to within a second. Normally, when the Arduino starts... (9 Replies)
Discussion started by: Neo
9 Replies

2. Shell Programming and Scripting

Convert UTC time into current UNIX sever time zone

Hi guys thanks for the help for my previous posts.Now i have a requirement that i download a XMl file which has UTC time stamp.I need to convert UTC time into Unix server timezone. For ex if the time zone of unix server is CDT then i need to convert into CDT.whatever may be the system time... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

3. Shell Programming and Scripting

To get the time exactly 24hrs from the current time

Hi guys, I am having file which contains below data. 2012-04-24 08:40:13 10739022730 1027699274PersonInfoShipTO details missing FirstName,LastName, 2012-04-24 08:40:13 10739022730 1027699274PersonInfoShipTO details missing FirstName,LastName, 2012-04-24 08:40:13 ... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

4. Shell Programming and Scripting

Add current date and time

i have file 1.txt asdas|csada|13|03|10|04|23|A1|canberra sdasd|sfdsf|13|04|26|23|28|A1|sydney i want to add today's date and time in the end of each row expected output asdas|csada|13|03|10|04|23|A1|canberra|130430|1358 sdasd|sfdsf|13|04|26|23|28|A1|sydney|130430|1358 todays date... (10 Replies)
Discussion started by: radius
10 Replies

5. Shell Programming and Scripting

Displaying current date time of EDT in IST time

Hi Folks, My server time is in EDT. And i am sending automated mails from that server in which i need to display the current date time as per IST (GMT+5:30). Please advice how to display the date time as per IST. IST time leads 9:30 mins to EDT. and i wrote something like below. ... (6 Replies)
Discussion started by: Showdown
6 Replies

6. Shell Programming and Scripting

How to display current time in the prompt

Hello All, I would like to display the current time in prompt. I tried using following command: export PS1="$(date +%k:%M:%S) $ " but it gave me a fixed time in prompt whereas my objective is to get the current time everytime. $ export PS1="$(date +%k:%M:%S) $ " 17:42:42 $ 17:42:42 $... (32 Replies)
Discussion started by: manishdivs
32 Replies

7. Shell Programming and Scripting

How far is given date from current time?

give a date and time: Jun 12 21:05:16 06-12-2012 21:05:16 2012/06/12 21:05:16 How can i subtract these dates and times from the current date and time and get back the difference in seconds? a one liner like: echo "Jun 12 21:05:16" | some perl/awk programming 90900s (2 Replies)
Discussion started by: SkySmart
2 Replies

8. Shell Programming and Scripting

Need help for AWK output with current time

Hi, Any suggestion to get the AWK output with time in each line ? I want to have the vmstat in solaris with time append to everyline like this: kthr memory page disk faults cpu r b w swap free re mf pi po fr de sr s0 s1 s3 sd in sy cs... (2 Replies)
Discussion started by: roxy2k
2 Replies
Login or Register to Ask a Question