Flutter: DateTime.now does not reflect manually set Timezone

Viewed 2235

To test the effect of different timezones I changed the Date & Time settings and the timezone of my phone manually. However DateTime.now() always returns the time in CEST.

My phone is connected to the Wifi but not logged in into a mobile network.

Is this the intended behavior? I am not sure what the local in the following refers to:

Constructs a DateTime instance with current date and time in the local time zone.

Is there another way to get phone time?


Flutter Doctor

[√] Flutter (Channel stable, v1.5.4-hotfix.2, on Microsoft Windows, locale en-US)
    • Flutter version 1.5.4-hotfix.2 at <path>\flutter
    • Framework revision 7a4c33425d (8 weeks ago), 2019-04-29 11:05:24 -0700
    • Engine revision 52c7a1e849
    • Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at <path>\android-sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: <path>\android-studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

[√] Android Studio (version 3.4)
    • Android Studio at <path>\android-studio
    • Flutter plugin version 34.0.2
    • Dart plugin version 183.5901
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[√] VS Code (version 1.35.1)
    • VS Code at <path>\VSCode\code.exe
    • Flutter extension version 3.1.0

[√] Connected device (1 available)
    • Wileyfox Swift • b6d5f2fa • android-arm64 • Android 7.1.2 (API 25)

• No issues found!

Example App

import 'dart:async';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class DateTimeWidget extends StatefulWidget {
  DateTimeWidget({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _DateTimeWidgetState();
}

class _DateTimeWidgetState extends State<DateTimeWidget> {
  DateTime time;
  Timer updateTimer;

  _DateTimeWidgetState() : time = DateTime.now();

  void timerFunction(Timer timer) {
    setState(() {
       this.time = DateTime.now();
    });
  }

  @override
  void initState() {
    super.initState();
    this.updateTimer = Timer.periodic(Duration(seconds: 1), this.timerFunction);
  }

  @override
  void dispose() {
    this.updateTimer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Text("${this.time.toString()}");
  }
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Date Time',
      home: Scaffold(
        body: Container(
          child: Center(
            child: DateTimeWidget(),
          ),
        ),
      ),
    );
  }
}
1 Answers

Using Flutter 2.0 on your code with an Android 11 device, the app correctly reflects time of the device even when changed. If the issue still persists, it might be a bug that's better posted here https://github.com/flutter/flutter/issues/

Related