How to resolve the algorithm Date manipulation step by step in the Smalltalk programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Date manipulation step by step in the Smalltalk programming language

Table of Contents

Problem Statement

Given the date string "March 7 2009 7:30pm EST", output the time 12 hours later in any human-readable format. As extra credit, display the resulting time in a time zone different from your own.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Date manipulation step by step in the Smalltalk programming language

Source code in the smalltalk programming language

DateTime extend [
  setYear: aNum [ year := aNum ]
].

Object subclass: DateTimeTZ [
  |dateAndTime timeZoneDST timeZoneName timeZoneVar|

  DateTimeTZ class >> new [ ^(super basicNew) ]

  DateTimeTZ class >> readFromWithMeridian: aStream andTimeZone: aDict [
    |me|
    me := self new.
    ^ me initWithMeridian: aStream andTimeZone: aDict
  ]

  initWithMeridian: aStream andTimeZone: aDict [ |s|
    dateAndTime := DateTime readFrom: aStream copy.
    s := aStream collection asString.
    s =~ '[pP][mM]'
      ifMatched: [ :m |
        dateAndTime := dateAndTime + (Duration days: 0 hours: 12 minutes: 0 seconds: 0)
      ].
    aDict keysAndValuesDo: [ :k :v |
      s =~ k
        ifMatched: [ :x |
          dateAndTime := dateAndTime setOffset: (v at: 1).
	  timeZoneDST := (v at: 2) setOffset: (v at: 1).
	  timeZoneVar := (v at: 3).
	  timeZoneDST setYear: (self year). "ignore the year"
          timeZoneName := k
        ]
    ].
    ^ self
  ]

  setYear: aNum [ dateAndTime setYear: aNum ]
  year [ ^ dateAndTime year ]

  timeZoneName [ ^timeZoneName ]

  + aDuration [ |n|
    n := dateAndTime + aDuration.
    (n > timeZoneDST) ifTrue: [ n := n + timeZoneVar ].
    ^ (self copy dateTime: n)
  ]

  dateTime [ ^dateAndTime ]
  dateTime: aDT [ dateAndTime := aDT ]

].


|s abbrDict dt|

s := 'March 7 2009 7:30pm EST'.

"Build a abbreviation -> offset for timezones (example)"
abbrDict := Dictionary new.

abbrDict at: 'EST' 
         put: { (Duration days: 0 hours: -5 minutes: 0 seconds: 0).
                (DateTime year: 2009 month: 3 day: 8 hour: 2 minute: 0 second: 0).
		(Duration days: 0 hours: 1 minutes: 0 seconds: 0) }.

dt := DateTimeTZ readFromWithMeridian: (s readStream) andTimeZone: abbrDict.

dt := dt + (Duration days: 0 hours: 12 minutes: 0 seconds: 0).

"let's print it"
('%1 %2 %3 %4:%5%6 %7' %
{
  (dt dateTime) monthName asString.
  (dt dateTime) day.
  (dt dateTime) year.
  (dt dateTime) hour12.
  (dt dateTime) minute.
  (dt dateTime) meridianAbbreviation asString.
  dt timeZoneName.
}) displayNl.

(dt dateTime) asUTC displayNl.


  

You may also check:How to resolve the algorithm Continued fraction step by step in the F# programming language
You may also check:How to resolve the algorithm Remove lines from a file step by step in the F# programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Chapel programming language
You may also check:How to resolve the algorithm Compiler/lexical analyzer step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the Rust programming language