Overview -h.schools.nsw.edu.au



Task 2 - Part A - Notes on converting time formats

The deliverable for the first part of this task is a function called 'convertWmiDateTime', which converts Windows date/time strings into the required format for printing.

Pseudocode

The pseudocode for the convertWmiDateTime function is:

function convertWmiDateTime (wmiDateTime):

get yr from wmiDateTime

get mon from wmiDateTime

get day from wmiDateTime

get hr from wmiDateTime

get min from wmiDateTime

get sec from wmiDateTime

pyDateTime =

outputString =

return outputString

Extract the parts of the Windows date/time string

You will notice that most of the information we require is there in the Windows date/time format. Your first step will be to extract the year, month, day, hours, minutes and seconds into separate variables.

Python gives us several ways to do this, but perhaps the most straightforward is to use array slices, which will give us a subset of an array. The example 'slices.py' (available under "Your tools for this task ") shows how to extract the parts of a string.

Write a function that accepts a string in the Windows date/time format, and extracts these various parts into separate variables, each containing an integer. Print each variable.

Reformat the time in the required format

Before we format our date and time in the required format, we still need to determine the month as a string (like 'May') rather than as an integer (like 5). This would be easy to do. But we also need to determine the day of the week (such as 'Mon'), and this is quite a bit more complicated.

Fortunately, the Python library provides us with the datetime module for doing calculations with dates and times. The 'datetime.py' example shows how to create a new Python datetime object, and hints at how to format times in any way you desire.

So now, extend your previous program to make a Python datetime object representing the input time, format a new string in the desired format, and return it from your function.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download