view src/dil/Time.d @ 821:09a64d96967a

Started the interpreter. Made a couple changes in other parts -- CharExpression now has an IntExpression of a character type as its value member and its type is set accordingly.
author Jarrett Billingsley <jarrett.billingsley@gmail.com>
date Fri, 14 Mar 2008 11:01:05 -0400
parents bcb74c9b895c
children
line wrap: on
line source

/++
  Author: Aziz Köksal
  License: GPL3
+/
module dil.Time;

import tango.stdc.time : time_t, time, ctime;
import tango.stdc.string : strlen;

/// Some convenience functions for dealing with C's time functions.
struct Time
{
static:
  /// Returns the current date as a string.
  char[] toString()
  {
    time_t time_val;
    .time(&time_val);
    char* str = ctime(&time_val); // ctime returns a pointer to a static array.
    char[] timeStr = str[0 .. strlen(str)-1]; // -1 removes trailing '\n'.
    return timeStr.dup;
  }

  /// Returns the time of timeStr: hh:mm:ss
  char[] time(char[] timeStr)
  {
    return timeStr[11..19];
  }

  /// Returns the month and day of timeStr: Mmm dd
  char[] month_day(char[] timeStr)
  {
    return timeStr[4..10];
  }

  /// Returns the year of timeStr: yyyy
  char[] year(char[] timeStr)
  {
    return timeStr[20..24];
  }
}