detect if date/time is daylight savings time

Posted on 2004-03-31
Medium Priority
Last Modified: 2013-11-20

Given the following datetime string "2004-03-30 13:09:28" I need to detect if it is PST or PDT?

Dalight Savings time:
Starts: First Sunday in April at 2 am local time
Ends: Last Sunday in October at 2 am local time

What I need to do is append PST or PDT accordingly.

Question by:astankovic
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 14

Accepted Solution

wayside earned 2000 total points
ID: 10727902
int startday = 1;
int endday = 31;

COleDateTime startDST(2004, 4, startday, 2, 0, 0);
COleDateTime endDST(2004, 10, endday, 2, 0, 0);

while (startDst.GetDayOfWeek() != 1) {
  startDST.SetDate(2004, 4, startday);

while (endDst.GetDayOfWeek() != 1) {
  endDST.SetDate(2004, 10, endday);

// now startDST and endDST contain the start and end dates of daylight savings time
COleDateTime testDate(2004, 6, 12, 12, 34, 56);

if (testDate >= startDST && testDate <= endDST) {
else {
LVL 14

Expert Comment

ID: 10727913
More generally, you would extract the year out of your test date and use that when building the dates for startDST and endDST.

Author Comment

ID: 10728216

Expert Comment

ID: 10731004

Those times you mention for DST are very specifically USA and Canada only - in fact it doesn't even really apply to all of the USA and Canada because Fort St. John, Charlie Lake, Taylor and Dawson Creek in British Columbia, Creston in the East Kootenays, and most of  Saskatchewan (except Denare Beach and Creighton) don't use daylight savings time at all.

Just to confuse things, the UK switches at one o'clock in the morning the last Sunday in March and two o'clock in the morning in October.  New Zealand did their switchover three weeks ago and Australia did their's the week after that.  Besides which, any region in the southern hemisphere that does daylight savings time does it at the opposite time of the year (Autumn here in New Zealand is in March/April)


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question