Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6196305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:37:11+00:00 2026-05-24T03:37:11+00:00

I made a little parking application in C#. There are some different pricings depending

  • 0

I made a little parking application in C#. There are some different pricings depending on vehicle type and time zone. Day can be divided into time zones (for example morning, day, evening and night). Now if customer stops parking I want to calculate in which time zones customer has parked and how long.

For example morning time zone starts at 6:00 and ends 12:00, day time zone starts at 12:00 and ends 16:00, evening time zone starts at 16:00 and ends at 23:00 and night time zone starts at 23:00 and ends at 6:00. Customer started parking his car at 00:30 and ends parking at 6:32. Currently I have 4 variables for that: parking start time, parking end time and timezone starting time and timezone ending time.

Second example would be like customer parks 24H, then the parking time has all time zones covered.

How is the simplest way to calculate how many hours and minutes customer parked his car in different time zones?

Regards,
evilone

EDIT:

Got this answer from MSDN and post it here so others can learn from it too.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DateTime start = new DateTime(2011, 7, 25, 0, 30, 0);
            DateTime end = new DateTime(2011, 7, 26, 6, 32, 0);
            List<DateTime> listTimeZones = CalculateTotalTime(start, end);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < listTimeZones.Count; i++)
            {
                sb.AppendLine(String.Format("{0}. {1}: from {2} to {3}",
                                            i + 1,
                                            GetZoneInWords(listTimeZones[i].Hour),
                                            String.Format("{0:dd.MM.yyyy hh:mm}", listTimeZones[i]),
                                            (i + 1) < listTimeZones.Count
                                                ? String.Format("{0:dd.MM.yyyy hh:mm}", listTimeZones[i + 1])
                                                : "Parking ended"));
            }
            MessageBox.Show(sb.ToString());
        }

        private List<DateTime> CalculateTotalTime(DateTime start, DateTime end)
        {
            DateTime temp = start;

            int hour = start.Hour;
            int minute = start.Minute;

            int morning = 6;
            int day = 12;
            int evening = 17;
            int night = 23;

            List<DateTime> timeZones = new List<DateTime>();

            do
            {
                temp = temp.AddHours(1);
                if (temp.Hour == morning || temp.Hour == day ||
                    temp.Hour == evening || temp.Hour == night)
                {
                    timeZones.Add(temp);
                }
            } while (temp < end);

            return timeZones;
        }

        private string GetZoneInWords(int time)
        {
            string timeOfDay = "";
            if (time.Equals(6))
                timeOfDay = "Morning";
            else if (time.Equals(12))
                timeOfDay = "Day";
            else if (time.Equals(17))
                timeOfDay = "Evening";
            else if (time.Equals(23))
                timeOfDay = "Night";

            return timeOfDay + " parking";
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:37:12+00:00Added an answer on May 24, 2026 at 3:37 am

    Iterate through all the “time zones” and for each, work out the overlap between that and the customer’s parking. For example, as pseudo-code:

    private static TimeSpan FindOverlap(ParkingTime parkingTime, TimeZone timeZone)
    {
        // Handle wraparound zones like 23-6. Note that this doesn't attempt
        // to handle *parking* which starts at 11.30pm etc.
        if (timeZone.Start > timeZone.End)
        {
            return FindOverlap(parkingTime,
                         new TimeZone(timeZone.Start.Date, timeZone.End)
                 + FindOverlap(parkingTime,
                         new TimeZone(timeZone.End, timeZone.Start.Date.AddDays(1));
        }
    
        DateTime overlapStart = Max(parkingTime.Start, timeZone.Start);
        DateTime overlapEnd = Min(parkingTime.End, timeZone.End);
        TimeSpan overlap = overlapEnd - overlapStart;
    
        // If the customer arrived after the end or left before the start,
        // the overlap will be negative at this point.
        return overlap < TimeSpan.Zero ? TimeSpan.Zero : overlap;
    }
    
    private static DateTime Min(DateTime x, DateTime y)
    {
        return x < y ? x : y;
    }
    
    private static DateTime Max(DateTime x, DateTime y)
    {
        return x > y ? x : y;
    }
    

    By the way, I would strongly encourage you to rename your “time zone” concept, given that it already has a well-known (if not well-understood 🙂 meaning.

    Perhaps you should call it ParkingInterval? Or ParkingPriceInterval if the difference is really in terms of cost?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So... I have some little flash graphs I made that pull data from an
Many times I only have made a little change for one page. How can
I made this little function: public String getDay() { String day = (String)android.text.format.DateFormat.format(E, new
I made a little Windows Forms program to do some auto backup of some
I've made a little game as an application for the web in silverlight using
I am using Data::Dumper::Dumper() method. The output is good, but can be made little
I made a little example of my problem here: http://peterbriers.be/test/float_html5.html As you can see,
I made a little Java application which writes stuff to an Access database. When
I recently made a little application to read in a text file of lyrics,
I have an issue with CSS I can't solve. I've made a little diagram

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.