MutableTime (Class)

In: MutableTime.rb
Parent: Object

The MutableTime class behaves much like the builtin Time class, except:

  • almost any property can be changed, e.g.
     mmTime = MutableTime.new
     mmTime.year = 1973
     mmTime.month += 13
    
  • by default, month numbers start at 0 instead of 1 — see monthsStartAtZero?
  • you can choose if the first day of the week is Sunday or Monday — see weekStartsOnMonday?
  • it has convenience methods named similar to JavaScript’s Date object — fullYear, month, date, hours, minutes, seconds
  • customFormat is slightly more powerful than Time#strftime
  • you can easily internationalize the month and day names (without editing the source code for the class)

In addition to the methods explicitly listed here, MutableTime instances support all the methods of the Time class

Methods

+   -   <=>   am?   at   customFormat   date   date=   day=   dayNames   dayNames=   fullYear   gmtime   gmtime!   hours   hours=   local   localtime   localtime!   microseconds   microseconds=   milliseconds   milliseconds=   minutes   minutes=   mon   month   month=   monthNames   monthNames=   monthsStartAtZero=   monthsStartAtZero?   new   now   parse   pm?   seconds   seconds=   succ   utc   wday   weekDay   weekDays   weekStartsOnMonday=   weekStartsOnMonday?   year=   yearDay  

Constants

MONTH_NAMES = %w| January February March April May June July August September October November December |
DAY_NAMES = %w| Sunday Monday Tuesday Wednesday Thursday Friday Saturday |

Attributes

t  [RW]  Accesses the Time class instance associated with the instance. Setting this to an object other than a Time instance will break the class.

Included Modules

Comparable ParseDate

Public Class methods

Like Time.at, but does not accept a Time or MutableTime instance as the parameter.

   mmTime = MutableTime.at(98059507)
   mmTime.to_s  =>  'Thu Feb 08 15:45:07 MST 1973'

Returns the array of day names used for customFormat.

See dayNames= for the impact of changing this array.

Allows you to set the localization-friendly day names used by customFormat

   mmTime = MutableTime.new
   mmTime.customFormat('#DDDD#, #MMMM# #D##th#')  =>  'Friday, February 13th'
   MutableTime.monthNames =  %w| Janvier Fevrier Mars Avril Mai Juin Juillet
                                 Aout Septembre Octobre Novembre Decembre |
   MutableTime.dayNames   =  %w| Dimanche Lundi Mardi Mercredi Jeudi
                                 Vendredi Samedi |
   mmTime.customFormat('#DDDD#, #MMMM#-#D#')      =>  'Samedi, Fevrier-13'

The array you supply must match with weekStartsOnMonday?; if weekStartsOnMonday? is true, then your array must start with the name of Monday and end with Sunday.

Similar to Time.local, but when monthStartsAtZero? is true, the default month number is 0, and 1 will be added to the month before calling Time.local

   mmTime = MutableTime.local(2004,1,8,15,45)
   mmTime.to_s  =>  'Sun Feb 08 15:45:00 MST 2004'
   MutableTime.monthStartsAtZero=false
   mmTime = MutableTime.local(2004,1,8,15,45)
   mmTime.to_s  =>  'Thu Jan 08 15:45:00 MST 2004'

Returns the array of month names used for customFormat.

See monthNames= for the impact of changing this array.

Allows you to set the localization-friendly month names used by customFormat

   mmTime = MutableTime.new
   mmTime.customFormat('#DDDD#, #MMMM# #D##th#')  =>  'Friday, February 13th'
   MutableTime.monthNames =  %w| Janvier Fevrier Mars Avril Mai Juin Juillet
                                 Aout Septembre Octobre Novembre Decembre |
   MutableTime.dayNames   =  %w| Dimanche Lundi Mardi Mercredi Jeudi
                                 Vendredi Samedi |
   mmTime.customFormat('#DDDD#, #MMMM#-#D#')      =>  'Samedi, Fevrier-13'

Unlike Time, month numbers returned by mon or month start with January==0 (by default)

If you prefer January==1, call this method and pass in a non-truth value.

Note that month names and numbers produced by strftime or customFormat will not be affected.

   mmTime = MutableTime.now
   mmTime.customFormat('#MMMM# #D##th# -- #M#/#D#')  =>  'February 13th -- 2/13'
   mmTime.month                                      =>  1
   MutableTime.monthsStartAtZero = false
   mmTime.month                                      =>  2
   mmTime.customFormat('#MMMM# #D##th# -- #M#/#D#')  =>  'February 13th -- 2/13'

Returns the current state of the class — see monthsStartAtZero=

Can be called in one of five ways:

Like Time.now, returns a MutableTime instance representing the current date/time

   mmTime = MutableTime.now
   mmTime.to_s  =>  'Fri Feb 13 14:51:05 MST 2004'

Attempts to create a new MutableTime instance, using ParseDate.parsedate on the supplied string

    mmTime = MutableTime.parse("2/8/1973 3:45pm")
    mmTime.to_s  =>  'Thu Feb 08 15:45:00 MST 1973'

