Results 1 to 1 of 1

Thread: Ontleed seconden naar jaren/dagen/etc...

  1. #1
    Senior Member PsychoMark's Avatar
    Join Date
    Nov 2001
    Location
    Raamsdonksveer
    Posts
    10,269

    Ontleed seconden naar jaren/dagen/etc...

    Omschrijving

    Deze functie accepteerd als input het aantal seconden, en haalt hier het aantal jaren/dagen/uren/minuten/seconden uit. Er zijn eigenlijk twee functies: de ene ontleedt het aantal seconden (SecondsToTime), de andere maakt er een string van voor de luie mensen zoals ik (SecondsToString)



    Opmerkingen

    De code houdt geen rekening met schrikkeljaren maar gaat gewoon uit van 365 dagen per jaar. Over het algemeen is dat geen probleem...

    Update: met dank aan SVG's suggestie ondersteund de functie nu een format parameter waardoor je zelf het formaat aan kunt passen. Een voorbeeld staat hieronder, en in de source staat boven de functie de uitleg van de mogelijke format-strings...



    Voorbeeld

    Code:
    
    ShowMessage(SecondsToString(309487));
    
    ShowMessage(SecondsToString(234, '%d dagen, %h uren, ' +
                                     '%m minuten en %s seconden')));
    


    Code

    Code:
    uses
      SysUtils;
    
    const
      // Modify these to change the language
    CYears  = 'year(s)';
      CDays   = 'day(s)';
    
      // Just in case some Delphi version misses these constants
    HoursPerDay   = 24;
      MinsPerDay    = HoursPerDay * 60;
      SecsPerDay    = MinsPerDay * 60;
      SecsPerMin    = 60;
      SecsPerHour   = SecsPerMin * 60;
    
    
    // Extract the number of days/hours/etc from the input in seconds
    procedure SecondsToTime(const AInput: Integer; var ASeconds, AMinutes, AHours,
                           ADays, AYears: Integer);
      function SubtractSeconds(var ASeconds: Integer; ASize: Integer): Integer;
      begin
        // How many times does ASize fit in ASeconds?
    Result  := ASeconds div ASize;
        Dec(ASeconds, Result * ASize);
      end;
    
    var
      iSeconds:     Integer;
    
    begin
      iSeconds  := AInput;
      AYears    := SubtractSeconds(iSeconds, SecsPerDay * 365);
      ADays     := SubtractSeconds(iSeconds, SecsPerDay);
      AHours    := SubtractSeconds(iSeconds, SecsPerHour);
      AMinutes  := SubtractSeconds(iSeconds, SecsPerMin);
      ASeconds  := iSeconds;
    end;
    
    // Uses SecondsToTime to make a nice ready-for-display string
    //
    //    Format:
    //      %y      Years
    //      %d      Days
    //      %h      Hours     (%hh = force 2 digits, ex: 04)
    //      %m      Minutes   (%mm = force 2 digits)
    //      %s      Seconds   (%ss = force 2 digits)
    function SecondsToString(const ASeconds: Integer;
                             const AFormat: String = ''): String;
    const
      CReplaceFlags = [rfReplaceAll, rfIgnoreCase];
    
    var
      iYears:     Integer;
      iDays:      Integer;
      iHours:     Integer;
      iMinutes:   Integer;
      iSeconds:   Integer;
    
    begin
      Result  := '';
      SecondsToTime(ASeconds, iSeconds, iMinutes, iHours, iDays, iYears);
    
      if Length(AFormat) = 0 then
        // No format specified, build our own format
    if iYears > 0 then
          Result  := Format('%d %s, %d %s, %.2d:%.2d:%.2d', [iYears, CYears,
                                                          iDays, CDays,
                                                          iHours, iMinutes,
                                                          iSeconds])
        else if iDays > 0 then
          Result  := Format('%d %s, %.2d:%.2d:%.2d', [iDays, CDays,
                                                   iHours, iMinutes,
                                                   iSeconds])
        else
          Result  := Format('%.2d:%.2d:%.2d', [iHours, iMinutes, iSeconds])
      else begin
        // Use the specified format explicitly
    Result  := AFormat;
        Result  := StringReplace(Result, '%y', IntToStr(iYears), CReplaceFlags);
        Result  := StringReplace(Result, '%d', IntToStr(iDays), CReplaceFlags);
        Result  := StringReplace(Result, '%hh', Format('%.2d', [iHours]), CReplaceFlags);
        Result  := StringReplace(Result, '%h', IntToStr(iHours), CReplaceFlags);
        Result  := StringReplace(Result, '%mm', Format('%.2d', [iMinutes]), CReplaceFlags);
        Result  := StringReplace(Result, '%m', IntToStr(iMinutes), CReplaceFlags);
        Result  := StringReplace(Result, '%ss', Format('%.2d', [iSeconds]), CReplaceFlags);
        Result  := StringReplace(Result, '%s', IntToStr(iSeconds), CReplaceFlags);
      end;
    end;
    Last edited by PsychoMark; 29-Sep-02 at 12:56.
    Qui custodiet ipsos custodes

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Van pag1, naar pag2, naar pag3, naar pag1
    By Matrix in forum WebDelphi
    Replies: 10
    Last Post: 30-Nov-04, 11:30
  2. Replies: 2
    Last Post: 15-Jul-04, 10:18
  3. data in ODBC-database overzetten naar Access
    By sliderrr in forum Algemeen
    Replies: 19
    Last Post: 20-May-03, 16:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •