The ** localtime** Perl function converts a time using a nine-part array of different parts of the current local server time.

## Defining localtime

The 9 parts of `localtime`

are defined as follows:

**0** - seconds (seconds of minutes from 0 to 61)*

Although typically seconds are 0 to 59, this will go to 61 to account for "leap seconds" in some systems.

**1** - minutes (minutes of hour from 0 to 59)

**2** - hours (hours of day from 0 to 24)

**3** - day of the month (day of month from 1 to 31)

**4** - month (number of months __since January__, from 0 to 11)

**5** - year (number of years __since 1900__)

**6** - day of the week (number of days __since Sunday__, from 0 to 6)

**7** - day of the year (number of days __since January 1st__, from 0 to 365)

The range has 366 days total to account for leap years.

**8** - daylight savings time (hours of daylight savings time)

This returns a Boolean value, 1 = daylight savings time in effect, 0 = daylight savings time __not__ in effect

**Note:** The month and day of the week are 0-indexed (they start at 0, not 1). This means that January = 0, February = 1, etc. If January needs to be '1', then add '1' to the result.

The same is true for days of the week where Sunday = 0, Monday = 1, etc. To make Sunday equal '1', add '1' to the result.

## Using localtime

For the following examples, suppose the time captured on a local server was: **Fri Oct 30 09:23:32 2020**

For a demo on how to use localtime, see https://demo70.intellisurvey.com/dev/localtime_demo

The official Perl documentation on localtime can be found here.

### Current local server time

NOW. Now type: text invisible: y onload: y cvalue: my $now = localtime; return $now;

### Seconds

Using the above example, seconds = 32.

1. Seconds type: text invisible: y onload: y cvalue: ((localtime)[0])

### Minutes

Using the above example, minutes = 23.

2. Minutes type: text invisible: y onload: y cvalue: ((localtime)[1])

### Hour of the day

Using the above example, hour = 9.

3. Hour type: text invisible: y onload: y cvalue: ((localtime)[2])

### Day of the month

Using the above example, the day of the month = 30.

4. Day of the month type: text invisible: y onload: y cvalue: ((localtime)[3])

### Month

The month may be calculated either as being assigned a 0-11 value (default), or a more "typical" 1-12 value. The month can also be calculated from the month's name, either via open or closed-end responses.

#### Default month calculation

Using the above example, since __months are 0-indexed__, month = 9.

5. Month type: text invisible: y onload: y cvalue: ((localtime)[4])

#### Month + 1

To use 1-12 instead of 0-11 for the month values, add '1' to the month's value. Using the above example, month = 10.

5A. Month + 1 type: text invisible: y onload: y cvalue: ((localtime)[4] + 1)

#### Month name (open)

5B. Month - name (open) type: text invisible: y onload: y cvalue:<<END my @months = qw(January February March April May June July August September October November December); my $month = ((localtime)[4]); return $months[$month]; END

#### Month name (closed)

The following example adds '1' to the returned value for `localtime`

.

5C. Month - name (closed) type: radio invisible: y onload: y cvalue: ((localtime)[4] + 1) 1. January 2. February 3. March 4. April 5. May 6. June 7. July 8. August 9. September 10. October 11. November 12. December

### Year

The localtime function is designed to return the __number of years since 1900__, but can also be used to return the current year by adding '1900' to the response.

#### Years since 1900

Using the above example, year = 120.

6. Years since 1900 type: text invisible: y onload: y cvalue: ((localtime)[5])

#### Current year

By adding '1900' to the calculation, the year = 2020.

6A. Year + 1900 type: text invisible: y onload: y cvalue: ((localtime)[5] + 1900)

### Day of the week

Like months, the day of the week is __0-indexed and runs from 0-6, starting with Sunday__. The day of the week may also be calculated from both open and closed-end responses as well.

#### Default day of the week

Using the above example, since it was Friday, the day of the week = 5.

7. Day of the week type: text invisible: y onload: y cvalue: ((localtime)[6])

#### Day of the week (open)

7A. Day of the week - name (open) type: text invisible: y onload: y cvalue:<<END my @days = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday); my $dow = ((localtime)[6]); return $days[$dow]; END

#### Day of the week (closed)

The following example adds '1' to the returned value for `localtime`

.

7B. Day of the week - name (closed) type: radio invisible: y onload: y cvalue: ((localtime)[6] + 1) 1. Sunday 2. Monday 3. Tuesday 4. Wednesday 5. Thursday 6. Friday 7. Saturday

### Day of the year

Using the above example, for October 3oth, 2020, the day of the year = 303.

8. Day of the year type: text invisible: y onload: y cvalue: ((localtime)[7])

### Daylight savings time

Using the above example, since daylight savings time was still in effect on October 30th, 2020, daylight savings time = 1.

9. Daylight savings time type: text invisible: y onload: y cvalue: ((localtime)[8])

## Comments

0 comments

Please sign in to leave a comment.