Similar to Time.utc, but when monthsStartAtZero? is true, the default month number is 0, and 1 will be added to the month before calling Time.local

   mmTime = MutableTime.utc(2004,1,8,15,45)
   mmTime.to_s  =>  'Sun Feb 08 15:45:00 UTC 2004'
   MutableTime.monthStartsAtZero=false
   mmTime = MutableTime.utc(2004,1,8,15,45)
   mmTime.to_s  =>  'Thu Jan 08 15:45:00 UTC 2004'

By default, weekDay numbers are the same as in Time: 0..6, with 0 being Sunday.

The MutableTime class can also use 0==Monday; if you prefer this, pass in a truth value to this method to achieve this goal.

Note that day names produced by strftime or customFormat will not be affected.

   mmTime = MutableTime.now
   mmTime.customFormat('#M#/#D# #DDDD#')  =>  '2/13 Friday'
   mmTime.weekDay                         =>  5
   MutableTime.weekStartsOnMonday = true
   mmTime.weekDay                         =>  4
   mmTime.customFormat('#M#/#D# #DDDD#')  =>  '2/13 Friday'

Returns the current state of the class — see weekStartsOnMonday=

Public Instance methods

Returns a new MutableTime instance which is n days in the future (and with the time unaffected).

   mmTime = MutableTime.new
   oneWeekFromToday = mmTime+7
   mmTime             =>  'Fri Feb 13 15:39:30 MST 2004'
   oneWeekFromToday   =>  'Fri Feb 20 15:39:30 MST 2004'

Days may wrap beyond month and year boundaries without problem, and may be fractional.

   mmTime+21          =>  'Fri Mar 05 15:39:30 MST 2004'
   mmTime+1.5         =>  'Sun Feb 15 03:39:30 MST 2004'

If n is an integer, returns a new MutableTime instance which is n days in the past (and with the time unaffected).

   mmTime = MutableTime.new
   oneWeekAgo = mmTime-7
   mmTime        =>  'Fri Feb 13 15:39:30 MST 2004'
   oneWeekAgo    =>  'Fri Feb 06 15:39:30 MST 2004'

Days may wrap beyond month and year boundaries without problem, and may be fractional.

   mmTime-87     =>  'Tue Nov 18 15:39:30 MST 2003'
   mmTime-1.5    =>  'Thu Feb 12 03:39:30 MST 2004'

If n is a MutableTime instance, returns the number of seconds (as a Float) between the two

Same as Time#<=>

Returns true if the hours are in 0..11 (12am is hours==0)

Replaces every token in formatString with its corresponding value. See the table below.

   token:     description:             example:
   #YYYY#     4-digit year             2004
   #YY#       2-digit year             04
   #MMMM#     full month name          February
   #MMM#      3-letter month name      Feb
   #MM#       2-digit month number     02
   #M#        month number             2
   #DDDD#     full weekday name        Wednesday
   #DDD#      3-letter weekday name    Wed
   #DD#       2-digit day number       09
   #D#        day number               9
   #th#       day ordinal suffix       nd
   #hhh#      military/24-based hour   17
   #hh#       2-digit hour             05
   #h#        hour                     5
   #mm#       2-digit minute           07
   #m#        minute                   7
   #ss#       2-digit second           09
   #s#        second                   9
   #ampm#     "am" or "pm"             pm
   #AMPM#     "AM" or "PM"             PM

Non-tokens are left untouched in the output string

   mmTime = MutableTime.new('2/3/2004 15:37')
   mmTime.customFormat('#DDDD#, #MMMM# #D##th# @ #h#:#mm##ampm#')
     =>  'Tuesday, February 3rd @ 3:37pm'
   mmTime.customFormat('#YYYY#-#MMM#-#D#')
     =>  '2004-Feb-3'
   mmTime.customFormat('#MM#/#DD#/#YY#')
     =>  '02/03/04'

See monthNames= and dayNames= to localize the month and day strings. Note that monthsStartAtZero= and weekStartsOnMonday= have no affect on the output of customFormat=

Returns the day number (1..31) for this date.

Sets the day number to the supplied value. Works with += and -= expansion to allow incrementing/decrementing. Supports day numbers outside the ‘valid’ range, automatically wrapping to the next/previous month as needed.

   mmTime = MutableTime.new   =>  'Fri Feb 13 17:24:52 MST 2004'
   mmTime.date=22
   puts mmTime                =>  'Sun Feb 22 17:24:52 MST 2004'
   mmTime.date=30
   puts mmTime                =>  'Mon Mar 01 17:24:52 MST 2004'
   mmTime.date-=15
   puts mmTime                =>  'Sun Feb 15 17:24:52 MST 2004'

Synonym for date=

Returns the year number (with leading century).

Synonym for gmtime! (since Time supports such a method).

Modifies the instance to reflect the equivalent time as specied in UTC/GMT.

   mmTime = MutableTime.new
   puts mmTime        =>  'Fri Feb 13 17:32:51 MST 2004'
   mmTime.gmtime!
   puts mmTime        =>  'Sat Feb 14 00:32:51 UTC 2004'

Returns the hour number (0..23) for the time.

Sets the hour number to the supplied value, using 24-hour values (0..23). Works with += and -= expansion to allow incrementing/decrementing. Supports hour numbers outside the ‘valid’ range, automatically wrapping to the next/previous day as needed.

Synonym for localtime! (since Time supports such a method).

Modifies the instance to reflect the equivalent time as specied in the local timezone.

   mmTime = MutableTime.new
   mmTime.gmtime!
   puts mmTime        =>  'Fri Feb 13 17:32:51 MST 2004'
   mmTime.localtime!
   puts mmTime        =>  'Sat Feb 14 00:32:51 UTC 2004'

Returns just the microseconds for the time.

Sets the microsecond number to the supplied value. Works with += and -= expansion to allow incrementing/decrementing. Supports millisecond numbers outside the ‘valid’ range, automatically wrapping to the next/previous second as needed.

Returns just the milliseconds for the time.

Sets the millisecond number to the supplied value. Works with += and -= expansion to allow incrementing/decrementing. Supports millisecond numbers outside the ‘valid’ range, automatically wrapping to the next/previous second as needed.

Returns the minute number (0..59) for the time.

Sets the minute number to the supplied value. Works with += and -= expansion to allow incrementing/decrementing. Supports minute numbers outside the ‘valid’ range, automatically wrapping to the next/previous hour as needed.

Synonym for month. (0..11 or 1..12 — see monthsStartAtZero?)

Returns the month number. (0..11 or 1..12 — see monthsStartAtZero?)

Sets the month number to the supplied value. Works with += and -= expansion to allow incrementing/decrementing. Supports months outside the ‘valid’ range, automatically wrapping to the next/previous year as needed.

This method is sensitive to the setting in monthsStartAtZero=; if true (the default), mmTime.month=1 represents February; if false, it represents January

   mmTime = MutableTime.new    => 'Fri Feb 13 16:24:58 MST 2004'
   mmTime.month=7
   puts mmTime                 => 'Fri Aug 13 16:24:58 MST 2004'
   mmTime.month+=13
   puts mmTime                 => 'Tue Sep 13 16:24:58 MST 2005'

Since setting an invalid month/day combination wraps to the valid equivalent date, repeatedly incrementing the month may have unintended results for date numbers greater than 28.

   def six_months_of_days(d)
     6.times{
             print d.customFormat('#MMM#-#D#-#YYYY#'),' : '
             d.month+=1
     }
     puts
   end

   six_months_of_days( MutableTime.new('1/1/2004') )
   => 'Jan-1 : Feb-1 : Mar-1 : Apr-1 : May-1 : Jun-1 : '

   six_months_of_days( MutableTime.new('1/31/2004') )
   => 'Jan-31 : Mar-2 : Apr-2 : May-2 : Jun-2 : Jul-2 : '

Returns true if the hours are in 12..23.

Returns the second number (0..60) for the time.

Sets the second number to the supplied value. Works with += and -= expansion to allow incrementing/decrementing. Supports second numbers outside the ‘valid’ range, automatically wrapping to the next/previous minute as needed.

Returns a new MutableTime instance that is one day in the future

Synonym for weekDay

Return the weekday number (0..6) of the date. See weekStartsOnMonday= for more information

Returns a 7-element array containing all the days which occur within the same week as this day

If showDaysInOtherMonths is false, nil elements will be used in place of MutableTime instances for those days which are in a month other than the receiver of this method

   mmTime = MutableTime.new('3/2/2000')
   puts mmTime.weekDays
   => Sun Feb 27 00:00:00 MST 2000
   => Mon Feb 28 00:00:00 MST 2000
   => Tue Feb 29 00:00:00 MST 2000
   => Wed Mar 01 00:00:00 MST 2000
   => Thu Mar 02 00:00:00 MST 2000
   => Fri Mar 03 00:00:00 MST 2000
   => Sat Mar 04 00:00:00 MST 2000

   puts mmTime.weekDays(false)
   => nil
   => nil
   => nil
   => Wed Mar 01 00:00:00 MST 2000
   => Thu Mar 02 00:00:00 MST 2000
   => Fri Mar 03 00:00:00 MST 2000
   => Sat Mar 04 00:00:00 MST 2000

Sets the year number to the supplied value. Also works with += and -= expansion to allow incrementing/decrementing.

   mmTime = MutableTime.new    => 'Fri Feb 13 16:24:58 MST 2004'
   mmTime.year=2003
   puts mmTime                 => 'Thu Feb 13 16:24:58 MST 2003'
   mmTime.year+=10
   puts mmTime                 => 'Wed Feb 13 16:24:58 MST 2013'

Returns the day number in the year, 1..366 (Synonym for Time#yday)

[Validate]